From 1cd621a635a04bbf8d3a416efa0691db6bee954a Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Sun, 1 Nov 2026 09:05:00 -0500 Subject: [PATCH] fix(web): keep a code block open at an info-bearing inner fence MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mapMarkdownProse closed a fenced code block at any later line whose fence marker matched, including lines that carry an info string (```tex inside a ```text block). Per CommonMark a closing fence has no info string, so such a line is code content, not a close. Closing early let the block's remaining content run through prose normalization — e.g. a documented \(x+y\) example became $x+y$ — which is exactly what this helper exists to prevent. Require an empty info string before treating a line as a closing fence, matching the sibling fence trackers in reading-outline.ts and think-segments.ts. Co-Authored-By: Claude Opus 4.8 --- web/lib/latex-normalize.ts | 14 +++++++++++--- web/tests/latex-normalize.test.ts | 8 ++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/web/lib/latex-normalize.ts b/web/lib/latex-normalize.ts index 42c5d7ae8..ff29bf7eb 100644 --- a/web/lib/latex-normalize.ts +++ b/web/lib/latex-normalize.ts @@ -23,7 +23,7 @@ import { isKnownLatexCommand, isMathTriggerCommand } from "./latex-commands"; -const FENCE_LINE_RE = /^ {0,3}(`{3,}|~{3,})/; +const FENCE_LINE_RE = /^ {0,3}(`{3,}|~{3,})(.*)$/; const INLINE_CODE_SPAN_RE = /(`+)([^`\n]+?)\1/g; // `$$...$$` first so a display span is never split into two inline ones. const MATH_SPAN_RE = /\$\$[\s\S]*?\$\$|\$(?!\s)(?:\\.|[^$\n])*?(?= fence.length) { + // A closing fence carries no info string; a same-char line with one + // (```tex inside a ```text block) is code content, not a close. + if ( + marker && + marker[0] === fence[0] && + marker.length >= fence.length && + (match?.[2] ?? "").trim() === "" + ) { fence = null; } continue; diff --git a/web/tests/latex-normalize.test.ts b/web/tests/latex-normalize.test.ts index e11711e78..edfbd26c7 100644 --- a/web/tests/latex-normalize.test.ts +++ b/web/tests/latex-normalize.test.ts @@ -97,6 +97,14 @@ test("fenced code is never rewritten", () => { assert.equal(normalizeMathMarkup(input), input); }); +test("an info-bearing inner fence line does not close the block", () => { + // The inner ```tex line carries an info string, so per CommonMark it is code + // content, not a closing fence. The whole input is one code block and must + // pass through untouched. + const input = "```text\n```tex\n\\(x+y\\)\n```"; + assert.equal(normalizeMathMarkup(input), input); +}); + // --------------------------------------------------------------------------- // delimiters the model chose instead of $ // ---------------------------------------------------------------------------