-
-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(markdown): Correctly render loose lists with blank lines #3916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
fix(markdown): Correctly render loose lists with blank lines #3916
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.
|
|
||
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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(" ")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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: