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