fix: parse empty list item with trailing space (#3984)

A bare list marker followed only by a space (e.g. `- ` or `1. `) was not
recognized as a list and fell through to a paragraph, so `- ` rendered as
`<p>- </p>` instead of an empty list item. A marker with no trailing space
(`-`, `1.`) and an empty item that continues an existing list were already
handled correctly, making the behavior inconsistent.

The block `list` rule required content after the marker's whitespace
(`[ \t][^\n]+?`), so a marker followed only by whitespace failed to match.
Relax it to `[ \t][^\n]*?` so a marker with trailing whitespace and no
content still starts a list, matching the CommonMark reference and
markdown-it.

To keep CommonMark's rule that an empty list item cannot interrupt a
paragraph, the paragraph list-interrupt patterns now require a non-blank
character after the marker, so `foo\n+ ` stays a single paragraph while
`foo\n- bar` still interrupts.
This commit is contained in:
Sarath Francis
2026-06-04 08:11:58 -06:00
committed by GitHub
parent c6e667bf2a
commit b55410f846
3 changed files with 33 additions and 4 deletions
+4 -4
View File
@@ -136,7 +136,7 @@ const def = edit(/^ {0,3}\[(label)\]: *(?:\n[ \t]*)?([^<\s][^\s]*|<.*?>)(?:(?: +
.replace('title', /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/)
.getRegex();
const list = edit(/^(bull)([ \t][^\n]+?)?(?:\n|$)/)
const list = edit(/^(bull)([ \t][^\n]*?)?(?:\n|$)/)
.replace(/bull/g, bullet)
.getRegex();
@@ -170,7 +170,7 @@ const paragraph = edit(_paragraph)
.replace('|table', '')
.replace('blockquote', ' {0,3}>')
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
.replace('list', ' {0,3}(?:[*+-]|1[.)])[ \\t]') // only lists starting from 1 can interrupt
.replace('list', ' {0,3}(?:[*+-]|1[.)])[ \\t]+[^ \\t\\n]') // only non-empty lists starting from 1 can interrupt
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
.replace('tag', _tag) // pars can be interrupted by type (6) html blocks
.getRegex();
@@ -214,7 +214,7 @@ const gfmTable = edit(
.replace('blockquote', ' {0,3}>')
.replace('code', '(?: {4}| {0,3}\t)[^\\n]')
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
.replace('list', ' {0,3}(?:[*+-]|1[.)])[ \\t]') // only lists starting from 1 can interrupt
.replace('list', ' {0,3}(?:[*+-]|1[.)])[ \\t]') // any bullet ends the table rows
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
.replace('tag', _tag) // tables can be interrupted by type (6) html blocks
.getRegex();
@@ -230,7 +230,7 @@ const blockGfm: Record<BlockKeys, RegExp> = {
.replace('table', gfmTable) // interrupt paragraphs with table
.replace('blockquote', ' {0,3}>')
.replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
.replace('list', ' {0,3}(?:[*+-]|1[.)])[ \\t]') // only lists starting from 1 can interrupt
.replace('list', ' {0,3}(?:[*+-]|1[.)])[ \\t]+[^ \\t\\n]') // only non-empty lists starting from 1 can interrupt
.replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
.replace('tag', _tag) // pars can be interrupted by type (6) html blocks
.getRegex(),
@@ -0,0 +1,16 @@
<ul>
<li></li>
</ul>
<ul>
<li></li>
</ul>
<ol>
<li></li>
</ol>
<ol>
<li></li>
</ol>
<p>foo
+</p>
<p>bar
1.</p>
@@ -0,0 +1,13 @@
-
*
1.
1)
foo
+
bar
1.