Skip to content

fix: preserve loose list spacing in MarkdownRenderer#272

Open
gaoflow wants to merge 1 commit into
frostming:masterfrom
gaoflow:fix-md-renderer-loose-list
Open

fix: preserve loose list spacing in MarkdownRenderer#272
gaoflow wants to merge 1 commit into
frostming:masterfrom
gaoflow:fix-md-renderer-loose-list

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 24, 2026

Copy link
Copy Markdown

Problem

MarkdownRenderer.render_list() always joins rendered list items with
"", ignoring the element.tight attribute. This means a loose
list
(items separated by blank lines, tight=False) is re-emitted as
a tight list, silently changing the document's semantics.

The difference matters: a tight list renders list content as bare text,
while a loose list wraps each item in <p> tags.

import marko
from marko.md_renderer import MarkdownRenderer

md = marko.Markdown(renderer=MarkdownRenderer)

# Before the fix:
md("- item1\n\n- item2\n")
# Returns: '- item1\n- item2\n'  ← blank line lost, semantics changed

Converting the re-rendered output back to HTML gives different HTML:

Input HTML
- item1\n\n- item2\n (loose) <ul><li><p>item1</p></li><li><p>item2</p></li></ul>
- item1\n- item2\n (tight) <ul><li>item1</li><li>item2</li></ul>

Fix

When element.tight is False, join the rendered items with "\n"
(a blank line) instead of "".

Tests

Added test_markdown_renderer_loose_list covering loose unordered
lists, 3-item loose lists, and loose ordered lists — verifying both
exact text round-trip and HTML semantic equivalence.

All 1397 tests pass.

render_list() joined items with "" regardless of element.tight, so a
loose list (items separated by blank lines, tight=False) was re-emitted
as a tight list.  This changes the HTML output: tight items render as
plain text while loose items wrap each item in <p> tags.

Fix: join rendered items with "\n" when tight is False so the blank
line between items is preserved.
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.

1 participant