From 497a2a39cf02a6be9a2b60c7a332f9a7542882b1 Mon Sep 17 00:00:00 2001 From: Willem Jiang Date: Fri, 17 Oct 2025 15:34:43 +0800 Subject: [PATCH] fix:the formual display error after report editing (#627) --- web/src/core/utils/markdown.ts | 34 ++++++++++++++++++++++++++ web/tests/markdown-math-editor.test.ts | 12 +++++++++ 2 files changed, 46 insertions(+) diff --git a/web/src/core/utils/markdown.ts b/web/src/core/utils/markdown.ts index 50add92..04888d7 100644 --- a/web/src/core/utils/markdown.ts +++ b/web/src/core/utils/markdown.ts @@ -21,6 +21,23 @@ export function normalizeMathForEditor(markdown: string): string { .replace(/\\\\\(([^)]*)\\\\\)/g, (_match, content) => `$${content}$`) // \\(...\\) → $...$ .replace(/\\\(([^)]*)\\\)/g, (_match, content) => `$${content}$`); // \(...\) → $...$ + // Replace double backslashes with single in math contexts + // For inline math: $...$ + normalized = normalized.replace( + /\$([^\$]+?)\$/g, + (match, mathContent) => { + return `$${mathContent.replace(/\\\\/g, '\\')}$`; + } + ); + + // For display math: $$...$$ + normalized = normalized.replace( + /\$\$([\s\S]+?)\$\$/g, + (match, mathContent) => { + return `$$${mathContent.replace(/\\\\/g, '\\')}$$`; + } + ); + return normalized; } @@ -41,6 +58,23 @@ export function normalizeMathForDisplay(markdown: string): string { .replace(/\\\\\(([^)]*)\\\\\)/g, (_match, content) => `$$${content}$$`) // \\(...\\) → $$...$$ .replace(/\\\(([^)]*)\\\)/g, (_match, content) => `$$${content}$$`); // \(...\) → $$...$$ + // Replace double backslashes with single in math contexts + // For inline math: $...$ + normalized = normalized.replace( + /\$([^\$]+?)\$/g, + (match, mathContent) => { + return `$${mathContent.replace(/\\\\/g, '\\')}$`; + } + ); + + // For display math: $$...$$ + normalized = normalized.replace( + /\$\$([\s\S]+?)\$\$/g, + (match, mathContent) => { + return `$$${mathContent.replace(/\\\\/g, '\\')}$$`; + } + ); + return normalized; } diff --git a/web/tests/markdown-math-editor.test.ts b/web/tests/markdown-math-editor.test.ts index 372eee4..ccdf992 100644 --- a/web/tests/markdown-math-editor.test.ts +++ b/web/tests/markdown-math-editor.test.ts @@ -10,6 +10,18 @@ describe("markdown math normalization for editor", () => { assert.strictEqual(output, "Here is a formula $$E=mc^2$$ in the text."); }); + it("converts LaTeX display delimiters to $ with \\ for editor", () => { + const input = "Here is a formula \\(F = k\\frac{q_1q_2}{r^2}\\) in the text."; + const output = normalizeMathForEditor(input); + assert.strictEqual(output, "Here is a formula $F = k\\frac{q_1q_2}{r^2}$ in the text."); + }); + + it("converts LaTeX display delimiters to $ with \\\\ for editor", () => { + const input = "Here is a formula \\(F = k\\\\frac{q_1q_2}{r^2}\\) in the text."; + const output = normalizeMathForEditor(input); + assert.strictEqual(output, "Here is a formula $F = k\\frac{q_1q_2}{r^2}$ in the text."); + }); + it("converts escaped LaTeX display delimiters to $$ for editor", () => { const input = "Formula \\\\[x^2 + y^2 = z^2\\\\] here."; const output = normalizeMathForEditor(input);