import type { MarkdownFileType } from './file'; import { getRoadmapById, type RoadmapFileType } from './roadmap.ts'; export const projectDifficulties = [ 'beginner', 'intermediate', 'advanced', ] as const; export type ProjectDifficultyType = (typeof projectDifficulties)[number]; export interface ProjectFrontmatter { title: string; description: string; isNew: boolean; sort: number; difficulty: ProjectDifficultyType; nature: string; skills: string[]; seo: { title: string; description: string; keywords: string[]; ogImageUrl: string; }; roadmapIds: string[]; } export type ProjectFileType = MarkdownFileType & { id: string; roadmaps: RoadmapFileType[]; }; /** * Generates id from the given project file * @param filePath Markdown file path * * @returns unique project identifier */ function projectPathToId(filePath: string): string { const fileName = filePath.split('/').pop() || ''; return fileName.replace('.md', ''); } export async function getProjectsByRoadmapId( roadmapId: string, ): Promise { const projects = await getAllProjects(); return projects.filter((project) => project.frontmatter?.roadmapIds?.includes(roadmapId), ); } let tempProjects: ProjectFileType[] = []; /** * Gets all the projects sorted by the publishing date * @returns Promisifed project files */ export async function getAllProjects(): Promise { if (tempProjects.length) { return tempProjects; } const projects = import.meta.glob( '/src/data/projects/*.md', { eager: true, }, ); tempProjects = Object.values(projects).map((projectFile) => ({ ...projectFile, id: projectPathToId(projectFile.file), })); return tempProjects; } export async function getProjectById( groupId: string, ): Promise { const project = await import(`../data/projects/${groupId}.md`); const roadmapIds = project.frontmatter.roadmapIds || []; const roadmaps = await Promise.all( roadmapIds.map((roadmapId: string) => getRoadmapById(roadmapId)), ); return { ...project, roadmaps: roadmaps, id: projectPathToId(project.file), }; }