diff --git a/changelog_unreleased/markdown/19351.md b/changelog_unreleased/markdown/19351.md new file mode 100644 index 000000000..8341bf584 --- /dev/null +++ b/changelog_unreleased/markdown/19351.md @@ -0,0 +1,32 @@ +#### Cap ordered list mark at 999,999,999 (#19351 by @tats-u) + +CommonMark parsers only support ordered list item numbers up to 999,999,999. + +With this change, Prettier now caps the ordered list item number at 999,999,999 to ensure that the output is correctly parsed as an ordered list by CommonMark parsers. Numbers larger than 999,999,999 are not parsed as list item numbers and are left unchanged in the output: + + +```markdown + +999999998. text +999999998. text +999999998. text +999999998. text + +1234567890123456789012) text + + +999999998. text +999999999. text +1000000000. text +1000000001. text + +1234567890123456789012) text + + +999999998. text +999999999. text +999999999. text +999999999. text + +1234567890123456789012) text +``` diff --git a/src/language-markdown/print/list.js b/src/language-markdown/print/list.js index 1a56bff36..911325b6f 100644 --- a/src/language-markdown/print/list.js +++ b/src/language-markdown/print/list.js @@ -10,6 +10,10 @@ import { printChildren } from "./children.js"; * @import {Doc} from "../../document/index.js" */ +// 999,999,999 is the maximum number that can be parsed as a list item number as intended in CommonMark +// https://spec.commonmark.org/0.31.2/#ordered-list-marker +const MAXIMUM_ORDERED_LIST_MARKER = 999_999_999; + /** * @param {AstPath} path * @param {*} options @@ -55,8 +59,10 @@ function printList(path, options, print) { ? node.start : isGitDiffFriendlyOrderedList ? 1 - : node.start + path.index) + - (nthSiblingIndex % 2 === 0 ? ". " : ") ") + : Math.min( + node.start + path.index, + MAXIMUM_ORDERED_LIST_MARKER, + )) + (nthSiblingIndex % 2 === 0 ? ". " : ") ") : nthSiblingIndex % 2 === 0 ? "- " : "* "; diff --git a/tests/format/markdown/list/huge-first-number/__snapshots__/format.test.js.snap b/tests/format/markdown/list/huge-first-number/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..0ab60ba04 --- /dev/null +++ b/tests/format/markdown/list/huge-first-number/__snapshots__/format.test.js.snap @@ -0,0 +1,48 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`huge-first-number.md format 1`] = ` +====================================options===================================== +parsers: ["markdown"] + printWidth: 80 (default) | +=====================================input====================================== +999999998. text +999999998. text +999999998. text +999999998. text + +--- + +999999998. text +1. text +1. text +1. text + +--- + +999999998. text +1. text +2. text +3. text + +=====================================output===================================== +999999998. text +999999999. text +999999999. text +999999999. text + +--- + +999999998. text +1. text +1. text +1. text + +--- + +999999998. text +1. text +1. text +1. text + +================================================================================ +`; diff --git a/tests/format/markdown/list/huge-first-number/format.test.js b/tests/format/markdown/list/huge-first-number/format.test.js new file mode 100644 index 000000000..c1fda6633 --- /dev/null +++ b/tests/format/markdown/list/huge-first-number/format.test.js @@ -0,0 +1,5 @@ +runFormatTest(import.meta, [ + "markdown", + // Add this parser when closing https://github.com/prettier/prettier/issues/19339 + // "mdx", +]); diff --git a/tests/format/markdown/list/huge-first-number/huge-first-number.md b/tests/format/markdown/list/huge-first-number/huge-first-number.md new file mode 100644 index 000000000..603db3fb2 --- /dev/null +++ b/tests/format/markdown/list/huge-first-number/huge-first-number.md @@ -0,0 +1,18 @@ +999999998. text +999999998. text +999999998. text +999999998. text + +--- + +999999998. text +1. text +1. text +1. text + +--- + +999999998. text +1. text +2. text +3. text