This commit is contained in:
ZockerKatze
2025-06-16 17:47:03 +02:00
parent a93a259ee5
commit 1922125adb
7 changed files with 625 additions and 0 deletions

View File

@@ -0,0 +1 @@

View 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 }
);
}
}

View File

@@ -0,0 +1,55 @@
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 formData = await request.formData();
const file = formData.get('file') as File;
const folderPath = formData.get('path') as string;
if (!file) {
return NextResponse.json(
{ error: 'No file provided' },
{ status: 400 }
);
}
// Read the file content
const content = await file.text();
// Parse frontmatter
const { data, content: markdownContent } = matter(content);
// Create slug from filename (without .md extension)
const slug = file.name.replace(/\.md$/, '');
// Create the full path for the file
const fullPath = path.join(postsDirectory, folderPath, file.name);
// Ensure the directory exists
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
// Write the file
fs.writeFileSync(fullPath, content);
return NextResponse.json({
success: true,
path: fullPath,
slug,
title: data.title || slug,
date: data.date || new Date().toISOString().split('T')[0],
tags: data.tags || [],
summary: data.summary || '',
});
} catch (error) {
console.error('Error uploading file:', error);
return NextResponse.json(
{ error: 'Error uploading file' },
{ status: 500 }
);
}
}