Skip to content

Commit f1804f5

Browse files
TheAnimatrixclaude
andcommitted
fix(web): run an unclosed display block to the end of its container
An unclosed `\[` or `$$` block failed after consuming later lines, which flattened the lists and quotes that followed. Like a fenced code block, it now ends at its closing line or the end of its container, and an unclosed block is shown as source instead of typeset. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent bcee3e5 commit f1804f5

4 files changed

Lines changed: 148 additions & 56 deletions

File tree

‎apps/web/src/components/ChatMarkdown.test.tsx‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,28 @@ describe("ChatMarkdown math", () => {
11811181
}
11821182
});
11831183

1184+
it("shows an unfinished display formula as source until its closing line arrives", async () => {
1185+
settingsOverrides.mathRenderingEnabled = true;
1186+
const unfinished = "Intro\n\n$$\nx = 1";
1187+
const streaming = await renderMath(unfinished);
1188+
try {
1189+
expect(streaming.container.querySelector(".katex")).toBeNull();
1190+
expect(streaming.container.querySelector("[data-markdown-math]")).toBeNull();
1191+
const block = streaming.container.querySelector("pre");
1192+
expect(block?.textContent).toContain("$$");
1193+
expect(block?.textContent).toContain("x = 1");
1194+
} finally {
1195+
await streaming.unmount();
1196+
}
1197+
const finished = await renderMath(`${unfinished}\n$$`);
1198+
try {
1199+
expect(finished.tex(".katex-display")).toEqual(["x = 1"]);
1200+
} finally {
1201+
await finished.unmount();
1202+
delete settingsOverrides.mathRenderingEnabled;
1203+
}
1204+
});
1205+
11841206
it("shows the source of TeX that KaTeX rejects and copies formulas as TeX", async () => {
11851207
settingsOverrides.mathRenderingEnabled = true;
11861208
const view = await renderMath("Good \\(x^2\\), bad \\(\\frac{1\\).");

‎apps/web/src/markdown-clipboard.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ export function serializeTableElementToMarkdown(table: Element): string {
350350
// the attribute from authored HTML.
351351
const MATH_SELECTOR = "[data-markdown-math]";
352352

353-
/** The formula wrapper at or above `element`, if any. */
354353
export function mathWrapperOf(element: Element | null): Element | null {
355354
return element?.closest(MATH_SELECTOR) ?? null;
356355
}

‎apps/web/src/markdown-math.test.ts‎

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,32 @@ import { texMathFromMarkdown, texMathSyntax } from "./markdown-math";
55

66
type Node = { type: string; value?: string; children?: Node[] };
77

8-
/** Math nodes in document order, as `type:value`. */
9-
function mathIn(source: string): string[] {
10-
const found: string[] = [];
8+
function parse(source: string): Node {
9+
return fromMarkdown(source, {
10+
extensions: [texMathSyntax],
11+
mdastExtensions: [texMathFromMarkdown],
12+
}) as Node;
13+
}
14+
15+
function nodesIn(source: string, ...types: string[]): Node[] {
16+
const found: Node[] = [];
1117
const visit = (node: Node) => {
12-
if (node.type === "inlineMath" || node.type === "math")
13-
found.push(`${node.type}:${node.value}`);
18+
if (types.includes(node.type)) found.push(node);
1419
node.children?.forEach(visit);
1520
};
16-
visit(
17-
fromMarkdown(source, {
18-
extensions: [texMathSyntax],
19-
mdastExtensions: [texMathFromMarkdown],
20-
}) as Node,
21-
);
21+
visit(parse(source));
2222
return found;
2323
}
2424

25+
/** Math nodes in document order, as `type:value`. */
26+
const mathIn = (source: string) =>
27+
nodesIn(source, "inlineMath", "math").map((node) => `${node.type}:${node.value}`);
28+
29+
/** Values of the code nodes in document order. */
30+
const codeIn = (source: string) => nodesIn(source, "code").map((node) => node.value);
31+
32+
const typesOf = (node: Node) => node.children?.map((child) => child.type);
33+
2534
describe("TeX math syntax", () => {
2635
it("reads \\(…\\) inline, including TeX backslashes and underscores", () => {
2736
expect(mathIn("so \\(\\sigma\\propto r_1T^{-5}\\) holds")).toEqual([
@@ -66,11 +75,6 @@ describe("TeX math syntax", () => {
6675
["currency", "Costs $5 and $10"],
6776
["a single $", "$\nx\n$"],
6877
["a citation", "see \\[1\\] and \\[2\\]"],
69-
["an unclosed display block", "\\[\nx = 1\n"],
70-
["an unclosed $$ block", "$$\nx = 1\n"],
71-
["a $$ block closed by \\]", "$$\na\n\\]"],
72-
["a \\[ block closed by $$", "\\[\na\n$$"],
73-
["a display block cut by a lazy line", "> \\[\n> x\nlazy\n> \\]"],
7478
["a link target", "[a](https://x.test/\\(b\\))"],
7579
])("leaves %s alone", (_, source) => {
7680
expect(mathIn(source)).toEqual([]);
@@ -87,8 +91,51 @@ describe("TeX math syntax", () => {
8791
expect(performance.now() - start).toBeLessThan(2000);
8892
});
8993

90-
it("ends an unfinished display block at the next opener", () => {
91-
expect(mathIn("\\[\na\n\\[\nb\n\\]")).toEqual(["math:b"]);
94+
it("keeps an opener line inside a display block as formula content", () => {
95+
expect(mathIn("\\[\na\n\\[\nb\n\\]")).toEqual(["math:a\n\\[\nb"]);
96+
});
97+
98+
it.each([
99+
["an unclosed \\[ block", "\\[\nx = 1\n", "\\[\nx = 1"],
100+
["an unclosed $$ block", "$$\nx = 1\n", "$$\nx = 1"],
101+
["a $$ block closed by \\]", "$$\na\n\\]", "$$\na\n\\]"],
102+
["a \\[ block closed by $$", "\\[\na\n$$", "\\[\na\n$$"],
103+
["a display block cut by a lazy line", "> \\[\n> x\nlazy\n> \\]", "\\[\nx"],
104+
["a display opener at the end", "text\n\n$$", "$$"],
105+
])("shows %s as source in a code node", (_, source, code) => {
106+
expect(mathIn(source)).toEqual([]);
107+
expect(codeIn(source)).toEqual([code]);
108+
});
109+
110+
it.each([
111+
["\\[", "\\]"],
112+
["$$", "$$"],
113+
])("keeps the structure after a %s block", (open, close) => {
114+
const closed = parse(`Intro\n${open}\nx\n${close}\n\n- a\n- b\n\n> quote`);
115+
expect(typesOf(closed)).toEqual(["paragraph", "math", "list", "blockquote"]);
116+
});
117+
118+
it.each(["\\[", "$$"])("runs an unclosed %s block to the end of the document", (open) => {
119+
const source = `Intro\n${open}\n\n- a\n- b\n\n> quote`;
120+
expect(typesOf(parse(source))).toEqual(["paragraph", "code"]);
121+
expect(codeIn(source)).toEqual([`${open}\n\n- a\n- b\n\n> quote`]);
122+
expect(codeIn(`Intro\n${open}\nx`)).toEqual([`${open}\nx`]);
123+
});
124+
125+
it("ends an unclosed block with its list item", () => {
126+
const [list, quote] = parse("- item\n $$\n x\n- next\n\n> quote").children!;
127+
expect(typesOf(list!)).toEqual(["listItem", "listItem"]);
128+
expect(typesOf(list!.children![0]!)).toEqual(["paragraph", "code"]);
129+
expect(list!.children![0]!.children![1]!.value).toBe("$$\nx");
130+
expect(typesOf(list!.children![1]!)).toEqual(["paragraph"]);
131+
expect(quote?.type).toBe("blockquote");
132+
});
133+
134+
it("ends an unclosed block with its blockquote", () => {
135+
const tree = parse("> $$\n> x\n\nafter");
136+
expect(typesOf(tree)).toEqual(["blockquote", "paragraph"]);
137+
expect(typesOf(tree.children![0]!)).toEqual(["code"]);
138+
expect(codeIn("> $$\n> x\n\nafter")).toEqual(["$$\nx"]);
92139
});
93140

94141
it("scans a message of unmatched display openers in linear time", () => {

‎apps/web/src/markdown-math.ts‎

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ declare module "micromark-util-types" {
1616
texMathTextData: "texMathTextData";
1717
texMathFlow: "texMathFlow";
1818
texMathFlowFence: "texMathFlowFence";
19+
texMathFlowClosingFence: "texMathFlowClosingFence";
1920
texMathFlowValue: "texMathFlowValue";
2021
}
2122
}
2223

24+
declare module "mdast-util-from-markdown" {
25+
interface CompileData {
26+
texMathFlowClosed?: boolean | undefined;
27+
}
28+
}
29+
2330
// \(…\) inline, and \[ / \] or $$ on their own lines for display. These run before
2431
// CommonMark's escape construct, which would otherwise consume \( and \[. A
2532
// one-line \[x\] stays prose so \[1\] citations do.
@@ -122,28 +129,30 @@ const texMathText: Construct = {
122129
type Marker = readonly [first: number, second: number];
123130

124131
/**
125-
* A line holding only the `open` marker, up to one holding only the `close`
126-
* marker. Like a code fence, it must close; an unfinished formula (a reply still
127-
* streaming) stays prose.
132+
* A line holding only the `open` marker, then lines up to one holding only the
133+
* `close` marker. Like a fenced code block, it runs to its closing line or the
134+
* end of its container; an unclosed block is shown as source.
128135
*/
129136
const tokenizeTexMathFlow = (open: Marker, close: Marker): Tokenizer =>
130137
function (effects, ok, nok) {
138+
const interrupting = this.interrupt;
131139
const nonLazyLine: Construct = {
132140
partial: true,
133141
tokenize(this: TokenizeContext, effects, ok, nok) {
134142
return (code) => {
143+
if (code === null) return nok(code);
135144
effects.enter("lineEnding");
136145
effects.consume(code);
137146
effects.exit("lineEnding");
138147
return (next) => (this.parser.lazy[this.now().line] ? nok(next) : ok(next));
139148
};
140149
},
141150
};
142-
const fenceLine = (marker: Marker): Construct => ({
151+
const closingFence: Construct = {
143152
partial: true,
144153
tokenize(effects, ok, nok) {
145154
return function start(code) {
146-
effects.enter("texMathFlowFence");
155+
effects.enter("texMathFlowClosingFence");
147156
return indent(code);
148157
};
149158

@@ -152,10 +161,10 @@ const tokenizeTexMathFlow = (open: Marker, close: Marker): Tokenizer =>
152161
effects.consume(code);
153162
return indent;
154163
}
155-
if (code !== marker[0]) return nok(code);
164+
if (code !== close[0]) return nok(code);
156165
effects.consume(code);
157166
return function second(code) {
158-
if (code !== marker[1]) return nok(code);
167+
if (code !== close[1]) return nok(code);
159168
effects.consume(code);
160169
return fenceEnd;
161170
};
@@ -167,13 +176,11 @@ const tokenizeTexMathFlow = (open: Marker, close: Marker): Tokenizer =>
167176
return fenceEnd;
168177
}
169178
if (code !== null && !isLineEnding(code)) return nok(code);
170-
effects.exit("texMathFlowFence");
179+
effects.exit("texMathFlowClosingFence");
171180
return ok(code);
172181
}
173182
},
174-
});
175-
const closingFence = fenceLine(close);
176-
const openingFence = fenceLine(open);
183+
};
177184

178185
return function start(code) {
179186
effects.enter("texMathFlow");
@@ -186,37 +193,36 @@ const tokenizeTexMathFlow = (open: Marker, close: Marker): Tokenizer =>
186193
};
187194
};
188195

196+
// Only the opening line can fail. Past it the block ends in `ok`, closed or
197+
// not. Checking whether the line interrupts a paragraph stops there too.
189198
function openEnd(code: Code): State | undefined {
190199
if (isSpace(code)) {
191200
effects.consume(code);
192201
return openEnd;
193202
}
194-
if (!isLineEnding(code)) return nok(code);
203+
if (code !== null && !isLineEnding(code)) return nok(code);
195204
effects.exit("texMathFlowFence");
196-
return effects.attempt(nonLazyLine, lineStart, nok)(code);
205+
return interrupting ? ok(code) : lineEnd(code);
197206
}
198207

199-
function lineStart(code: Code): State | undefined {
200-
return effects.attempt(closingFence, finish, notOpening)(code);
208+
function lineEnd(code: Code): State | undefined {
209+
return effects.attempt(nonLazyLine, lineStart, finish)(code);
201210
}
202211

203-
// Another opener cannot be inside a formula. Giving up at it keeps a run of
204-
// unclosed openers linear.
205-
function notOpening(code: Code): State | undefined {
206-
return effects.attempt(openingFence, nok, content)(code);
212+
function lineStart(code: Code): State | undefined {
213+
return effects.attempt(closingFence, finish, content)(code);
207214
}
208215

209216
function content(code: Code): State | undefined {
210-
if (code === null) return nok(code);
211-
if (isLineEnding(code)) return effects.attempt(nonLazyLine, lineStart, nok)(code);
217+
if (code === null || isLineEnding(code)) return lineEnd(code);
212218
effects.enter("texMathFlowValue");
213219
return value(code);
214220
}
215221

216222
function value(code: Code): State | undefined {
217223
if (code === null || isLineEnding(code)) {
218224
effects.exit("texMathFlowValue");
219-
return content(code);
225+
return lineEnd(code);
220226
}
221227
effects.consume(code);
222228
return value;
@@ -250,8 +256,7 @@ export const texMathFromMarkdown: FromMarkdownExtension = {
250256
this.enter({ type: "inlineMath", value: "", data: { hName: "code" } } as never, token);
251257
this.buffer();
252258
},
253-
texMathFlow(token) {
254-
this.enter({ type: "math", value: "", data: { hName: "pre" } } as never, token);
259+
texMathFlow() {
255260
this.buffer();
256261
},
257262
},
@@ -279,29 +284,48 @@ export const texMathFromMarkdown: FromMarkdownExtension = {
279284
node.data.hChildren = [{ type: "text", value }];
280285
this.exit(token);
281286
},
287+
texMathFlowClosingFence() {
288+
this.data.texMathFlowClosed = true;
289+
},
282290
texMathFlow(token) {
291+
const closed = this.data.texMathFlowClosed;
292+
this.data.texMathFlowClosed = undefined;
283293
const value = this.resume()
284294
.replace(/\r\n?/g, "\n")
285295
.replace(/^\n|\n$/g, "");
286-
const node = this.stack.at(-1) as unknown as {
287-
value: string;
288-
data: Record<string, unknown>;
289-
};
290-
node.value = value;
291-
node.data.hChildren = [
296+
if (!closed) {
297+
// Not a formula: a plain code node that keeps its opener line.
298+
const opener = this.sliceSerialize(token).slice(0, 2);
299+
this.enter(
300+
{ type: "code", lang: null, meta: null, value: value ? `${opener}\n${value}` : opener },
301+
token,
302+
);
303+
this.exit(token);
304+
return;
305+
}
306+
this.enter(
292307
{
293-
type: "element",
294-
tagName: "code",
295-
properties: { className: ["language-math-display"] },
296-
children: [{ type: "text", value }],
297-
},
298-
];
308+
type: "math",
309+
value,
310+
data: {
311+
hName: "pre",
312+
hChildren: [
313+
{
314+
type: "element",
315+
tagName: "code",
316+
properties: { className: ["language-math-display"] },
317+
children: [{ type: "text", value }],
318+
},
319+
],
320+
},
321+
} as never,
322+
token,
323+
);
299324
this.exit(token);
300325
},
301326
},
302327
};
303328

304-
/** unified plugin: registers both extensions on the remark-parse processor. */
305329
export const remarkTexMath = function (this: Processor) {
306330
const data = this.data() as {
307331
micromarkExtensions?: MicromarkExtension[];

0 commit comments

Comments
 (0)