fix(web): keep a code block open at an info-bearing inner fence

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 <noreply@anthropic.com>
This commit is contained in:
Rudy Celekli
2026-11-01 09:05:00 -05:00
parent f7aed0744c
commit 1cd621a635
2 changed files with 19 additions and 3 deletions

View File

@@ -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])*?(?<!\s)\$/g;
@@ -112,11 +112,19 @@ export function mapMarkdownProse(
};
for (const line of lines) {
const marker = FENCE_LINE_RE.exec(line)?.[1];
const match = FENCE_LINE_RE.exec(line);
const marker = match?.[1];
if (fence) {
out.push(line);
if (marker && marker[0] === fence[0] && marker.length >= 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;

View File

@@ -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 $
// ---------------------------------------------------------------------------