project pages created and populated automatically

This commit is contained in:
2026-04-16 10:15:14 +02:00
parent 82e6e3dc01
commit 5e1368f790
17 changed files with 2320 additions and 101 deletions
+42
View File
@@ -0,0 +1,42 @@
import fs from "node:fs";
import path from "node:path";
import matter from "gray-matter";
import { Project, ProjectFrontmatter } from "./types";
const projectsDirectory = path.join(process.cwd(), "content/projects");
function fileNameToSlug(fileName: string) {
return fileName.replace(/\.mdx$/, "");
}
export function getProjectSlugs() {
return fs
.readdirSync(projectsDirectory)
.filter((file) => file.endsWith(".mdx"))
.map(fileNameToSlug);
}
export function getProjectBySlug(slug: string): Project | null {
const realSlug = slug.replace(/\.mdx$/, "");
const fullPath = path.join(projectsDirectory, `${realSlug}.mdx`);
if (!fs.existsSync(fullPath)) {
return null;
}
const fileContents = fs.readFileSync(fullPath, "utf8");
const { data, content } = matter(fileContents);
return {
slug: realSlug,
...(data as ProjectFrontmatter),
content,
};
}
export function getAllProjects(): Project[] {
return getProjectSlugs()
.map((slug) => getProjectBySlug(slug))
.filter((project): project is Project => project !== null)
.sort((a, b) => b.year - a.year);
}