From fccfc5f67e9830921ac01a9b7fc6817af0f7f323 Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Wed, 25 Nov 2026 13:15:56 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Keep=20task-list=20conten?= =?UTF-8?q?t=20in=20its=20original=20paragraph?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 6 ++++ markdown_it/rules_block/list.py | 42 ++++++++++------------ tests/test_port/test_task_block_content.py | 32 +++++++++++++++++ 3 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 tests/test_port/test_task_block_content.py diff --git a/CHANGELOG.md b/CHANGELOG.md index c411d126..740f1b40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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), diff --git a/markdown_it/rules_block/list.py b/markdown_it/rules_block/list.py index 27c16870..3a5fb56e 100644 --- a/markdown_it/rules_block/list.py +++ b/markdown_it/rules_block/list.py @@ -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] @@ -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] @@ -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 @@ -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:] + # If any of list item is tight, mark list as tight if (not state.tight) or prevEmptyEnd: tight = False @@ -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 diff --git a/tests/test_port/test_task_block_content.py b/tests/test_port/test_task_block_content.py new file mode 100644 index 00000000..56e34014 --- /dev/null +++ b/tests/test_port/test_task_block_content.py @@ -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" + )