DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(web): render display math whose $$ hug a multi-line formula (#1623)
Maxia2eb9acd360a
diff --git a/web/lib/latex.ts b/web/lib/latex.tsindex 250ed0865..51a4b5248 100644--- a/web/lib/latex.ts+++ b/web/lib/latex.ts@@ -65,6 +65,26 @@ function looksLikeLatexBlock(lines: string[]): boolean {return block.length > 0 && LIKELY_LATEX_BLOCK_RE.test(block);}+function countFences(line: string): number {+ return line.match(/\$\$/g)?.length ?? 0;+}++/**+ * The line that closes a display block opened above `start`: the next line+ * with a single `$$`, at its end. A blank line or a line that opens another+ * block first means there is nothing to close.+ */+function findDisplayClose(lines: string[], start: number): number {+ for (let j = start; j < lines.length; j++) {+ const trimmed = lines[j].trim();+ if (!trimmed) return -1;+ const fences = countFences(trimmed);+ if (fences === 0) continue;+ return fences === 1 && trimmed.endsWith("$$") ? j : -1;+ }+ return -1;+}+function normalizeEditorMdInlineMath(content: string): string {return mapMarkdownProse(content, normalizeEditorMdInlineMathInProse);}@@ -97,6 +117,26 @@ function normalizeEditorMdInlineMathInProse(content: string): string {}}+ // `$$` hugging a formula that spans several lines, e.g.+ // `$$a = b\\` / `c = d$$` (#1623). remark-math only reads a multi-line+ // display block with each fence on its own line, so move them there.+ if (trimmed.startsWith("$$") && countFences(trimmed) === 1) {+ const endIdx = findDisplayClose(lines, i + 1);+ const first = trimmed.slice(2).trim();+ const last = endIdx === -1 ? "" : lines[endIdx].trim().slice(0, -2).trim();+ if (endIdx !== -1 && (first || last)) {+ result.push("$$");+ if (first) result.push(first);+ for (let j = i + 1; j < endIdx; j++) {+ result.push(lines[j]);+ }+ if (last) result.push(last);+ result.push("$$");+ i = endIdx;+ continue;+ }+ }+if (/^\$\$[\s\S]+\$\$$/.test(trimmed) &&(trimmed.match(/\$\$/g)?.length ?? 0) === 2diff --git a/web/tests/latex.test.ts b/web/tests/latex.test.tsindex 0014f10c4..8b0944715 100644--- a/web/tests/latex.test.ts+++ b/web/tests/latex.test.ts@@ -156,6 +156,37 @@ test("inline math: standalone one-line $$...$$ → split to block", () => {assert.equal(lines[2], "$$");});+test("display math: $$ hugging a multi-line formula → fences on their own lines (#1623)", () => {+ const input = "$$Testing\\ Latex \\rightarrow\n\\leftarrow ENTER\\ HERE$$";+ const lines = processMarkdownContent(input).trim().split("\n");+ assert.deepEqual(lines, [+ "$$",+ "Testing\\ Latex \\rightarrow",+ "\\leftarrow ENTER\\ HERE",+ "$$",+ ]);+});++test("display math: aligned block with the opening fence hugging, closing alone", () => {+ const input = "$$\\begin{aligned}\na &= b \\\\\nc &= d\n\\end{aligned}\n$$";+ const lines = processMarkdownContent(input).trim().split("\n");+ assert.equal(lines[0], "$$");+ assert.equal(lines[1], "\\begin{aligned}");+ assert.equal(lines[lines.length - 2], "\\end{aligned}");+ assert.equal(lines[lines.length - 1], "$$");+});++test("display math: fences already on their own lines are left alone", () => {+ const input = "$$\na = b\nc = d\n$$";+ assert.equal(processMarkdownContent(input).trim(), input);+});++test("display math: an unclosed $$ across a blank line is not rewritten", () => {+ const input = "$$x = 1\n\nprice is 5$ tomorrow";+ const result = processMarkdownContent(input);+ assert.ok(result.includes("$$x = 1"));+});+// ---------------------------------------------------------------------------// processMarkdownContent — loose block math ($...\n...\n$) promotion// ---------------------------------------------------------------------------