/** * Interactive HTML Post-Processor * * Ported from Python's PostProcessor class (learn-your-way/concept_to_html.py:287-385) * * Handles: * - LaTeX delimiter conversion ($$...$$ -> \[...\], $...$ -> \(...\)) * - KaTeX CSS/JS injection with auto-render and MutationObserver * - Script tag protection during LaTeX conversion */ /** * Main entry point: post-process generated interactive HTML * Converts LaTeX delimiters and injects KaTeX rendering resources. */ export function postProcessInteractiveHtml(html: string): string { // Convert LaTeX delimiters while protecting script tags let processed = convertLatexDelimiters(html); // Inject KaTeX resources if not already present if (!processed.toLowerCase().includes('katex')) { processed = injectKatex(processed); } return processed; } /** * Convert LaTeX delimiters while protecting `; // Use indexOf + substring instead of String.replace() because the // katexInjection string contains '$' characters that .replace() would // interpret as special substitution patterns ($$ → $, $' → post-match text). const headCloseIdx = html.indexOf(''); if (headCloseIdx !== -1) { return ( html.substring(0, headCloseIdx) + katexInjection + '\n' + html.substring(headCloseIdx + 7) ); } // Fallback: inject before if is missing const bodyCloseIdx = html.indexOf(''); if (bodyCloseIdx !== -1) { return ( html.substring(0, bodyCloseIdx) + katexInjection + '\n' + html.substring(bodyCloseIdx + 7) ); } // Last resort: append at end return html + katexInjection; }