feat(snippets): introduce reusable code chunks section, add cms support, and fix navbar turkish characters

This commit is contained in:
Poyraz Avsever
2026-03-30 11:30:14 +03:00
parent d3c13ed25d
commit 31da9f114b
9 changed files with 308 additions and 5 deletions
+44
View File
@@ -0,0 +1,44 @@
import fs from "node:fs/promises";
import path from "node:path";
import matter from "gray-matter";
export type Snippet = {
slug: string;
title: string;
language: string;
category: string;
description: string;
markdown: string;
};
const SNIPPETS_DIR = path.join(process.cwd(), "content", "snippets");
export async function listSnippets(): Promise<Snippet[]> {
let files: string[] = [];
try {
files = await fs.readdir(SNIPPETS_DIR);
} catch {
return [];
}
const mdFiles = files.filter((f) => f.endsWith(".md"));
const snippets = await Promise.all(
mdFiles.map(async (fileName) => {
const raw = await fs.readFile(path.join(SNIPPETS_DIR, fileName), "utf8");
const parsed = matter(raw);
const slug = fileName.replace(/\.md$/i, "");
return {
slug,
title: String(parsed.data.title || slug),
language: String(parsed.data.language || "text"),
category: String(parsed.data.category || "General"),
description: String(parsed.data.description || ""),
markdown: parsed.content.trim(),
};
}),
);
return snippets.sort((a, b) => a.title.localeCompare(b.title));
}