Enhance Admin page with post editing functionality; implement PUT API for saving post edits, including frontmatter parsing and error handling. Update welcome post metadata and improve UI for editing posts.
This commit is contained in:
@@ -63,4 +63,26 @@ export async function PATCH(request: Request) {
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: Request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { slug, path: folderPath, content } = body;
|
||||
if (!slug || typeof content !== 'string') {
|
||||
return NextResponse.json({ error: 'Missing slug or content' }, { status: 400 });
|
||||
}
|
||||
// Compute file path
|
||||
const filePath = folderPath && folderPath.trim() !== ''
|
||||
? path.join(postsDirectory, folderPath, `${slug}.md`)
|
||||
: path.join(postsDirectory, `${slug}.md`);
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return NextResponse.json({ error: 'File does not exist' }, { status: 404 });
|
||||
}
|
||||
fs.writeFileSync(filePath, content, 'utf8');
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error editing post:', error);
|
||||
return NextResponse.json({ error: 'Error editing post' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user