rust parser working on local build. fix docker build
This commit is contained in:
@@ -9,6 +9,7 @@ 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();
|
||||
|
||||
@@ -52,6 +53,29 @@ marked.setOptions({
|
||||
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);
|
||||
@@ -60,12 +84,8 @@ async function getPostBySlug(slug: string) {
|
||||
try {
|
||||
// Convert markdown to HTML
|
||||
const rawHtml = marked.parse(content);
|
||||
|
||||
// Create a DOM window for DOMPurify
|
||||
const window = new JSDOM('').window;
|
||||
const purify = DOMPurify(window);
|
||||
|
||||
// Sanitize the HTML
|
||||
processedContent = purify.sanitize(rawHtml as string, {
|
||||
ALLOWED_TAGS: [
|
||||
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
|
||||
@@ -80,11 +100,10 @@ async function getPostBySlug(slug: string) {
|
||||
'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
|
||||
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);
|
||||
// Return a more informative error message in the content
|
||||
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>
|
||||
@@ -107,11 +126,40 @@ 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('/');
|
||||
const post = await getPostBySlug(slugPath);
|
||||
return NextResponse.json(post);
|
||||
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);
|
||||
}
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user