Files
developer-roadmap/src/lib/best-practice-topic.ts
T
Kamran AhmedandArik Chakma ad6002a514 feat: profile pages, custom roadmap pages and SSR (#5494)
* Update

* Add stats and health endpoints

* Add pre-render

* fix: redirect to the error page

* Fix generate-renderer issue

* Rename

* Fix best practice topics not loading

* Handle SSR for static pages

* Refactor faqs

* Refactor best practices

* Fix absolute import

* Fix stats

* Add custom roadmap page

* Minor UI change

* feat: custom roadmap slug routes (#4987)

* feat: replace roadmap slug

* fix: remove roadmap slug

* feat: username route

* fix: user public page

* feat: show roadmap progress

* feat: update public profile

* fix: replace with toast

* feat: user public profile page

* feat: implement profile form

* feat: implement user profile roadmap page

* refactor: remove logs

* fix: increase progress gap

* fix: remove title margin

* fix: breakpoint for roadmaps

* Update dependencies

* Upgrade dependencies

* fix: improper avatars

* fix: heatmap focus

* wip: remove `getStaticPaths`

* fix: add disable props

* wip

* feat: add email icon

* fix: update pnpm lock

* fix: implement author page

* Fix beginner roadmaps not working

* Changes to form

* Refactor profile and form

* Refactor public profile form

* Rearrange sidebar items

* Update UI for public form

* Minor text update

* Refactor public profile form

* Error page for user

* Revamp UI for profile page

* Add public profile page

* Fix vite warnings

* Add private profile banner

* feat: on blur check username

* Update fetch depth

* Add error detail

* Use hybrid mode of rendering

* Do not pre-render stats pages

* Update deployment workflow

* Update deployment workflow

---------

Co-authored-by: Arik Chakma <arikchangma@gmail.com>
2024-04-11 22:35:52 +01:00

67 lines
2.2 KiB
TypeScript

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;
}