import { Marked } from 'marked'; const marked = new Marked({ breaks: true, gfm: true }); const CODE_OPEN = ''; const CODE_CLOSE = ''; export function processLatex(text: string): string { 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`; }) .replace(/\$(.+?)\$/g, (_, tex) => { 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 { return marked.parse(processLatex(text)) as string; }