Files
developer-roadmap/src/lib/project.ts
T
Kamran Ahmed ad3f28a8c4 feat: add projects functionality for backend roadmap (#6412)
* Add empty projects listing and cards

* Projects page header

* Projects listing and filtering

* Add project detail page

* Add a task tracker CLI project

* UI changes

* Add projects for expense and task tracker

* Add new projects

* Add blogging platform API project

* Project page UI changes

* Add todo list api project

* Add todo list api project

* Add a project idea for weather service

* Add expense tracker API project

* Add more details to expense tracker

* Add markdown note taking app idea

* Add URL shortener service project

* Add broadcast server project

* Add ecommerce project idea

* Add project on other roadmaps

* Add workout tracker project

* Add image processing project idea

* Add movie reservation system ap

* Add movie reservation system project

* Add realtime leader board system project

* Add details for a database backup utility

* Add project contribution docs

* Fix UI of the discover page
2024-08-07 16:35:19 +01:00

96 lines
2.2 KiB
TypeScript

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<ProjectFrontmatter> & {
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<ProjectFileType[]> {
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<ProjectFileType[]> {
if (tempProjects.length) {
return tempProjects;
}
const projects = import.meta.glob<ProjectFileType>(
'/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<ProjectFileType> {
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),
};
}