Skip to content

Commit 684de7e

Browse files
only21milclaude
andcommitted
fix(web): streamed replies keep a list's fenced code block whole when its closing fence is dedented
The streaming splitter closed a fence opened inside a list item on any later fence marker, whatever its indentation. A line left of the item's content ends the item and its code block, so the streamed blocks and the finished reply rendered differently. The splitter now stops cutting at such a line and renders the rest of the reply as one block. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 15fe283 commit 684de7e

2 files changed

Lines changed: 6 additions & 0 deletions

File tree

‎apps/web/src/components/chat/markdownStreamBlocks.test.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ describe("splitMarkdownBlocks", () => {
3131
"| a | b |\n| - | - |\n| 1 | 2 |\n\nAfter the table.",
3232
"para\n\n indented code\n\nnext",
3333
"Setext\n===\n\nnext\n---\n\n- [ ] task\n- [x] done",
34+
"- a\n\n ```\n x\n\n```\n\nb",
35+
"- a\n\n ```\n x\n\ny\n\n ```\n\nb",
3436
])("renders each block as the whole text renders it, at every streamed prefix: %j", (text) => {
3537
for (let end = 0; end <= text.length; end++) {
3638
const source = text.slice(0, end);

‎apps/web/src/components/chat/markdownStreamBlocks.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ export function splitMarkdownBlocks(text: string): string[] | null {
1414
let blockStart = 0;
1515
let lineStart = 0;
1616
let fence: string | null = null;
17+
let fenceIndent = 0;
1718
let afterBlankLine = false;
1819
for (const line of text.split("\n")) {
1920
const opener = FENCE_PATTERN.exec(line)?.[1];
2021
if (fence !== null) {
22+
// A line left of an indented fence may end the list item holding it, which isn't tracked.
23+
if (/\S/.test(line.slice(0, fenceIndent))) break;
2124
if (
2225
opener &&
2326
opener[0] === fence[0] &&
@@ -35,6 +38,7 @@ export function splitMarkdownBlocks(text: string): string[] | null {
3538
}
3639
afterBlankLine = false;
3740
fence = opener ?? null;
41+
if (opener) fenceIndent = line.indexOf(opener);
3842
}
3943
lineStart += line.length + 1;
4044
}

0 commit comments

Comments
 (0)