Skip to content

Commit 5330151

Browse files
Hjalmar Karlsenclaude
authored andcommitted
fix(web): unclosed display math stays text while streaming
A `$$` or `\[` block needs its closing line before it typesets, so a formula arriving token by token no longer flips between KaTeX output and source on every prefix. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent bc37437 commit 5330151

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ describe("chat math parsing", () => {
117117
expect(() => parse(message.slice(0, end))).not.toThrow();
118118
}
119119
expect(text("Energy \\(E = mc")).toBe("Energy (E = mc");
120+
expect(formulas("$$\nx^2")).toEqual([]);
121+
expect(formulas("\\[\nx^2\n- y")).toEqual([]);
120122
expect(formulas(message)).toEqual([
121123
["inline", "E = mc^2"],
122124
["display", "\\sum_i x_i"],

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ const mathBlock: Construct = {
237237
}
238238

239239
function openingRest(code: Code): State | undefined {
240-
if (code === null || isLineEnding(code)) {
240+
// Unlike a code fence, a block needs its closing line, so a formula
241+
// still streaming in stays text instead of typesetting each prefix.
242+
if (code === null) return nok(code);
243+
if (isLineEnding(code)) {
241244
effects.exit("mathFlowFence");
242245
if (interrupt) return ok(code);
243246
return effects.attempt(nonLazyContinuation, lineStart, after)(code);
@@ -252,7 +255,7 @@ const mathBlock: Construct = {
252255
}
253256

254257
function content(code: Code): State | undefined {
255-
if (code === null) return after(code);
258+
if (code === null) return nok(code);
256259
if (isLineEnding(code)) return effects.attempt(nonLazyContinuation, lineStart, after)(code);
257260
effects.enter("mathFlowValue");
258261
return value(code);

0 commit comments

Comments
 (0)