Skip to content

fix: parse empty list item with trailing space#3984

Open
sarathfrancis90 wants to merge 1 commit into
markedjs:masterfrom
sarathfrancis90:fix-empty-list-item-trailing-space
Open

fix: parse empty list item with trailing space#3984
sarathfrancis90 wants to merge 1 commit into
markedjs:masterfrom
sarathfrancis90:fix-empty-list-item-trailing-space

Conversation

@sarathfrancis90
Copy link
Copy Markdown

Marked version: 18.0.4 (master)

Markdown flavor: CommonMark

Description

A bare list marker followed only by a space (e.g. - , * , 1. , 1) ) was not recognized as a list. It fell through to the paragraph rule and rendered as a paragraph instead of an empty list item.

This is internally inconsistent: a marker with no trailing space already worked, and an empty item that continues an existing list also worked — only a marker followed by a single trailing space was broken.

Expectation

- (and * , 1. , 1) ) should produce an empty list item, matching the CommonMark reference implementation (commonmark 0.31.2) and markdown-it 14.2.0 in CommonMark mode:

<ul>
<li></li>
</ul>

Result

marked produced a paragraph containing the literal marker:

<p>- </p>

Root cause

The block list rule required at least one content character after the marker's whitespace:

^(bull)([ \t][^\n]+?)?(?:\n|$)

The +? means a marker followed only by whitespace cannot match, so it falls through to the paragraph rule. A previous change (PR #3890) updated the marker prefix but left this content group as +?, so the trailing-space case stayed broken (the fixture there had no trailing space). This PR is the complementary fix.

Fix

Relax the content group from [ \t][^\n]+? to [ \t][^\n]*? so a marker followed only by whitespace still starts a list:

^(bull)([ \t][^\n]*?)?(?:\n|$)

To preserve CommonMark's rule that an empty list item cannot interrupt a paragraph, the paragraph (and GFM block) 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. The GFM table interrupt pattern is intentionally left unchanged, since a bare bullet still ends table rows.

Test evidence

  • Added test/specs/new/list_item_empty_trailing_space.{md,html} covering - , * , 1. , 1) , the paragraph non-interrupt case foo\n+ , and the still-interrupting case bar\n1. .
  • The new fixture fails on unmodified master and passes with this change (TDD).
  • Full suite green locally: test:specs 1743 pass / 0 fail, test:unit 188 pass / 0 fail, plus test:cjs, test:umd, test:types (attw) and test:lint. CommonMark "List items" (48/48) and "Lists" (26/26) remain at 100% with no regression.

Contributor

  • Test(s) exist to ensure functionality and minimize regression (if no tests added, list tests covering this PR); or,
  • no tests required for this PR.
  • If submitting new feature, it has been documented in the appropriate places.

Committer

In most cases, this should be a different person than the contributor.

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.
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 2, 2026

@sarathfrancis90 is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates markdown parsing rules in src/rules.ts to ensure that only non-empty lists starting with 1 can interrupt a paragraph, and updates the list regex to match zero or more non-newline characters. It also adds new test specs for list items with empty trailing spaces. I have no feedback to provide as there are no review comments.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 2, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marked-website Ready Ready Preview, Comment Jun 2, 2026 5:58am

Request Review

Copy link
Copy Markdown
Member

@UziTech UziTech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 💯

@UziTech UziTech requested review from calculuschild and styfle June 2, 2026 06:15
@sarathfrancis90
Copy link
Copy Markdown
Author

Thanks for the quick review, @UziTech! 🙏 Glad it lines up with the CommonMark reference and markdown-it behavior — happy to adjust anything if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants