* Process custom syntax (subscripts, superscripts) with enhanced robustness * @param {string} md - Markdown text * @returns {string} Processed markdown
(md)
| 117 | * @returns {string} Processed markdown |
| 118 | */ |
| 119 | function processCustomSyntax(md) { |
| 120 | // Enhanced regex patterns with better boundary detection |
| 121 | const patterns = [ |
| 122 | // Base with superscript: ${base}^{sup}$ |
| 123 | { |
| 124 | regex: /\$\{\s*([^}]*?)\s*\}\^\{([^}]*?)\}\$/g, |
| 125 | replacement: (_, base, sup) => { |
| 126 | const cleanBase = (base || '').trim(); |
| 127 | const cleanSup = (sup || '').trim(); |
| 128 | return cleanBase ? |
| 129 | `<span>${escapeHtml(cleanBase)}<sup>${escapeHtml(cleanSup)}</sup></span>` : |
| 130 | `<sup>${escapeHtml(cleanSup)}</sup>`; |
| 131 | } |
| 132 | }, |
| 133 | // Base with subscript: ${base}_{sub}$ |
| 134 | { |
| 135 | regex: /\$\{\s*([^}]*?)\s*\}_\{([^}]*?)\}\$/g, |
| 136 | replacement: (_, base, sub) => { |
| 137 | const cleanBase = (base || '').trim(); |
| 138 | const cleanSub = (sub || '').trim(); |
| 139 | return cleanBase ? |
| 140 | `<span>${escapeHtml(cleanBase)}<sub>${escapeHtml(cleanSub)}</sub></span>` : |
| 141 | `<sub>${escapeHtml(cleanSub)}</sub>`; |
| 142 | } |
| 143 | }, |
| 144 | // Empty base superscript: ${}^{sup}$ |
| 145 | { |
| 146 | regex: /\$\{\s*\}\^\{([^}]*?)\}\$/g, |
| 147 | replacement: (_, sup) => `<sup>${escapeHtml((sup || '').trim())}</sup>` |
| 148 | }, |
| 149 | // Empty base subscript: ${}_{sub}$ |
| 150 | { |
| 151 | regex: /\$\{\s*\}_\{([^}]*?)\}\$/g, |
| 152 | replacement: (_, sub) => `<sub>${escapeHtml((sub || '').trim())}</sub>` |
| 153 | }, |
| 154 | // Simple superscript: ${content}$ |
| 155 | { |
| 156 | regex: /\$\{\s*([^}]*?)\s*\}\$/g, |
| 157 | replacement: (_, content) => `<sup>${escapeHtml((content || '').trim())}</sup>` |
| 158 | } |
| 159 | ]; |
| 160 | |
| 161 | patterns.forEach(({ regex, replacement }) => { |
| 162 | try { |
| 163 | md = md.replace(regex, replacement); |
| 164 | } catch (error) { |
| 165 | console.warn(`[MarkdownProcessorEnhanced] Custom syntax processing error:`, error); |
| 166 | } |
| 167 | }); |
| 168 | |
| 169 | return md; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Enhanced KaTeX rendering with improved error handling and formula analysis |
no test coverage detected