Fix blank lines between list items and nested sub-lists being removed in Markdown/MDX#17857
Conversation
Fixed an issue where prettier was removing blank lines between list items and nested sublists in Markdown, which could change the semantic meaning of the content. The fix detects original blank lines in the source and preserves them when they occur before nested lists within list items. This addresses cases where users intentionally use blank lines to create visual separation or semantic grouping in their markdown lists. Fixes prettier#17746
Add changelog entry for the fix that preserves blank lines in nested markdown lists, addressing issue prettier#17746. This changelog documents the bug fix that ensures markdown formatting maintains proper spacing in nested list structures, improving output consistency with markdown standards.
commit: |
✅ Deploy Preview for prettier ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
Why is this change needed? The test snapshots need to be updated to reflect the fix for issue prettier#17746, which preserves blank lines in nested markdown lists. The snapshots now correctly show blank lines between numbered list items and their nested sub-items. How does it address the issue? Updates the Jest snapshot file to match the corrected formatting behavior where blank lines are preserved in nested list structures, ensuring tests pass with the new formatting rules. What effects does the patch have? Test snapshots now accurately represent the expected markdown formatting output, ensuring that the markdown parser correctly maintains blank lines in nested list structures as intended by the fix.
|
I think https://github.com/prettier/prettier/actions/runs/17574300842/job/49916187236?pr=17857 will be passed when rerun the task. |
|
The merge conflict with #18321 needs to be resolved. |
|
@byplayer You have never got a review yet and PRs are squashed when merged, so you can either |
# Conflicts: # src/language-markdown/print/mdast.js # tests/format/markdown/commonmark-test-suite/__snapshots__/format.test.js.snap
After merging main's refactoring (which split printer-markdown.js into separate files), the blank line preservation logic for issue prettier#17746 was re-applied to its new location in children.js. # Why is this change needed? Main's refactoring moved shouldPrePrintDoubleHardline to children.js. The fix that preserves blank lines before nested lists within list items was not carried over in the refactoring, causing the behavior to regress. # How does it address the issue? Added a check in shouldPrePrintDoubleHardline (non-MDX path) to return true when there is a blank line in the original source between a paragraph and a nested list within a listItem.
# Why is this change needed? When a listItem contained an indented code block immediately followed by a nested list (no blank line in original), prettier was incorrectly inserting a blank line before the nested list, converting a tight list to a loose one and changing the rendered output. # How does it address the issue? Added a blank line presence check (previous.position.end.line + 1 < node.position.start.line) to the "code before list" branch in shouldPrePrintDoubleHardline so it only preserves blank lines that actually existed in the original source.
✅ Deploy Preview for prettier ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
resolved |
The same loose-list bug (prettier#17746) existed in the MDX parser path. The `shouldPrePrintDoubleHardline` function had a `paragraph → list` blank-line check added to the non-MDX branch but the MDX branch was missed. Added the equivalent check to the MDX branch, a new MDX test fixture (issue-17746.mdx), and updated the changelog to mention MDX.
|
The MDX branch of `shouldPrePrintDoubleHardline` was inserting a blank line before a nested list after a code node even when no blank line existed in the source. Added the same `position.end.line + 1 < position.start.line` guard already present in the non-MDX branch. Added `issue-17746-code-before-list.mdx` as a regression test to confirm tight code→list stays tight in the MDX parser path.
Make the changelog title more concrete and self-contained. Add a description and a code example showing the before/after behavior of the blank-line preservation fix for loose lists with nested sublists in Markdown/MDX.
Replace "sublists" with "sub-lists" in the changelog entry to pass the cSpell spellcheck lint.
|
Could you change the title of this PR to that written in your changelog? |
I updated it. |
There was a problem hiding this comment.
Two if statements can be merged.
@fisker Which do you prefer or think is easier to be maintained?
I've tested it somewhat in the playground and this code appears to me to have no logical errors or overlooked considerations.
👍 |
Co-authored-by: Tatsunori Uchino <tats.u@live.jp>
Co-authored-by: Tatsunori Uchino <tats.u@live.jp>
|
I applied two suggestions. |
| (previous.type === "code" || | ||
| // Preserve blank line before nested list within listItem (issue #17746) | ||
| previous.type === "paragraph") && | ||
| previous.position.end.line + 1 < node.position.start.line) |
There was a problem hiding this comment.
Do you have an idea to test the path previous.type === "code" && previous.position.end.line + 1 < node.position.start.line? No newly added testcases contain "code" (code blocks).
There was a problem hiding this comment.
I found one:
- a
```
b
```
- c
- dThere was a problem hiding this comment.
Thank you for your comment.
I added test case.
…#17746 # Why is this change needed? The `previous.type === "code" && previous.position.end.line + 1 < node.position.start.line` branch in `shouldPrePrintDoubleHardline` (both the markdown and MDX paths) had no dedicated fixture. Prior snapshots only exercised the tight (no-blank) negative path via `issue-17746-code-before-list`, leaving the positive "preserve the blank line" path untested for code blocks. A PR reviewer on prettier#17857 pointed this out and suggested a repro. # How does it address the issue? Added fixtures that place a code block followed by a blank line and a nested list inside the same listItem, plus the reviewer's mixed case (sibling listItem with code + paragraph/nested-list with blank line). Both fenced and indented code variants are covered, in both markdown and MDX parsers. - tests/format/markdown/list/issue-17746-code-sibling-nested-list.md - tests/format/markdown/list/issue-17746-fenced-code-then-nested-list.md - tests/format/markdown/list/issue-17746-indented-code-then-nested-list.md - tests/format/mdx/mdx/issue-17746-fenced-code-then-nested-list.mdx - tests/format/mdx/mdx/issue-17746-indented-code-then-nested-list.mdx Refs: prettier#17746, prettier#17857
Co-authored-by: Tatsunori Uchino <tats.u@live.jp>
… in Markdown/MDX (#17857) Co-authored-by: Tatsunori Uchino <tats.u@live.jp> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: fisker Cheung <lionkay@gmail.com>
Description
This is a bug fix of issue #17746.
Preserve blank lines in nested lists
Fixed an issue where prettier was removing blank lines between list items
and nested sublists in Markdown, which could change the semantic meaning
of the content. The fix detects original blank lines in the source and
preserves them when they occur before nested lists within list items.
This addresses cases where users intentionally use blank lines to create
visual separation or semantic grouping in their markdown lists.
Checklist
docs/directory).changelog_unreleased/*/XXXX.mdfile followingchangelog_unreleased/TEMPLATE.md.