no more typescript parser. only rust!
SSE Changed a bit to fit the Rust Parser
This commit is contained in:
@@ -1,172 +1,34 @@
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
import { NextResponse } from 'next/server';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import matter from 'gray-matter';
|
||||
import { marked } from 'marked';
|
||||
import DOMPurify from 'dompurify';
|
||||
import { JSDOM } from 'jsdom';
|
||||
import hljs from 'highlight.js';
|
||||
import { getPostsDirectory } from '@/lib/postsDirectory';
|
||||
import { spawnSync } from 'child_process';
|
||||
|
||||
const postsDirectory = getPostsDirectory();
|
||||
|
||||
// Function to get file creation date
|
||||
function getFileCreationDate(filePath: string): Date {
|
||||
const stats = fs.statSync(filePath);
|
||||
return stats.birthtime ?? stats.mtime;
|
||||
}
|
||||
|
||||
// Function to generate ID from text (matches frontend logic)
|
||||
function generateId(text: string): string {
|
||||
return text
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '');
|
||||
}
|
||||
|
||||
const renderer = new marked.Renderer();
|
||||
|
||||
// Custom heading renderer to add IDs
|
||||
renderer.heading = (text, level) => {
|
||||
const id = generateId(text);
|
||||
return `<h${level} id="${id}">${text}</h${level}>`;
|
||||
};
|
||||
|
||||
renderer.code = (code, infostring, escaped) => {
|
||||
const lang = (infostring || '').match(/\S*/)?.[0];
|
||||
const highlighted = lang && hljs.getLanguage(lang)
|
||||
? hljs.highlight(code, { language: lang }).value
|
||||
: hljs.highlightAuto(code).value;
|
||||
const langClass = lang ? `language-${lang}` : '';
|
||||
return `<pre><code class="hljs ${langClass}">${highlighted}</code></pre>`;
|
||||
};
|
||||
|
||||
marked.setOptions({
|
||||
gfm: true,
|
||||
breaks: true,
|
||||
renderer,
|
||||
});
|
||||
|
||||
async function getPostBySlug(slug: string) {
|
||||
const realSlug = slug.replace(/\.md$/, '');
|
||||
const fullPath = path.join(postsDirectory, `${realSlug}.md`);
|
||||
let rustResult;
|
||||
try {
|
||||
// Try Rust backend first
|
||||
rustResult = spawnSync(
|
||||
path.resolve(process.cwd(), 'markdown_backend/target/release/markdown_backend'),
|
||||
['show', realSlug],
|
||||
{ encoding: 'utf-8' }
|
||||
);
|
||||
if (rustResult.status === 0 && rustResult.stdout) {
|
||||
// Expect Rust to output a JSON object matching the post shape
|
||||
const post = JSON.parse(rustResult.stdout);
|
||||
// Map snake_case to camelCase for frontend compatibility
|
||||
post.createdAt = post.created_at;
|
||||
delete post.created_at;
|
||||
return post;
|
||||
} else {
|
||||
console.error('[Rust parser error]', rustResult.stderr || rustResult.error);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[Rust parser exception]', e);
|
||||
}
|
||||
|
||||
// Fallback to TypeScript parser
|
||||
const fileContents = fs.readFileSync(fullPath, 'utf8');
|
||||
const { data, content } = matter(fileContents);
|
||||
const createdAt = getFileCreationDate(fullPath);
|
||||
|
||||
let processedContent = '';
|
||||
try {
|
||||
// Convert markdown to HTML
|
||||
const rawHtml = marked.parse(content);
|
||||
const window = new JSDOM('').window;
|
||||
const purify = DOMPurify(window);
|
||||
processedContent = purify.sanitize(rawHtml as string, {
|
||||
ALLOWED_TAGS: [
|
||||
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
|
||||
'p', 'a', 'ul', 'ol', 'li', 'blockquote',
|
||||
'pre', 'code', 'em', 'strong', 'del',
|
||||
'hr', 'br', 'img', 'table', 'thead', 'tbody',
|
||||
'tr', 'th', 'td', 'div', 'span', 'iframe'
|
||||
],
|
||||
ALLOWED_ATTR: [
|
||||
'class', 'id', 'style',
|
||||
'href', 'target', 'rel',
|
||||
'src', 'alt', 'title', 'width', 'height',
|
||||
'frameborder', 'allowfullscreen'
|
||||
],
|
||||
ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp):|[^a-z]|[a-z+.-]+(?:[^a-z+.-:]|$))/i
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(`Error processing markdown for slug "${realSlug}":`, err);
|
||||
processedContent = `<div class="error-message">
|
||||
<p>Error processing markdown content. Please check the console for details.</p>
|
||||
<pre>${err instanceof Error ? err.message : 'Unknown error'}</pre>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
return {
|
||||
slug: realSlug,
|
||||
title: data.title,
|
||||
date: data.date,
|
||||
tags: data.tags || [],
|
||||
summary: data.summary,
|
||||
content: processedContent,
|
||||
createdAt: createdAt.toISOString(),
|
||||
author: (process.env.NEXT_PUBLIC_BLOG_OWNER || 'Anonymous') + "'s",
|
||||
};
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: { slug: string[] | string } }
|
||||
) {
|
||||
let parser = 'typescript';
|
||||
let rustError = '';
|
||||
try {
|
||||
const slugArr = Array.isArray(params.slug) ? params.slug : [params.slug];
|
||||
const slugPath = slugArr.join('/');
|
||||
let post;
|
||||
try {
|
||||
const rustResult = spawnSync(
|
||||
path.resolve(process.cwd(), 'markdown_backend/target/release/markdown_backend'),
|
||||
['show', slugPath],
|
||||
{ encoding: 'utf-8' }
|
||||
);
|
||||
if (rustResult.status === 0 && rustResult.stdout) {
|
||||
post = JSON.parse(rustResult.stdout);
|
||||
post.createdAt = post.created_at;
|
||||
delete post.created_at;
|
||||
parser = 'rust';
|
||||
} else {
|
||||
rustError = rustResult.stderr || rustResult.error?.toString() || 'Unknown error';
|
||||
console.error('[Rust parser error]', rustError);
|
||||
}
|
||||
} catch (e) {
|
||||
rustError = e instanceof Error ? e.message : String(e);
|
||||
console.error('[Rust parser exception]', rustError);
|
||||
const rustResult = spawnSync(
|
||||
path.resolve(process.cwd(), 'markdown_backend/target/release/markdown_backend'),
|
||||
['show', slugPath],
|
||||
{ encoding: 'utf-8' }
|
||||
);
|
||||
if (rustResult.status === 0 && rustResult.stdout) {
|
||||
const post = JSON.parse(rustResult.stdout);
|
||||
post.createdAt = post.created_at;
|
||||
delete post.created_at;
|
||||
return NextResponse.json(post);
|
||||
} else {
|
||||
const rustError = rustResult.stderr || rustResult.error?.toString() || 'Unknown error';
|
||||
return NextResponse.json({ error: 'Rust parser error', details: rustError }, { status: 500 });
|
||||
}
|
||||
if (!post) {
|
||||
post = await getPostBySlug(slugPath);
|
||||
}
|
||||
const response = NextResponse.json(post);
|
||||
response.headers.set('X-Parser', parser);
|
||||
if (parser !== 'rust' && rustError) {
|
||||
response.headers.set('X-Rust-Parser-Error', rustError);
|
||||
}
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('Error loading post:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Error loading post',
|
||||
details: error instanceof Error ? error.message : 'Unknown error'
|
||||
},
|
||||
{ error: 'Error loading post', details: error instanceof Error ? error.message : 'Unknown error' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user