Skip to content

Commit 644448d

Browse files
Fix MarkdownRenderer blockquote child spacing
Avoid inserting top-level blank separators between adjacent child blocks when rendering Markdown block quotes, while preserving explicit blank_line tokens inside the quote. Fixes #368 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 75cab78 commit 644448d

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/mistune/renderers/markdown.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def block_quote(self, token: Dict[str, Any], state: BlockState) -> str:
152152
# strip the children's trailing blank lines first so the quote marker is
153153
# not added to a dangling empty line; stripping it back off afterwards
154154
# would also eat a ">" that ends the content (an autolink or HTML tag).
155-
text = self.render_children(token, state).rstrip("\n")
155+
text = _render_block_quote_children(self, token, state).rstrip("\n")
156156
text = indent(text, "> ", lambda _: True)
157157
return text + "\n\n"
158158

@@ -216,6 +216,19 @@ def _escape_title(title: str) -> str:
216216
return title.replace("\\", "\\\\").replace('"', '\\"')
217217

218218

219+
def _render_block_quote_children(renderer: MarkdownRenderer, token: Dict[str, Any], state: BlockState) -> str:
220+
children = token["children"]
221+
parts: list[str] = []
222+
previous_type = ""
223+
for child in children:
224+
child_type = child["type"]
225+
if parts and previous_type != "blank_line" and child_type != "blank_line":
226+
parts[-1] = parts[-1].rstrip("\n") + "\n"
227+
parts.append(renderer.render_token(child, state))
228+
previous_type = child_type
229+
return "".join(parts)
230+
231+
219232
def _escape_block_prefix(text: str) -> str:
220233
"""Backslash-escape a leading block marker on each line so that literal
221234
text is not re-parsed as a list, heading or block quote."""

tests/test_renderers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ def test_block_quote_content_ending_in_gt(self):
112112
):
113113
self.assert_round_trip(text)
114114

115+
def test_block_quote_adjacent_child_blocks(self):
116+
reformat = create_markdown(renderer=MarkdownRenderer())
117+
self.assertEqual(
118+
reformat("> #### Lorem\n> Ipsum"),
119+
"> #### Lorem\n> Ipsum\n",
120+
)
121+
115122
def test_multiline_setext_heading(self):
116123
# a setext heading may span several lines; re-emitting it as an ATX
117124
# heading ("# ...") drops every line after the first out of the

0 commit comments

Comments
 (0)