Toipc pages rendering

pull/3356/head
Kamran Ahmed 2 years ago
parent 813a3d9b2b
commit 190c75cebe
  1. 1
      src/best-practices/frontend-performance/content/minimize-iframes.md
  2. 10
      src/lib/best-practice-topic.ts
  3. 40
      src/pages/best-practices/[bestPracticeId]/[...topicId].astro

@ -15,7 +15,7 @@ function generateTopicUrl(filePath: string) {
.replace(/\.md$/, ''); // Remove `.md` from the end of file .replace(/\.md$/, ''); // Remove `.md` from the end of file
} }
interface TopicFileType { export interface BestPracticeTopicFileType {
url: string; url: string;
heading: string; heading: string;
file: MarkdownFileType; file: MarkdownFileType;
@ -27,12 +27,12 @@ interface TopicFileType {
* Gets all the topic files available for all the best practices * Gets all the topic files available for all the best practices
* @returns Hashmap containing the topic slug and the topic file content * @returns Hashmap containing the topic slug and the topic file content
*/ */
async function getTopicFiles(): Promise<Record<string, TopicFileType>> { export async function getAllBestPracticeTopicFiles(): Promise<Record<string, BestPracticeTopicFileType>> {
const contentFiles = await import.meta.glob<string>('/src/best-practices/*/content/**/*.md', { const contentFiles = await import.meta.glob<string>('/src/best-practices/*/content/**/*.md', {
eager: true, eager: true,
}); });
const mapping: Record<string, TopicFileType> = {}; const mapping: Record<string, BestPracticeTopicFileType> = {};
for (let filePath of Object.keys(contentFiles)) { for (let filePath of Object.keys(contentFiles)) {
const fileContent: MarkdownFileType = contentFiles[filePath] as any; const fileContent: MarkdownFileType = contentFiles[filePath] as any;
@ -63,8 +63,8 @@ async function getTopicFiles(): Promise<Record<string, TopicFileType>> {
* *
* @returns Promise<TopicFileType[]> * @returns Promise<TopicFileType[]>
*/ */
export async function getTopicsByBestPracticeId(bestPracticeId: string): Promise<TopicFileType[]> { export async function getTopicsByBestPracticeId(bestPracticeId: string): Promise<BestPracticeTopicFileType[]> {
const topicFileMapping = await getTopicFiles(); const topicFileMapping = await getAllBestPracticeTopicFiles();
const allTopics = Object.values(topicFileMapping); const allTopics = Object.values(topicFileMapping);
return allTopics.filter((topic) => topic.bestPracticeId === bestPracticeId); return allTopics.filter((topic) => topic.bestPracticeId === bestPracticeId);

@ -0,0 +1,40 @@
---
import BaseLayout from '../../../layouts/BaseLayout.astro';
import { BestPracticeTopicFileType, getAllBestPracticeTopicFiles } from '../../../lib/best-practice-topic';
export async function getStaticPaths() {
const topicPathMapping = await getAllBestPracticeTopicFiles();
return Object.keys(topicPathMapping).map((topicSlug) => {
const topicDetails = topicPathMapping[topicSlug];
const bestPracticeId = topicDetails.bestPracticeId;
const topicId = topicSlug.replace(`/${bestPracticeId}/`, '');
return {
params: {
topicId,
bestPracticeId,
},
props: topicDetails,
};
});
}
const { topicId } = Astro.params;
const { file, bestPracticeId, bestPractice, heading } = Astro.props as BestPracticeTopicFileType;
---
<BaseLayout
title={`${heading} - roadmap.sh`}
description={`Free resources to learn ${heading} in ${bestPractice.featuredTitle}. Everything you need to know about ${heading} and how it realtes to ${bestPractice.featuredTitle}.`}
noIndex={true}
permalink={`/${topicId}`}
>
<div class='bg-gray-50'>
<div class='container pb-16 prose prose-p:mt-0 prose-h1:mb-4 prose-h2:mb-3 prose-h2:mt-0'>
<main id='main-content'>
<file.Content />
</main>
</div>
</div>
</BaseLayout>
Loading…
Cancel
Save