[Bug]: @tiptap/markdown parses "Hi. there" as an ordered list and deletes the word; lists starting at 0 are renumbered
Affected Packages
@tiptap/extension-list, @tiptap/markdown
Version(s)
3.31.3 (also 3.27.1)
Bug Description
The ordered-list markdown tokenizer accepts alpha and roman markers
(ORDERED_LIST_MARKER_PATTERN = \d+|[ivxlcdmIVXLCDM]+|[a-zA-Z]{1,2}). Two
problems follow.
1. Ordinary sentences become lists, and the first word is deleted. Any
paragraph starting with a 1-2 letter word (or a roman-numeral-looking word)
followed by . or ) is parsed as an ordered list. The word is treated as the
marker and dropped, so the text is lost on the first getMarkdown(). The same
happens to continuation lines inside a numbered item.
| Input | getMarkdown() |
|---|---|
Hi. there |
1. there |
Ok. Next step. |
1. Next step. |
Ex) aside |
1. aside |
1. Step one\n Ok. then more\n2. two |
1. Step one\n 1. then more\n2. two |
CommonMark and GFM only define numeric ordered-list markers (1-9 digits), so a markdown file written anywhere else is silently rewritten here. Alpha/roman markers are a Pandoc-style extension. If they are kept, they should at least be opt-in and require stricter context.
2. A list that starts at 0 is renumbered from 1. 0. zero\n1. one becomes
1. zero\n2. one. Both OrderedList.parseMarkdown (token.start || 1) and
ListItem.renderMarkdown (parentAttrs.start || 1) treat 0 as missing. 0.
is a valid CommonMark start number.
Browser Used
Other (Node.js 24 + happy-dom; the parser is browser-independent. Also seen in Chrome.)
Code Example URL
(no sandbox - self-contained script below)
// npm i @tiptap/[email protected] @tiptap/[email protected] @tiptap/[email protected] @tiptap/[email protected] happy-dom
import { Window } from 'happy-dom'
const window = new Window()
for (const [k, v] of Object.entries({ window, document: window.document, Node: window.Node, HTMLElement: window.HTMLElement, getComputedStyle: window.getComputedStyle.bind(window) })) {
Object.defineProperty(globalThis, k, { value: v, configurable: true })
}
const { Editor } = await import('@tiptap/core')
const { default: StarterKit } = await import('@tiptap/starter-kit')
const { Markdown } = await import('@tiptap/markdown')
const roundTrip = (md) => {
const editor = new Editor({ extensions: [StarterKit, Markdown], content: md, contentType: 'markdown' })
const out = editor.getMarkdown()
editor.destroy()
return out
}
console.log(roundTrip('Hi. there')) // "1. there" - expected "Hi. there"
console.log(roundTrip('Ex) aside')) // "1. aside" - expected "Ex) aside"
console.log(roundTrip('0. zero\n1. one')) // "1. zero\n2. one" - expected unchangedExpected Behavior
Hi. there,Ok. Next step.andEx) asidestay paragraphs, matching every CommonMark/GFM renderer. At minimum, no text is deleted.0. zero\n1. oneround-trips unchanged:start: 0is kept when parsing and when rendering item markers.
Additional Context
- Suggested fix for (1): only match numeric markers by default, or put alpha and
roman markers behind an option (e.g.
OrderedList.configure({ markdown: { alphaMarkers: true } })). If they stay enabled, require list context, such as a following item with the next marker in sequence, before treating a loneHi.as a list. - Suggested fix for (2): use
??instead of||forstartinOrderedList.parseMarkdownandListItem.renderMarkdown. - Workaround we ship: replace
OrderedListwith an extension whosemarkdownTokenizernever matches (so marked's GFM list tokenizer runs), and replaceListItem.renderMarkdownwith a?? 1marker. In 3.27.1 that fallback path also flattened inline markup in list items (`code`became literal text). That part is fixed in 3.31.3, which is why it is not reported here. - Found through Nuxt UI's
UEditor(@nuxt/ui4.9,content-type="markdown"), where user documents were rewritten on the first visual edit.
Source: ueberdosis/tiptap