import React, { lazy, Suspense } from 'react'; const KatexRenderer = lazy(() => import('../components/KatexRenderer')); export function renderKatex(math: string): string { const escaped = math .replace(/&/g, '&') .replace(//g, '>'); return `${escaped}`; } /** * Tokenize and render inline text elements (inline math, code, bold, italic, links). */ export function renderInlineMarkdown(text: string): React.ReactNode[] { if (!text) return []; // Regex matching inline tokens in order: // 1. Display math: $$ ... $$ // 2. Display math: \[ ... \] // 3. Inline math: \( ... \) // 4. Inline math: $ ... $ (non-greedy) // 5. Inline code: ` ... ` // 6. Bold: ** ... ** or __ ... __ // 7. Italic: * ... * or _ ... _ // 8. Link: [text](url) const tokenRegex = /(\$\$[\s\S]*?\$\$|\\\[[\s\S]*?\\\]|\\\([\s\S]*?\\\)|\$(?:\\\$|[^$\n])+?\$|`[^`\n]+`|\*\*[^*\n]+\*\*|__[^_\n]+__|(? { if (!part) return null; // 1. Display math $$ ... $$ if (part.startsWith('$$') && part.endsWith('$$') && part.length >= 4) { const math = part.slice(2, -2); if (!math.trim()) return part; return ( {math}}> ); } // 2. Display math \[ ... \] if (part.startsWith('\\[') && part.endsWith('\\]') && part.length >= 4) { const math = part.slice(2, -2); if (!math.trim()) return part; return ( {math}}> ); } // 3. Inline math \( ... \) if (part.startsWith('\\(') && part.endsWith('\\)') && part.length >= 4) { const math = part.slice(2, -2); if (!math.trim()) return part; return ( {math}}> ); } // 4. Inline math $ ... $ if (part.startsWith('$') && part.endsWith('$') && part.length >= 2) { const math = part.slice(1, -1); if (!math.trim()) return part; return ( {math}}> ); } // 5. Inline code ` ... ` if (part.startsWith('`') && part.endsWith('`') && part.length >= 2) { return ( {part.slice(1, -1)} ); } // 6. Bold ** ... ** or __ ... __ if ( (part.startsWith('**') && part.endsWith('**') && part.length >= 4) || (part.startsWith('__') && part.endsWith('__') && part.length >= 4) ) { return ( {renderInlineMarkdown(part.slice(2, -2))} ); } // 7. Italic * ... * or _ ... _ if ( (part.startsWith('*') && part.endsWith('*') && part.length >= 2) || (part.startsWith('_') && part.endsWith('_') && part.length >= 2) ) { return ( {renderInlineMarkdown(part.slice(1, -1))} ); } // 8. Link [text](url) const linkMatch = part.match(/^\[([^\]]+)\]\(([^)]+)\)$/); if (linkMatch) { return ( {linkMatch[1]} ); } return part; }); }