Refactor navigation links to use Next.js routing and improve post handling
- Updated AboutButton to navigate to the about page using Next.js router. - Changed HeaderButtons and MobileNav to link directly to the about page. - Modified Home component to exclude the 'about' post from the posts list. - Added a helper function to strip YAML frontmatter from post summaries. - Enhanced API routes to handle reading and writing markdown files for posts.
This commit is contained in:
@@ -19,6 +19,13 @@ export async function GET(
|
||||
);
|
||||
if (rustResult.status === 0 && rustResult.stdout) {
|
||||
const post = JSON.parse(rustResult.stdout);
|
||||
const fs = require('fs');
|
||||
const filePath = path.join(postsDirectory, slugPath + '.md');
|
||||
let raw = '';
|
||||
try {
|
||||
raw = fs.readFileSync(filePath, 'utf8');
|
||||
} catch {}
|
||||
post.raw = raw;
|
||||
post.createdAt = post.created_at;
|
||||
delete post.created_at;
|
||||
return NextResponse.json(post);
|
||||
@@ -33,4 +40,20 @@ export async function GET(
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request, { params }: { params: { slug: string[] | string } }) {
|
||||
try {
|
||||
const { markdown } = await request.json();
|
||||
if (typeof markdown !== 'string') {
|
||||
return NextResponse.json({ error: 'Invalid markdown' }, { status: 400 });
|
||||
}
|
||||
const slugArr = Array.isArray(params.slug) ? params.slug : [params.slug];
|
||||
const slugPath = slugArr.join('/');
|
||||
const filePath = path.join(postsDirectory, slugPath + '.md');
|
||||
require('fs').writeFileSync(filePath, markdown, 'utf8');
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Error saving file', details: error instanceof Error ? error.message : 'Unknown error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
26
src/app/api/posts/preview/route.ts
Normal file
26
src/app/api/posts/preview/route.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { spawnSync } from 'child_process';
|
||||
import path from 'path';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { markdown } = await request.json();
|
||||
if (typeof markdown !== 'string') {
|
||||
return NextResponse.json({ error: 'Invalid markdown' }, { status: 400 });
|
||||
}
|
||||
// Call Rust backend with 'render' command, pass markdown via stdin
|
||||
const rustPath = path.resolve(process.cwd(), 'markdown_backend/target/release/markdown_backend');
|
||||
const rustResult = spawnSync(rustPath, ['render'], {
|
||||
input: markdown,
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
if (rustResult.status === 0 && rustResult.stdout) {
|
||||
return NextResponse.json({ html: rustResult.stdout });
|
||||
} else {
|
||||
const rustError = rustResult.stderr || rustResult.error?.toString() || 'Unknown error';
|
||||
return NextResponse.json({ error: 'Rust parser error', details: rustError }, { status: 500 });
|
||||
}
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Error rendering markdown', details: error instanceof Error ? error.message : 'Unknown error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -73,6 +73,10 @@ async function readPostsDir(dir: string, relDir = '', pinnedData: { pinned: stri
|
||||
const posts: any[] = [];
|
||||
|
||||
for (const entry of entries) {
|
||||
// Skip the 'assets' folder
|
||||
if (entry.isDirectory() && entry.name === 'assets' && relDir === '') {
|
||||
continue;
|
||||
}
|
||||
const fullPath = path.join(dir, entry.name);
|
||||
const relPath = relDir ? path.join(relDir, entry.name) : entry.name;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user