From 8723bbeaf8a21a65688787a5f643a4d181244e55 Mon Sep 17 00:00:00 2001 From: one Date: Thu, 26 Jun 2025 15:52:58 +0800 Subject: [PATCH] fix(Markdown): falsely early return for display `\[\n...\n\]` (#7565) --- .../src/utils/__tests__/markdown.test.ts | 32 +++++++++++++++++-- src/renderer/src/utils/markdown.ts | 4 +-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/renderer/src/utils/__tests__/markdown.test.ts b/src/renderer/src/utils/__tests__/markdown.test.ts index 4f48deba0c..f16509ced4 100644 --- a/src/renderer/src/utils/__tests__/markdown.test.ts +++ b/src/renderer/src/utils/__tests__/markdown.test.ts @@ -465,10 +465,28 @@ describe('markdown', () => { describe('processLatexBrackets', () => { describe('basic LaTeX conversion', () => { - it('should convert display math \\[...\\] to $$...$$', () => { + it('should convert (inline) display math \\[...\\] to $$...$$', () => { expect(processLatexBrackets('The formula is \\[a+b=c\\]')).toBe('The formula is $$a+b=c$$') }) + it('should convert display math \\[...\\] to $$...$$', () => { + const input = ` +The formula is + +\\[ +a+b=c +\\] +` + const expected = ` +The formula is + +$$ +a+b=c +$$ +` + expect(processLatexBrackets(input)).toBe(expected) + }) + it('should convert inline math \\(...\\) to $...$', () => { expect(processLatexBrackets('The formula is \\(a+b=c\\)')).toBe('The formula is $a+b=c$') }) @@ -611,9 +629,13 @@ const func = \\(x\\) => x * 2; Read more in [Section \\[3.2\\]: Advanced Topics](url) and see inline code \`\\[array\\]\`. -Final thoughts on \\(\\nabla \\cdot \\vec{F} = \\rho\\) and display math: +Final thoughts on \\(\\nabla \\cdot \\vec{F} = \\rho\\) in inline math and display math: \\[\\int_0^\\infty e^{-x^2} dx = \\frac{\\sqrt{\\pi}}{2}\\] + +\\[ +\\int_0^\\infty e^{-x^2} dx = \\frac{\\sqrt{\\pi}}{2} +\\] ` const expectedOutput = ` @@ -647,9 +669,13 @@ const func = \\(x\\) => x * 2; Read more in [Section \\[3.2\\]: Advanced Topics](url) and see inline code \`\\[array\\]\`. -Final thoughts on $\\nabla \\cdot \\vec{F} = \\rho$ and display math: +Final thoughts on $\\nabla \\cdot \\vec{F} = \\rho$ in inline math and display math: $$\\int_0^\\infty e^{-x^2} dx = \\frac{\\sqrt{\\pi}}{2}$$ + +$$ +\\int_0^\\infty e^{-x^2} dx = \\frac{\\sqrt{\\pi}}{2} +$$ ` expect(processLatexBrackets(complexInput)).toBe(expectedOutput) diff --git a/src/renderer/src/utils/markdown.ts b/src/renderer/src/utils/markdown.ts index 57025ca633..4fc499300d 100644 --- a/src/renderer/src/utils/markdown.ts +++ b/src/renderer/src/utils/markdown.ts @@ -1,5 +1,5 @@ import { languages } from '@shared/config/languages' -import balanced from 'balanced-match' +import { default as balanced } from 'balanced-match' import remarkParse from 'remark-parse' import remarkStringify from 'remark-stringify' import removeMarkdown from 'remove-markdown' @@ -31,7 +31,7 @@ export const findCitationInChildren = (children: any): string => { } // 检查是否包含潜在的 LaTeX 模式 -const containsLatexRegex = /\\\(.*?\\\)|\\\[.*?\\\]|\$.*?\$|\\begin\{equation\}.*?\\end\{equation\}/ +const containsLatexRegex = /\\\(.*?\\\)|\\\[.*?\\\]|\$.*?\$|\\begin\{equation\}.*?\\end\{equation\}/s /** * 转换 LaTeX 公式括号 `\[\]` 和 `\(\)` 为 Markdown 格式 `$$...$$` 和 `$...$`