computer-scienceangular-roadmapbackend-roadmapblockchain-roadmapdba-roadmapdeveloper-roadmapdevops-roadmapfrontend-roadmapgo-roadmaphactoberfestjava-roadmapjavascript-roadmapnodejs-roadmappython-roadmapqa-roadmapreact-roadmaproadmapstudy-planvue-roadmapweb3-roadmap
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.2 KiB
66 lines
2.2 KiB
import type { MarkdownFileType } from './file'; |
|
import type { BestPracticeFrontmatter } from './best-practice'; |
|
|
|
// Generates URL from the topic file path e.g. |
|
// -> /src/data/best-practices/frontend-performance/content/100-use-https-everywhere |
|
// /best-practices/frontend-performance/use-https-everywhere |
|
// -> /src/data/best-practices/frontend-performance/content/102-use-cdn-for-static-assets |
|
// /best-practices/use-cdn-for-static-assets |
|
function generateTopicUrl(filePath: string) { |
|
return filePath |
|
.replace('/src/data/best-practices/', '/') // Remove the base `/src/data/best-practices` from path |
|
.replace('/content', '') // Remove the `/[bestPracticeId]/content` |
|
.replace(/\/\d+-/g, '/') // Remove ordering info `/101-ecosystem` |
|
.replace(/\/index\.md$/, '') // Make the `/index.md` to become the parent folder only |
|
.replace(/\.md$/, ''); // Remove `.md` from the end of file |
|
} |
|
|
|
export interface BestPracticeTopicFileType { |
|
url: string; |
|
heading: string; |
|
file: MarkdownFileType; |
|
bestPractice: BestPracticeFrontmatter; |
|
bestPracticeId: string; |
|
} |
|
|
|
/** |
|
* Gets all the topic files available for all the best practices |
|
* @returns Hashmap containing the topic slug and the topic file content |
|
*/ |
|
export async function getAllBestPracticeTopicFiles(): Promise< |
|
Record<string, BestPracticeTopicFileType> |
|
> { |
|
const contentFiles = await import.meta.glob<string>( |
|
'/src/data/best-practices/*/content/**/*.md', |
|
{ |
|
eager: true, |
|
}, |
|
); |
|
|
|
const mapping: Record<string, BestPracticeTopicFileType> = {}; |
|
|
|
for (let filePath of Object.keys(contentFiles)) { |
|
const fileContent: MarkdownFileType = contentFiles[filePath] as any; |
|
const fileHeadings = fileContent.getHeadings(); |
|
const firstHeading = fileHeadings[0]; |
|
|
|
const [, bestPracticeId] = |
|
filePath.match(/^\/src\/data\/best-practices\/(.+)?\/content\/(.+)?$/) || |
|
[]; |
|
const topicUrl = generateTopicUrl(filePath); |
|
|
|
const currentBestPractice = await import( |
|
`../data/best-practices/${bestPracticeId}/${bestPracticeId}.md` |
|
); |
|
|
|
mapping[topicUrl] = { |
|
url: topicUrl, |
|
heading: firstHeading?.text, |
|
file: fileContent, |
|
bestPractice: currentBestPractice?.frontmatter, |
|
bestPracticeId: bestPracticeId, |
|
}; |
|
} |
|
|
|
return mapping; |
|
}
|
|
|