diff --git a/frontend/src/lib/utils/markdown.ts b/frontend/src/lib/utils/markdown.ts index c60bf2e..52b3d23 100644 --- a/frontend/src/lib/utils/markdown.ts +++ b/frontend/src/lib/utils/markdown.ts @@ -2,8 +2,20 @@ import { Marked } from 'marked'; const marked = new Marked({ breaks: true, gfm: true }); +const CODE_OPEN = ''; +const CODE_CLOSE = ''; + export function processLatex(text: string): string { - return text + const placeholders: string[] = []; + const stash = (match: string) => { + const token = `${CODE_OPEN}${placeholders.length}${CODE_CLOSE}`; + placeholders.push(match); + return token; + }; + + const protectedText = text.replace(/```[\s\S]*?```/g, stash).replace(/`[^`\n]*`/g, stash); + + const rendered = protectedText .replace(/\$\$(.*?)\$\$/gs, (_, tex) => { const encoded = encodeURIComponent(tex.trim()); return `LaTeX`; @@ -12,6 +24,11 @@ export function processLatex(text: string): string { const encoded = encodeURIComponent(tex.trim()); return `LaTeX`; }); + + return rendered.replace( + new RegExp(`${CODE_OPEN}(\\d+)${CODE_CLOSE}`, 'g'), + (_, i) => placeholders[Number(i)] + ); } export function processContent(text: string): string {