rust parser working on local build. fix docker build

This commit is contained in:
ZockerKatze
2025-06-24 10:23:34 +02:00
parent 7e2ada529d
commit 5ad73485ce
10 changed files with 425 additions and 24 deletions

View File

@@ -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(

View File

@@ -33,6 +33,7 @@ export default function Home() {
const [search, setSearch] = useState('');
const [isLoading, setIsLoading] = useState(false);
const [lastUpdate, setLastUpdate] = useState<Date | null>(null);
const [error, setError] = useState<string | null>(null);
// Get blog owner from env
const blogOwner = process.env.NEXT_PUBLIC_BLOG_OWNER || 'Anonymous';
@@ -99,12 +100,17 @@ export default function Home() {
const loadTree = async () => {
try {
setIsLoading(true);
setError(null);
const response = await fetch('/api/posts');
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
const data = await response.json();
setTree(data);
setLastUpdate(new Date());
} catch (error) {
console.error('Fehler beim Laden der Beiträge:', error);
setError(error instanceof Error ? error.message : String(error));
} finally {
setIsLoading(false);
}
@@ -168,6 +174,12 @@ export default function Home() {
return (
<main className="container mx-auto px-3 sm:px-4 py-4 sm:py-8">
{/* Error display */}
{error && (
<div className="mb-4 p-4 bg-red-100 text-red-800 rounded">
<strong>Fehler:</strong> {error}
</div>
)}
{/* Mobile-first header section */}
<div className="mb-6 sm:mb-8 space-y-4 sm:space-y-0 sm:flex sm:flex-row sm:gap-4 sm:items-center sm:justify-between">
<h1 className="text-2xl sm:text-3xl md:text-4xl font-bold text-center sm:text-left">{blogOwner}&apos;s Blog</h1>

View File

@@ -14,6 +14,13 @@ interface Post {
createdAt: string;
}
// Runtime statistics for parser usage
const parserStats = {
rust: 0,
typescript: 0,
lastRustError: '',
};
export default function PostPage({ params }: { params: { slug: string[] } }) {
const [post, setPost] = useState<Post | null>(null);
// Modal state for zoomed image
@@ -648,6 +655,20 @@ export default function PostPage({ params }: { params: { slug: string[] } }) {
const loadPost = async () => {
try {
const response = await fetch(`/api/posts/${encodeURIComponent(slugPath)}`);
const parser = response.headers.get('X-Parser');
const rustError = response.headers.get('X-Rust-Parser-Error');
if (parser === 'rust') {
parserStats.rust++;
console.log('%c[Rust Parser] Used for this post.', 'color: green; font-weight: bold');
} else {
parserStats.typescript++;
console.log('%c[TypeScript Parser] Used for this post.', 'color: orange; font-weight: bold');
if (rustError) {
parserStats.lastRustError = rustError;
console.warn('[Rust Parser Error]', rustError);
}
}
console.info('[Parser Stats]', parserStats);
const data = await response.json();
setPost(data);
} catch (error) {

View File

@@ -221,8 +221,25 @@ export function watchPosts(callback: () => void) {
onChangeCallback = callback;
watcher = chokidar.watch(postsDirectory, {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true
ignored: [
/(^|[\/\\])\../, // ignore dotfiles
/node_modules/,
/\.git/,
/\.next/,
/\.cache/,
/\.DS_Store/,
/Thumbs\.db/,
/\.tmp$/,
/\.temp$/
],
persistent: true,
ignoreInitial: true, // Don't trigger on initial scan
awaitWriteFinish: {
stabilityThreshold: 1000, // Wait 1 second after file changes
pollInterval: 100 // Check every 100ms
},
usePolling: false, // Use native file system events when possible
interval: 1000 // Fallback polling interval (only used if native events fail)
});
watcher
@@ -235,20 +252,6 @@ function handleFileChange() {
if (onChangeCallback) {
onChangeCallback();
}
// Also notify via webhook if available
try {
fetch('/api/posts/webhook', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ type: 'update', timestamp: new Date().toISOString() })
}).catch(error => {
// Webhook is optional, so we don't need to handle this as a critical error
console.debug('Webhook notification failed:', error);
});
} catch (error) {
// Ignore webhook errors
}
}
export function stopWatching() {