Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## Unreleased

### 🐛 Fixed

* Keep block-like text after a task-list checkbox inside its original paragraph.

## 4.3.0 - 2026-11-10

This release fixes two quadratic-time parsing paths that let crafted input stall rendering for minutes (security),
Expand Down
42 changes: 18 additions & 24 deletions markdown_it/rules_block/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ def list_block(state: StateBlock, startLine: int, endLine: int, silent: bool) ->
indent = initial + indentAfterMarker

# Run subparser & write tokens
itemTokIdx = len(state.tokens)
token = state.push("list_item_open", "li", 1)
token.markup = markerChar
token.map = itemLines = [startLine, 0]
Expand All @@ -237,7 +238,6 @@ def list_block(state: StateBlock, startLine: int, endLine: int, silent: bool) ->

# change current state, then restore it after parser subcall
oldTight = state.tight
oldBMark = state.bMarks[startLine]
oldTShift = state.tShift[startLine]
oldSCount = state.sCount[startLine]

Expand All @@ -253,27 +253,11 @@ def list_block(state: StateBlock, startLine: int, endLine: int, silent: bool) ->
state.tShift[startLine] = contentStart - state.bMarks[startLine]
state.sCount[startLine] = offset

# Detect GFM task checkboxes only after setting the item's indentation,
# so literal markers in indented code blocks are left untouched.
checkboxLen = 0
if (
state.md.options.get("tasklists", False)
and contentStart < maximum
and not state.is_code_block(startLine)
):
checked = _detect_task_checkbox(state.src, contentStart, maximum)
if checked is not None:
token.meta = {"checked": checked}
# Advance content past the checkbox: `[x]` (3 chars) + whitespace.
# `_detect_task_checkbox` already guarantees a whitespace char at
# pos+3, so we always consume 4 characters.
checkboxLen = 4

# If we detected a checkbox, advance bMarks past it so that
# getLines() doesn't include the checkbox text in the content.
if checkboxLen:
state.bMarks[startLine] = contentStart + checkboxLen
state.tShift[startLine] = 0
checked = (
_detect_task_checkbox(state.src, contentStart, maximum)
if state.md.options.get("tasklists", False)
else None
)

if contentStart >= maximum and state.isEmpty(startLine + 1):
# workaround for this case
Expand All @@ -290,6 +274,18 @@ def list_block(state: StateBlock, startLine: int, endLine: int, silent: bool) ->
# but tokeniz does not take the final parameter
state.md.block.tokenize(state, startLine, endLine)

# A task marker belongs to the first paragraph. Removing it before
# block parsing turns text such as "[x] # title" into a heading.
firstBlock = state.tokens[itemTokIdx + 1 : itemTokIdx + 3]
if (
checked is not None
and len(firstBlock) == 2
and firstBlock[0].type == "paragraph_open"
and firstBlock[1].type == "inline"
):
state.tokens[itemTokIdx].meta["checked"] = checked
firstBlock[1].content = firstBlock[1].content[4:]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

On main the text after the checkbox was stripped again by the paragraph rule. Here only 4 characters are cut, so whitespace after the marker now ends up in the inline tokens:

md = MarkdownIt("gfm-like2", {"linkify": False, "breaks": True})
md.render("- [x] \n  foo\n")
# main:    ...checked=""> foo</li>
# this PR: ...checked=""> <br />\nfoo</li>

md.parse("- [x]  foo\n")  # text token: "foo" on main, " foo" here

In the first case the paragraph now starts with a softbreak token (GitHub does not make a task item of it at all: it renders [x]<br>foo).

Suggested change
firstBlock[1].content = firstBlock[1].content[4:]
firstBlock[1].content = firstBlock[1].content[3:].lstrip()

With this both give the same tokens as main again, and tests/test_port + tests/test_cmark_spec pass (1028).


# If any of list item is tight, mark list as tight
if (not state.tight) or prevEmptyEnd:
tight = False
Expand All @@ -300,8 +296,6 @@ def list_block(state: StateBlock, startLine: int, endLine: int, silent: bool) ->

state.blkIndent = state.listIndent
state.listIndent = oldListIndent
if checkboxLen:
state.bMarks[startLine] = oldBMark
state.tShift[startLine] = oldTShift
state.sCount[startLine] = oldSCount
state.tight = oldTight
Expand Down
32 changes: 32 additions & 0 deletions tests/test_port/test_task_block_content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Removing a task marker must not reinterpret its paragraph as a block."""

import pytest

from markdown_it import MarkdownIt


@pytest.mark.parametrize("text", ["# heading", "> quote", "- child", "1. child", "---"])
def test_task_content_stays_in_its_paragraph(text):
md = MarkdownIt("gfm-like2", {"linkify": False})
tokens = md.parse(f"- [x] {text}\n")
assert [token.type for token in tokens] == [
"bullet_list_open",
"list_item_open",
"paragraph_open",
"inline",
"paragraph_close",
"list_item_close",
"bullet_list_close",
]
assert tokens[1].meta["checked"] is True
assert tokens[3].content == text


def test_marker_in_a_setext_heading_is_literal() -> None:
md = MarkdownIt("gfm-like2", {"linkify": False})
tokens = md.parse("- [x] heading\n ---\n")
assert all("checked" not in token.meta for token in tokens)
assert (
next(token for token in tokens if token.type == "inline").content
== "[x] heading"
)
Loading