Simple blog system (not quite done)

This commit is contained in:
2025-12-03 18:50:37 +01:00
parent 36ce2b9888
commit 0e128b5308
9 changed files with 224 additions and 100 deletions
+45
View File
@@ -0,0 +1,45 @@
// app/api/blog/[slug]/route.ts
import { NextRequest, NextResponse } from "next/server";
import fs from "fs";
import path from "path";
import { PostPreview } from "@/lib/posts";
const BLOG_DIR = path.join(process.cwd(), "blog");
export async function GET(
req: NextRequest,
{ params }: { params: Promise<{ slug: string }> }
) {
try {
const { slug } = await params;
// Read JSON metadata
const jsonPath = path.join(BLOG_DIR, `${slug}.json`);
if (!fs.existsSync(jsonPath)) {
return NextResponse.json({ error: "Post not found" }, { status: 404 });
}
const rawData = fs.readFileSync(jsonPath, "utf-8");
const data: Omit<PostPreview, "url"> = JSON.parse(rawData);
// Read Markdown content
const mdPath = path.join(BLOG_DIR, `${slug}.md`);
let content = "";
if (fs.existsSync(mdPath)) {
content = fs.readFileSync(mdPath, "utf-8");
}
return NextResponse.json({
meta: {
...data,
url: `/blog/${slug}`,
},
content,
});
} catch (err) {
console.error(err);
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
}