From e30e9b1622a16a476ec69e5d0fd4979000bc4e2f Mon Sep 17 00:00:00 2001 From: Saifeddine ALOUI Date: Wed, 18 Sep 2024 01:31:02 +0200 Subject: [PATCH] fixed --- .../libraries/lollms_markdown_renderer.js | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/endpoints/libraries/lollms_markdown_renderer.js b/endpoints/libraries/lollms_markdown_renderer.js index a76586b4..48511052 100644 --- a/endpoints/libraries/lollms_markdown_renderer.js +++ b/endpoints/libraries/lollms_markdown_renderer.js @@ -321,14 +321,15 @@ class MarkdownRenderer { return `${code}`; }); } - - handleMathEquations(text) { + + handleLatexEquations(text) { if (typeof katex === 'undefined') { console.error('KaTeX is not loaded. Make sure to include KaTeX scripts and CSS.'); return text; } - return text.replace(/\\\[([\s\S]*?)\\\]|\$\$([\s\S]*?)\$\$|\$([^\n]+?)\$/g, function(match, p1, p2, p3) { + // Function to render a single equation + function renderEquation(match, p1, p2, p3, offset, string) { const equation = p1 || p2 || p3; const isDisplayMode = match.startsWith('\\[') || match.startsWith('$$'); @@ -340,9 +341,18 @@ class MarkdownRenderer { }); } catch (e) { console.error("KaTeX rendering error:", e); - return `${match}`; // Return error-marked original string if rendering fails + return `${match}`; } - }); + } + + // Handle display equations: \[...\] and $$...$$ + text = text.replace(/\\\[([\s\S]*?)\\\]|\$\$([\s\S]*?)\$\$/g, renderEquation); + + // Handle inline equations: \(...\) and $...$ + // Be careful not to match single $ used for currency + text = text.replace(/\\\(([\s\S]*?)\\\)|\$(\S.*?\S|\S)\$/g, renderEquation); + + return text; } @@ -574,7 +584,7 @@ class MarkdownRenderer { text = this.handleInlineCode(text); // Handle LaTeX-style math equations - text = this.handleMathEquations(text); + text = this.handleLatexEquations(text); // Handle tables text = await this.handleTables(text);