Skip to content

Fix blank lines between list items and nested sub-lists being removed in Markdown/MDX#17857

Merged
fisker merged 17 commits into
prettier:mainfrom
byplayer:fix-17746
Jun 4, 2026
Merged

Fix blank lines between list items and nested sub-lists being removed in Markdown/MDX#17857
fisker merged 17 commits into
prettier:mainfrom
byplayer:fix-17746

Conversation

@byplayer

@byplayer byplayer commented Sep 8, 2025

Copy link
Copy Markdown
Contributor

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

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

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.
@pkg-pr-new

pkg-pr-new Bot commented Sep 8, 2025

Copy link
Copy Markdown

Open in StackBlitz

yarn add https://pkg.pr.new/@prettier/plugin-hermes@17857.tgz
yarn add https://pkg.pr.new/@prettier/plugin-oxc@17857.tgz
yarn add https://pkg.pr.new/prettier@17857.tgz

commit: 0d2063e

@netlify

netlify Bot commented Sep 8, 2025

Copy link
Copy Markdown

Deploy Preview for prettier ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 0186dec
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/68bfce4efd6e09000895a1e4
😎 Deploy Preview https://deploy-preview-17857--prettier.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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.
@byplayer

byplayer commented Sep 9, 2025

Copy link
Copy Markdown
Contributor Author

I think https://github.com/prettier/prettier/actions/runs/17574300842/job/49916187236?pr=17857 will be passed when rerun the task.

@andersk

andersk commented Dec 9, 2025

Copy link
Copy Markdown
Contributor

The merge conflict with #18321 needs to be resolved.

@tats-u

tats-u commented Jan 17, 2026

Copy link
Copy Markdown
Contributor

@byplayer You have never got a review yet and PRs are squashed when merged, so you can either merge main or rebase main. merge is easier because of less conflicts.

byplayer added 3 commits April 8, 2026 16:02
# 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.
@netlify

netlify Bot commented Apr 8, 2026

Copy link
Copy Markdown

Deploy Preview for prettier ready!

Name Link
🔨 Latest commit 0d2063e
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/6a208581b4d8ca000886ef39
😎 Deploy Preview https://deploy-preview-17857--prettier.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@byplayer

byplayer commented Apr 8, 2026

Copy link
Copy Markdown
Contributor Author

The merge conflict with #18321 needs to be resolved.

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.
@tats-u

tats-u commented Apr 8, 2026

Copy link
Copy Markdown
Contributor
  • You have not written a changelog yet
  • I think the title of this PR should be self-contained and more concrete like the other PRs

byplayer added 3 commits April 9, 2026 10:20
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.
@tats-u

tats-u commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

Could you change the title of this PR to that written in your changelog?

@byplayer byplayer changed the title Fix 17746 Fix blank lines between list items and nested sub-lists being removed in Markdown/MDX Fix 17746 Apr 16, 2026
@byplayer byplayer changed the title Fix blank lines between list items and nested sub-lists being removed in Markdown/MDX Fix 17746 Fix blank lines between list items and nested sub-lists being removed in Markdown/MDX Apr 16, 2026
@byplayer

Copy link
Copy Markdown
Contributor Author

Could you change the title of this PR to that written in your changelog?

I updated it.

@tats-u tats-u left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/language-markdown/print/children.js Outdated
Comment thread src/language-markdown/print/children.js Outdated
@fisker

fisker commented Apr 18, 2026

Copy link
Copy Markdown
Member

Two if statements can be merged.

👍

byplayer and others added 2 commits April 20, 2026 09:11
Co-authored-by: Tatsunori Uchino <tats.u@live.jp>
Co-authored-by: Tatsunori Uchino <tats.u@live.jp>
@byplayer

Copy link
Copy Markdown
Contributor Author

I applied two suggestions.
Thank you.

Comment on lines +74 to +77
(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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@tats-u tats-u left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fisker

Comment thread tests/format/markdown/list/issue-17746-fenced-code-then-nested-list.md Outdated
Co-authored-by: Tatsunori Uchino <tats.u@live.jp>
@byplayer byplayer requested a review from tats-u June 4, 2026 06:12
@fisker fisker merged commit 8ea698c into prettier:main Jun 4, 2026
36 of 37 checks passed
fisker added a commit that referenced this pull request Jun 9, 2026
… 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>
@fisker fisker mentioned this pull request Jun 9, 2026
4 tasks
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.

4 participants