MCPcopy Create free account
hub / github.com/Feather-2/Burner-X / processFormulasEnhanced

Function processFormulasEnhanced

js/processing/markdown_processor_enhanced.js:282–362  ·  view source on GitHub ↗

* Enhanced formula processing with better error handling and context analysis * @param {string} md - Markdown text * @param {Map} protectedContent - Map to store protected content * @param {Object} counterObj - Counter object with 'value' property * @returns {string} Processe

(md, protectedContent, counterObj)

Source from the content-addressed store, hash-verified

280 * @returns {string} Processed markdown with formulas rendered and protected
281 */
282 function processFormulasEnhanced(md, protectedContent, counterObj) {
283 // Normalize math delimiters to avoid regex mismatches and nested '$' leakage
284 function normalizeMathDelimiters(text) {
285 if (typeof text !== 'string' || !text) return text;
286 let s = text;
287 // Convert encoded dollars to literal '$'
288 s = s.replace(/&(?:#0*36|dollar);/gi, '$');
289 // Normalize fullwidth dollar to ASCII
290 s = s.replace(/\uFF04/g, '$');
291 // Remove zero-width and combining marks immediately around '$' so `$̲` → `$`
292 s = s.replace(/\$[\u200B-\u200D\uFEFF\u0300-\u036F]+/g, '$');
293 s = s.replace(/[\u200B-\u200D\uFEFF\u0300-\u036F]+\$/g, '$');
294
295 // **NEW: 修复 OCR 错误转义的 $ 符号**
296 // 1. $\$ ... \$ ,$ → $ ... $ ,(移除尾部的 ,$)
297 s = s.replace(/\$\\\$\s*([^\$]+?)\s*\\\$\s*,\s*\$/g, '$$$1$$ ,');
298
299 // 2. $\$ ... \$$ → $ ... $ (处理末尾多余的 $$,先处理这个避免被后面的规则误处理)
300 s = s.replace(/\$\\\$\s*([^\$]+?)\s*\\\$\$/g, '$$$1$$');
301
302 // 3. $\$ ... \$ → $ ... $
303 s = s.replace(/\$\\\$\s*([^\$]+?)\s*\\\$/g, '$$$1$$');
304
305 // 4. \$...\$ → $...$ (完全转义的内联公式)
306 s = s.replace(/\\\$([^\$\n]+?)\\\$/g, '$$$1$$');
307
308 return s;
309 }
310
311 md = normalizeMathDelimiters(md);
312
313 // Helper function to protect rendered formulas
314 function protectRenderedFormula(renderedHtml) {
315 if (!renderedHtml || typeof renderedHtml !== 'string') return renderedHtml;
316 // 如果渲染结果包含 HTML 标签,保护它
317 if (renderedHtml.includes('<')) {
318 const placeholder = `PBTOKEN${counterObj.value++}Z`;
319 protectedContent.set(placeholder, renderedHtml);
320 return placeholder;
321 }
322 return renderedHtml;
323 }
324
325 // Process block formulas first ($$...$$)
326 md = md.replace(/\$\$([\s\S]*?)\$\$/g, (match, content) => {
327 const rendered = renderFormula(content.trim(), true, match);
328 return protectRenderedFormula(rendered);
329 });
330
331 // Process LaTeX-style block formulas (\[...\])
332 md = md.replace(/\\\[([\s\S]*?)\\\]/g, (match, content) => {
333 const rendered = renderFormula(content.trim(), true, match);
334 return protectRenderedFormula(rendered);
335 });
336
337 // Process inline formulas ($...$)
338 // 支持多行公式,但限制长度防止误匹配
339 md = md.replace(/\$([^\$]{1,2000}?)\$/g, (match, content) => {

Callers 1

renderWithKatexEnhancedFunction · 0.85

Calls 4

protectRenderedFormulaFunction · 0.85
logMethod · 0.80
normalizeMathDelimitersFunction · 0.70
renderFormulaFunction · 0.70

Tested by

no test coverage detected