diff --git a/apps/web/src/composer-list-continuation.test.ts b/apps/web/src/composer-list-continuation.test.ts index 3b02ddea6e751..6cf1868e56cad 100644 --- a/apps/web/src/composer-list-continuation.test.ts +++ b/apps/web/src/composer-list-continuation.test.ts @@ -150,12 +150,23 @@ describe("nextOrderedMarkerText", () => { ["001)", "002)"], ["99.", "100."], ["99999999999999999999.", "99999999999999999999."], + ["999999999.", "999999999."], ])("counts %s up to %s", (marker, expected) => { expect(nextOrderedMarkerText(marker)).toBe(expected); }); }); describe("composer list renumbering", () => { + it("keeps generated markers within Markdown's nine digits", () => { + expect(applyEdit("999999999. a", listContinuationForEnter("999999999. a", 12)!)).toBe( + "999999999. a\n999999999. ", + ); + const value = "999999998. a\n999999999. b"; + expect(applyEdit(value, listContinuationForEnter(value, value.indexOf("\n"))!)).toBe( + "999999998. a\n999999999. \n999999999. b", + ); + }); + it("renumbers following items with their padding and leaves the caret after the inserted marker", () => { const value = "1. @README.md go\n2. two\n03. three\n10. four"; const edit = listContinuationForEnter(value, value.indexOf("go"))!; diff --git a/apps/web/src/composer-list-continuation.ts b/apps/web/src/composer-list-continuation.ts index a74de0f28dcae..d701f4a07386a 100644 --- a/apps/web/src/composer-list-continuation.ts +++ b/apps/web/src/composer-list-continuation.ts @@ -78,14 +78,14 @@ function contentColumn(indent: string, markerWidth: number, padding: string): nu return markerEnd + (space >= 1 && space <= 4 ? space : 1); } +// CommonMark ordered markers allow at most nine digits. +const MAX_ORDERED_MARKER = 999_999_999; + export function nextOrderedMarkerText(marker: string): string { const numberText = marker.slice(0, -1); const delimiter = marker.slice(-1); - const number = Number.parseInt(numberText, 10); - const next = Number.isSafeInteger(number) - ? String(number + 1).padStart(numberText.length, "0") - : numberText; - return `${next}${delimiter}`; + const next = String(Number.parseInt(numberText, 10) + 1).padStart(numberText.length, "0"); + return `${next.length <= 9 ? next : numberText}${delimiter}`; } function nextMarkerText(marker: ListMarker): string { @@ -123,7 +123,7 @@ function renumberFollowingItems( let position = afterLineEnd; let copiedUntil = afterLineEnd; let tokenIndex = 0; - while (position < value.length && Number.isSafeInteger(expected)) { + while (position < value.length && expected <= MAX_ORDERED_MARKER) { const lineStart = position + 1; const nextBreak = value.indexOf("\n", lineStart); const lineEnd = nextBreak === -1 ? value.length : nextBreak; diff --git a/apps/web/src/composer-list-split.browser.test.ts b/apps/web/src/composer-list-split.browser.test.ts index fa1aa139ff860..632409a5bf0f8 100644 --- a/apps/web/src/composer-list-split.browser.test.ts +++ b/apps/web/src/composer-list-split.browser.test.ts @@ -112,4 +112,12 @@ describe("Enter in a list item", () => { expect(splitOrLiftListItem(editor, () => true)).toBe(true); expect(storedDraft(editor)).toBe(expected); }); + + it("keeps the new item's marker within Markdown's nine digits", () => { + using editor = makeEditor("999999999. first"); + editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc))); + expect(splitOrLiftListItem(editor, () => true)).toBe(true); + editor.commands.insertContent("second"); + expect(storedDraft(editor)).toBe("999999999. first\n999999999. second"); + }); });