File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+
69import { OrderedList as TiptapOrderedList } from '@tiptap/extension-list'
710import { 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
2843export default OrderedList
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments