Skip to content

Render streamed Markdown incrementally unless it has a parsed HTML node - #7702

Merged
ymichael merged 2 commits into
get-bb:mainfrom
lanjak:perf/markdown-html-detection
Nov 17, 2026
Merged

ymichael merged 2 commits into
get-bb:mainfrom
lanjak:perf/markdown-html-detection

Conversation

@lanjak

@lanjak lanjak commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

MarkdownPreview turned off incremental rendering when allowHtml was set and the content contained <. Most < in agent output are not HTML (code fences, Array<T>, a < b). For those messages, each completed block caused a full parse of the settled message. That parse also ran rehype-raw and rehype-sanitize. #4343 states the intent "Plain Markdown retains incremental rendering". The < check was a cheap stand-in for "plain".

What changed

  • markdown-incremental-pieces.ts: each piece summary records whether its mdast has an html node (checked only when the piece contains <). resolveMarkdownPieces returns hasHtml.
  • markdown-preview.tsx: with incremental blocks on, the preview uses the sanitized whole-document path only if a piece has a real html node. Without incremental blocks, the old < check stays.
  • Real HTML keeps the whole-document path, because an HTML block can span several pieces.
  • Messages with < only in code now render like messages without <. One visible effect: footnote ids no longer get the second user-content- prefix from the sanitizer (see the comment on Markdown file preview: #fragment links open a new bb tab instead of scrolling to the target #6274).

How you verified

  • New tests in markdown-incremental-pieces.test.ts:
    • < in code spans, fences and prose gives hasHtml: false.
    • <kbd>, a <details> block and an HTML comment give true.
    • A settled HTML piece keeps true while later pieces stream.
  • pnpm exec turbo run typecheck --filter=@bb/app and test --filter=@bb/app -- markdown Conversation: pass. This includes the incremental and cold-HTML differential tests.

Fixes #7695

AGENT GENERATED

Messages with real HTML still render through the sanitized whole-document
path, so resolving incremental pieces for them on every update doubled the
work. The piece cache now remembers the body where HTML was found and skips
piece parsing while the content keeps extending it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit d601706 into get-bb:main Nov 17, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A '<' anywhere in a streamed message turns off incremental Markdown rendering

2 participants