fix(markdown): Correctly render loose lists with blank lines#3916
Open
anish-devgit wants to merge 1 commit intoTextualize:masterfrom
Open
fix(markdown): Correctly render loose lists with blank lines#3916anish-devgit wants to merge 1 commit intoTextualize:masterfrom
anish-devgit wants to merge 1 commit intoTextualize:masterfrom
Conversation
Updates ListItem to respect the 'hidden' token attribute from markdown-it-py. This ensures that loose lists (items separated by newlines) render with proper spacing, complying with the CommonMark spec, while keeping tight lists compact. Updated regression tests to match the corrected output.
willmcgugan
requested changes
Jan 22, 2026
|
|
||
| def on_child_close(self, context: MarkdownContext, child: MarkdownElement) -> bool: | ||
| self.elements.append(child) | ||
| # FIX: If the child is a visible paragraph (loose list), add a blank line |
Member
There was a problem hiding this comment.
FIX would be superfluous once merged...
| def on_child_close(self, context: MarkdownContext, child: MarkdownElement) -> bool: | ||
| self.elements.append(child) | ||
| # FIX: If the child is a visible paragraph (loose list), add a blank line | ||
| if getattr(child, "visible", False): |
Member
There was a problem hiding this comment.
getattr is often a hack. Why can't we be sure of this attribute?
| self.elements.append(child) | ||
| # FIX: If the child is a visible paragraph (loose list), add a blank line | ||
| if getattr(child, "visible", False): | ||
| # Text(" ") creates exactly one new line of height |
Member
There was a problem hiding this comment.
A little redundant perhaps?
| # FIX: If the child is a visible paragraph (loose list), add a blank line | ||
| if getattr(child, "visible", False): | ||
| # Text(" ") creates exactly one new line of height | ||
| self.elements.append(Text(" ")) |
Member
There was a problem hiding this comment.
What's the intent here? I doubt a single space in the output is desirable...
| return cls(justify=markdown.justify or "left") | ||
| return cls( | ||
| justify=markdown.justify or "left", | ||
| visible=not getattr(token, "hidden", False), |
Member
There was a problem hiding this comment.
Why can't we be sure that the "hidden" attribute exists? Is it not on all Tokens? If not, would an isinstance check allow us to avoid the getattr.
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.
Problem
Previously,
Richrendered all Markdown lists as "tight" lists, even when items were separated by blank lines (loose lists). This squashed list items together, ignoring the intended vertical spacing defined in the CommonMark spec.Solution
I updated the
ListItemclass inrich/markdown.pyto respect thehiddenattribute provided by themarkdown-it-pyparser.ListItem.on_child_closeprocesses a child element, it now checks if that child is explicitly marked as "visible" (not token.hidden).Text(" ")) is appended to the renderable elements.tests/test_markdown.pyandtests/test_markdown_no_hyperlinks.pyto reflect the corrected, spaced-out rendering. This ensures no regressions for standard (tight) lists while fixing the output for loose lists.Example
Input: