From 0df4e49f1430dab791788921cbbbde92817438bd Mon Sep 17 00:00:00 2001 From: Bao Nguyen <39545125+giaBaoJS@users.noreply.github.com> Date: Fri, 11 Sep 2026 04:36:16 +0700 Subject: [PATCH] Fix indentation drift for indented code blocks in list items (#19990) Co-authored-by: fisker --- changelog_unreleased/markdown/19647.md | 4 +- src/language-markdown/print/list.js | 8 ++++ .../__snapshots__/format.test.js.snap | 47 +++++++++++++++++++ .../markdown/list/tab-width/format.test.js | 1 + .../list/tab-width/indented-code-block.md | 17 +++++++ 5 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 tests/format/markdown/list/tab-width/__snapshots__/format.test.js.snap create mode 100644 tests/format/markdown/list/tab-width/format.test.js create mode 100644 tests/format/markdown/list/tab-width/indented-code-block.md diff --git a/changelog_unreleased/markdown/19647.md b/changelog_unreleased/markdown/19647.md index 11504e69a..813a730d5 100644 --- a/changelog_unreleased/markdown/19647.md +++ b/changelog_unreleased/markdown/19647.md @@ -1,6 +1,4 @@ -#### Prevent indentation drift in task-list code blocks (#19647 by @Austin1serb) - -Indented code blocks inside task-list items no longer gain four spaces on each formatting pass. +#### Prevent indentation drift in list-item code blocks (#19647, #19990 by @Austin1serb, @giaBaoJS) ```markdown diff --git a/src/language-markdown/print/list.js b/src/language-markdown/print/list.js index cc890304e..eb4c5c673 100644 --- a/src/language-markdown/print/list.js +++ b/src/language-markdown/print/list.js @@ -103,6 +103,10 @@ function printListItem(path, options, print, listPrefix) { return align(" ".repeat(prefix.length), print()); } + if (node.type === "code" && node.isIndented) { + return print(); + } + const alignment = " ".repeat( clamp(options.tabWidth - listPrefix.length, 0, 3), // 4+ will cause indented code block ); @@ -208,6 +212,10 @@ function printListItemLegacy(path, options, print, listPrefix) { return align(" ".repeat(prefix.length), print()); } + if (node.type === "code" && node.isIndented) { + return print(); + } + const alignment = " ".repeat( clamp(options.tabWidth - listPrefix.length, 0, 3), // 4+ will cause indented code block ); diff --git a/tests/format/markdown/list/tab-width/__snapshots__/format.test.js.snap b/tests/format/markdown/list/tab-width/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..4133c06cd --- /dev/null +++ b/tests/format/markdown/list/tab-width/__snapshots__/format.test.js.snap @@ -0,0 +1,47 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`indented-code-block.md - {"tabWidth":4} format 1`] = ` +====================================options===================================== +parsers: ["markdown", "mdx"] +tabWidth: 4 + printWidth: 80 (default) | +=====================================input====================================== +- [x] task item + + indented code + +- unordered item + + indented code + +- outer item + + - nested item + + indented code + +- item with extra code indentation + + indented code + +=====================================output===================================== +- [x] task item + + indented code + +- unordered item + + indented code + +- outer item + + - nested item + + indented code + +- item with extra code indentation + + indented code + +================================================================================ +`; diff --git a/tests/format/markdown/list/tab-width/format.test.js b/tests/format/markdown/list/tab-width/format.test.js new file mode 100644 index 000000000..f4e2cd407 --- /dev/null +++ b/tests/format/markdown/list/tab-width/format.test.js @@ -0,0 +1 @@ +runFormatTest(import.meta, ["markdown", "mdx"], { tabWidth: 4 }); diff --git a/tests/format/markdown/list/tab-width/indented-code-block.md b/tests/format/markdown/list/tab-width/indented-code-block.md new file mode 100644 index 000000000..e2afadbff --- /dev/null +++ b/tests/format/markdown/list/tab-width/indented-code-block.md @@ -0,0 +1,17 @@ +- [x] task item + + indented code + +- unordered item + + indented code + +- outer item + + - nested item + + indented code + +- item with extra code indentation + + indented code