fix(cli): preserve trailing block separators in streamed markdown - #3272
fix(cli): preserve trailing block separators in streamed markdown#3272nankingjing wants to merge 1 commit into
Conversation
|
Nice edge-case fix — trailing block separators getting stripped during streaming would break markdown rendering. The fix is minimal and correctly scoped. Thanks @nankingjing! |
|
Thanks for the review feedback @1716775457damn! All 6 PRs are green on CI. If you have a moment, could you submit a formal PR review approval (Review changes → Approve) on each? That would let them merge cleanly. Much appreciated! |
|
Approved. trim_end() stripping structural trailing newlines during streaming is a subtle but impactful bug — paragraphs and code blocks bleeding together in the terminal would be confusing. The fix looks correctly scoped to preserve block separators without reintroducing unwanted trailing whitespace. |
|
Already approved. The fix is correctly scoped — markdown_to_ansi_stream preserving block separators without changing the existing trim behavior for one-shot rendering is exactly the right approach. |
Summary
Fixes #3257. During streaming, block-separating newlines were being dropped, causing paragraphs, lists and code blocks to run into the previous line in the terminal (e.g.
...Hello World!1. 打开记事本and...:╭─ python).Root cause
MarkdownStreamState::pushrenders each ready chunk withTerminalRenderer::markdown_to_ansi, which callsrender_markdown→output.trim_end(). That trim strips the structural trailing\n\nthat the parser emits at the end of a paragraph/heading/list/code block. Inrun_turnthe streamed chunk is written straight to the terminal withwrite!(out, "{rendered}")and no separator of its own (seemain.rs), so once the trailing newlines are trimmed the next block is printed flush against the previous one.The final
flushpath legitimately wants the tail trimmed, so only the per-chunkpushpath is wrong.Fix
render_markdown_raw;render_markdownnow trims its result (behavior unchanged for one-shot rendering).markdown_to_ansi_stream, a stream-friendly variant that returns the untrimmed output.MarkdownStreamState::pushnow usesmarkdown_to_ansi_stream, preserving the block separators between chunks.flushis unchanged and still trims the final tail.Because
pulldown-cmarkcollapses runs of blank lines and chunk boundaries fall on blank lines, each block still ends with at most one blank line — no extra blank-line accumulation is introduced.Test
Adds
stream_rendering_preserves_block_separators, which asserts the stream variant keeps trailing newlines (whilemarkdown_to_ansistill trims) and that two consecutive streamed chunks do not stick together.Verification
Verified by reading the source and tracing the parser/stream logic; not compiled or executed in this environment. The change is additive plus a one-line call swap, matches the existing
render_markdown/markdown_to_ansiconvention, and preserves all existing tests.