yes
This commit is contained in:
39
src/app/api/admin/posts/route.ts
Normal file
39
src/app/api/admin/posts/route.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import matter from 'gray-matter';
|
||||
|
||||
const postsDirectory = path.join(process.cwd(), 'posts');
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { title, date, tags, summary, content } = body;
|
||||
|
||||
// Create slug from title
|
||||
const slug = title
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/(^-|-$)/g, '');
|
||||
|
||||
// Create frontmatter
|
||||
const frontmatter = matter.stringify(content, {
|
||||
title,
|
||||
date,
|
||||
tags,
|
||||
summary,
|
||||
});
|
||||
|
||||
// Write the file
|
||||
const filePath = path.join(postsDirectory, `${slug}.md`);
|
||||
fs.writeFileSync(filePath, frontmatter);
|
||||
|
||||
return NextResponse.json({ success: true, slug });
|
||||
} catch (error) {
|
||||
console.error('Error creating post:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Error creating post' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user