From 8ea698ccc260aafb415874df918a9b87552fd407 Mon Sep 17 00:00:00 2001 From: "byplayer(Andy)" Date: Thu, 4 Jun 2026 19:21:03 +0900 Subject: [PATCH] Fix blank lines between list items and nested sub-lists being removed in Markdown/MDX (#17857) Co-authored-by: Tatsunori Uchino Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: fisker Cheung --- changelog_unreleased/markdown/17746.md | 31 ++ src/language-markdown/print/children.js | 10 +- .../__snapshots__/format.test.js.snap | 1 + .../__snapshots__/format.test.js.snap | 1 + .../html/__snapshots__/format.test.js.snap | 3 + .../list/__snapshots__/format.test.js.snap | 519 ++++++++++++++++++ .../list/issue-17746-code-before-list.md | 4 + .../issue-17746-code-sibling-nested-list.md | 7 + ...ssue-17746-fenced-code-then-nested-list.md | 6 + ...ue-17746-indented-code-then-nested-list.md | 3 + tests/format/markdown/list/issue-17746.md | 8 + .../mdx/mdx/__snapshots__/format.test.js.snap | 170 ++++++ .../mdx/mdx/issue-17746-code-before-list.mdx | 4 + ...sue-17746-fenced-code-then-nested-list.mdx | 5 + ...e-17746-indented-code-then-nested-list.mdx | 3 + tests/format/mdx/mdx/issue-17746.mdx | 8 + 16 files changed, 781 insertions(+), 2 deletions(-) create mode 100644 changelog_unreleased/markdown/17746.md create mode 100644 tests/format/markdown/list/issue-17746-code-before-list.md create mode 100644 tests/format/markdown/list/issue-17746-code-sibling-nested-list.md create mode 100644 tests/format/markdown/list/issue-17746-fenced-code-then-nested-list.md create mode 100644 tests/format/markdown/list/issue-17746-indented-code-then-nested-list.md create mode 100644 tests/format/markdown/list/issue-17746.md create mode 100644 tests/format/mdx/mdx/issue-17746-code-before-list.mdx create mode 100644 tests/format/mdx/mdx/issue-17746-fenced-code-then-nested-list.mdx create mode 100644 tests/format/mdx/mdx/issue-17746-indented-code-then-nested-list.mdx create mode 100644 tests/format/mdx/mdx/issue-17746.mdx diff --git a/changelog_unreleased/markdown/17746.md b/changelog_unreleased/markdown/17746.md new file mode 100644 index 000000000..aa7516d72 --- /dev/null +++ b/changelog_unreleased/markdown/17746.md @@ -0,0 +1,31 @@ +#### Fix blank lines between list items and nested sub-lists being removed in Markdown/MDX (#17746 by @byplayer) + +Prettier was removing blank lines between list items and their nested sub-lists, converting loose lists into tight lists and changing their semantic meaning. + + +```markdown + +- a + + - b + +- c + + + - d + + +- a + - b +- c + - d + + +- a + + - b + +- c + + - d +``` diff --git a/src/language-markdown/print/children.js b/src/language-markdown/print/children.js index 20927c5fc..be78f2e95 100644 --- a/src/language-markdown/print/children.js +++ b/src/language-markdown/print/children.js @@ -64,7 +64,10 @@ function shouldPrePrintDoubleHardline(path, options) { isLooseListItemLegacy(previous, options) || (node.type === "list" && parent.type === "listItem" && - previous.type === "code") + (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) ) { return true; } @@ -80,7 +83,10 @@ function shouldPrePrintDoubleHardline(path, options) { isPreviousNodeLooseListItem(path) || (node.type === "list" && parent.type === "listItem" && - previous.type === "code") + (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) ) { return true; } diff --git a/tests/format/markdown/commonmark-test-suite-legacy/__snapshots__/format.test.js.snap b/tests/format/markdown/commonmark-test-suite-legacy/__snapshots__/format.test.js.snap index 7a7225918..1db6092de 100644 --- a/tests/format/markdown/commonmark-test-suite-legacy/__snapshots__/format.test.js.snap +++ b/tests/format/markdown/commonmark-test-suite-legacy/__snapshots__/format.test.js.snap @@ -818,6 +818,7 @@ proseWrap: "always" \`\`\` - baz + - \`\`\` foo diff --git a/tests/format/markdown/commonmark-test-suite/__snapshots__/format.test.js.snap b/tests/format/markdown/commonmark-test-suite/__snapshots__/format.test.js.snap index 35fab0c50..c1aa7636c 100644 --- a/tests/format/markdown/commonmark-test-suite/__snapshots__/format.test.js.snap +++ b/tests/format/markdown/commonmark-test-suite/__snapshots__/format.test.js.snap @@ -1776,6 +1776,7 @@ proseWrap: "always" =====================================output===================================== 1. foo + - bar ================================================================================ diff --git a/tests/format/markdown/html/__snapshots__/format.test.js.snap b/tests/format/markdown/html/__snapshots__/format.test.js.snap index 8f5c218c0..294f58790 100644 --- a/tests/format/markdown/html/__snapshots__/format.test.js.snap +++ b/tests/format/markdown/html/__snapshots__/format.test.js.snap @@ -666,6 +666,7 @@ text - In list + - In list - In list + - In list - In list + - In list