import { NextRequest, NextResponse } from "next/server"; import { headers } from "next/headers"; import { auth } from "@/lib/auth"; import matter from "gray-matter"; import { mdToTiptap } from "@/lib/md-to-tiptap"; export async function POST(req: NextRequest) { const session = await auth.api.getSession({ headers: await headers() }); if (!session || session.user.role !== "admin") { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } const formData = await req.formData(); const file = formData.get("file") as File | null; if (!file) { return NextResponse.json({ error: "No file provided" }, { status: 400 }); } if (!file.name.endsWith(".md")) { return NextResponse.json({ error: "Only .md files are supported" }, { status: 400 }); } const raw = await file.text(); const { data: fm, content } = matter(raw); // Extract known frontmatter fields (Obsidian-compatible naming) const title = typeof fm.title === "string" ? fm.title.trim() : null; const kinescopeId = (fm.kinescopeId ?? fm.kinescope_id ?? fm.videoId ?? fm.video_id ?? "") as string; const order = typeof fm.order === "number" ? fm.order : null; const published = typeof fm.published === "boolean" ? fm.published : null; const tiptapContent = mdToTiptap(content); return NextResponse.json({ title, kinescopeId, order, published, content: tiptapContent }); }