Skip to content

fix: keep an indented code block after a list item paragraph - #4198

Merged
UziTech merged 2 commits into
markedjs:masterfrom
jayhemnani9910:fix/list-item-indented-code
Nov 16, 2026
Merged

UziTech merged 2 commits into
markedjs:masterfrom
jayhemnani9910:fix/list-item-indented-code

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

Marked version: 18.1.3 (also master ba50aa9)

Markdown flavor: CommonMark

Description

Expectation

- Run this
      after wrapping

      npm install

commonmark.js and markdown-it both give:

<ul><li><p>Run this after wrapping</p><pre><code>npm install
</code></pre></li></ul>

Result

<ul><li>Run this
after wrapping

npm install</li></ul>

The code block becomes paragraph text. This shows up in docs where a step in a numbered list has a deeper indented line (for example a mkdocs !!! note admonition) followed by a code block. Top level and block quotes are fine.

What was attempted

Inside a list item the text rule takes one line, so after wrapping is lexed as an indented code token. The code rule then runs across the blank line and takes npm install with it, and the Lexer appends the whole token to the paragraph, since indented code cannot interrupt a paragraph.

When that merge happens, only the lines before the first blank line are appended now. The rest goes back into src and is lexed again, so it becomes a space token and a code block. The new regex is other.innerBlankLine (/\n[ \t]*\n/), and it only runs on that merge path.

Added test/specs/new/list_item_indented_code_after_paragraph; it fails before the change. npm run test:specs (1985), npm run test:unit (242) and npm run test:lint pass.

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.

In a list item, a paragraph line indented four or more spaces is lexed
as indented code and then merged back into the paragraph. The code
token also ran across the following blank line, so a real indented
code block after it was merged into the paragraph too.
@vercel

vercel Bot commented Nov 8, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
marked-website Ready Ready Preview Nov 11, 2026 7:20am UTC

Request Review

Comment thread src/Lexer.ts Outdated
if (blankLine !== -1) {
src = token.raw.substring(blankLine) + src;
token.raw = token.raw.substring(0, blankLine);
token.text = token.raw.replace(this.tokenizer.rules.other.codeRemoveIndent, '');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should only edit tokens in a tokenizer function otherwise extensions can't easily change the behavior

@jayhemnani9910

Copy link
Copy Markdown
Contributor Author

Moved it out of the lexer in 2f60a91. Lexer.ts is back to master. The text tokenizer now takes the indented lines after a text line, up to the next blank line, as part of the same text, since indented code can't interrupt it. An indented code block after the blank line is then lexed as code, and an extension that overrides text controls this.

The new spec fails on master and passes here, and npm test passes. One side effect: with breaks: true, - a\n w\n x rendered w<br><br>x and now renders w<br>x. One case still behaves as on master: a link definition in the item before the indented lines. Covering that would need code() to see the previous token.

@UziTech UziTech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! 💯

@UziTech
UziTech merged commit b9e3824 into markedjs:master Nov 16, 2026
8 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 24, 2026
# [18.3.0](v18.2.1...v18.3.0) (2026-11-24)

### Bug Fixes

* allow trailing tabs after closing code fences ([#4241](#4241)) ([1e11902](1e11902))
* avoid quadratic scan on adjacent inline links ([#4274](#4274)) ([f5ffd22](f5ffd22))
* avoid quadratic time on long local part before @ in GFM text ([#4225](#4225)) ([c6b22e8](c6b22e8))
* check own properties on extensions.renderers in Parser ([#4271](#4271)) ([c572307](c572307))
* honor the CLI end-of-options marker ([#4230](#4230)) ([a1ddc72](a1ddc72))
* keep an indented code block after a list item paragraph ([#4198](#4198)) ([b9e3824](b9e3824))
* preserve literal entities in image alt text ([#4232](#4232)) ([04f1c6e](04f1c6e))
* reject CLI options missing their values ([#4261](#4261)) ([502e658](502e658))
* skip missing optional childTokens properties in walkTokens ([#4262](#4262)) ([283f74f](283f74f))
* wait for CLI error output before exiting ([#4263](#4263)) ([6c2a5ab](6c2a5ab))

### Features

* add inBlockquote to lexer state ([#4222](#4222)) ([201bfce](201bfce)), closes [#4184](#4184) [#4221](#4221)

### Performance Improvements

* avoid scanning table bodies for header-only decisions ([#4213](#4213)) ([a0925cc](a0925cc))
* skip paragraph lookahead for initial quoted code ([#4233](#4233)) ([171f177](171f177))

This branch was successfully deployed

1 active deployment
Preview — 2f60a910 Deployed Nov 11, 2026 by vercel[bot]
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.

3 participants