Skip to content

Commit 00d020e

Browse files
mejo-backportbot[bot]
authored andcommitted
fix(orderdList): preserve non-1 start numbers
Also fixes preserving `0` as first list number. Fixes: #4828 Signed-off-by: Jonas <jonas@freesources.org>
1 parent 7398235 commit 00d020e

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/nodes/OrderedList.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6+
import type { Node } from '@tiptap/pm/model'
7+
import type { MarkdownSerializerState } from 'prosemirror-markdown'
8+
69
import { OrderedList as TiptapOrderedList } from '@tiptap/extension-list'
710
import { toggleListCommand } from '../commands'
811

@@ -23,6 +26,18 @@ const OrderedList = TiptapOrderedList.extend({
2326
toggleOrderedList: toggleListCommand('orderedList', 'listItem'),
2427
}
2528
},
29+
30+
// Overwrite toMarkdown from prosemirror-markdown to preserve non-1 start numbers in lists
31+
// @ts-expect-error - toMarkdown is a custom field not part of the official Tiptap API
32+
toMarkdown(state: MarkdownSerializerState, node: Node) {
33+
const start = node.attrs.start ?? 1
34+
const maxW = String(start + node.childCount - 1).length
35+
const space = state.repeat(' ', maxW + 2)
36+
state.renderList(node, space, (i) => {
37+
const nStr = String(start + i)
38+
return state.repeat(' ', maxW - nStr.length) + nStr + '. '
39+
})
40+
},
2641
})
2742

2843
export default OrderedList

src/tests/markdown.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ describe('Markdown though editor', () => {
5555
})
5656
test('ol', () => {
5757
expect(markdownThroughEditor('1. foo\n2. bar')).toBe('1. foo\n2. bar')
58+
expect(markdownThroughEditor('0. foo\n1. bar')).toBe('0. foo\n1. bar')
59+
// regression test for #4828
60+
expect(markdownThroughEditor('42. foo\n43. bar')).toBe('42. foo\n43. bar')
61+
// for now we enforce sequential order in ordered lists
62+
expect(markdownThroughEditor('2. foo\n19. bar\n2. baz')).toBe(
63+
'2. foo\n3. bar\n4. baz',
64+
)
5865
})
5966
test('paragraph', () => {
6067
// Test whitespace characters are untouched

0 commit comments

Comments
 (0)