// ================================================================================ // CODE VED - ADVANCED MARKDOWN & MATH RENDERER (markdownRenderer.js) // Engineered by Divy Patel | Modular Architecture // // This module is the "Visual Engine" of the AI. It handles: // 1. Markdown parsing (via marked.js) // 2. Syntax Highlighting (via highlight.js) // 3. Complex Math Formulas (via MathJax) with pre/post-processing to prevent breaking // 4. Mermaid Diagrams rendering // 5. AI "Thinking Process" collapsible UI // 6. Code block copy buttons // ================================================================================ (function() { 'use strict'; // ---------------------------------------------------------------------------- // 1. INITIALIZATION & CONFIGURATION // ---------------------------------------------------------------------------- // Initialize Mermaid (Diagrams) function initMermaid() { if (typeof mermaid !== 'undefined') { mermaid.initialize({ startOnLoad: false, theme: 'default', securityLevel: 'loose', fontFamily: 'Inter, sans-serif' }); } } // Configure Marked.js (Markdown Parser) function initMarked() { if (typeof marked === 'undefined') return; const renderer = new marked.Renderer(); // Custom Code Block Renderer (Handles Syntax Highlighting & Mermaid) renderer.code = function(code, language, isEscaped) { // Handle Mermaid Diagrams if (language === 'mermaid') { return `
${code}`;
}
// Handle Syntax Highlighting
let highlightedCode = code;
const langClass = language ? `language-${language}` : '';
if (language && typeof hljs !== 'undefined' && hljs.getLanguage(language)) {
try {
highlightedCode = hljs.highlight(code, { language: language, ignoreIllegals: true }).value;
} catch (e) {
highlightedCode = hljs.highlightAuto(code).value;
}
} else if (typeof hljs !== 'undefined') {
highlightedCode = hljs.highlightAuto(code).value;
}
// SVG Icons for Copy Button
const copyIcon = ``;
// Return formatted code block with a copy button
return `${highlightedCode}`;
};
// Apply the custom renderer to marked
marked.use({
renderer: renderer,
gfm: true,
breaks: true,
pedantic: false,
mangle: false,
headerIds: false
});
}
// ----------------------------------------------------------------------------
// 2. MATHJAX PRE-PROCESSING & POST-PROCESSING (The Secret Sauce)
// ----------------------------------------------------------------------------
/*
* WHY THIS IS NEEDED:
* Markdown parsers (like marked) often break LaTeX math syntax. For example,
* underscores (_) in math formulas get converted to italics (), and
* asterisks (*) get converted to bold ().
* To prevent this, we temporarily hide math blocks using unique placeholders,
* parse the markdown, and then put the math blocks back!
*/
function preprocessMath(text) {
const mathBlocks = {};
let counter = 0;
function createPlaceholder(match) {
const id = `@@MATHBLOCK_${counter++}@@`;
mathBlocks[id] = match;
return id;
}
// 1. Display Math: $$ ... $$
text = text.replace(/\$\$([\s\S]+?)\$\$/g, createPlaceholder);
// 2. Display Math: \[ ... \]
text = text.replace(/\\\[([\s\S]+?)\\\]/g, createPlaceholder);
// 3. Inline Math: $ ... $ (Ensuring it doesn't match currency or empty spaces)
text = text.replace(/(?thought\s*<\|channel\|>/gi, "