fix(cli): preserve trailing block separators in streamed markdown#3272
Open
nankingjing wants to merge 1 commit into
Open
fix(cli): preserve trailing block separators in streamed markdown#3272nankingjing wants to merge 1 commit into
nankingjing wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.