From 921ee22102a4aa9c19286afd61610d1952ffca8e Mon Sep 17 00:00:00 2001
From: Tony Brix
Date: Thu, 20 Nov 2025 09:56:01 -0700
Subject: [PATCH] fix: fix block elements in task item (#3828)
Co-authored-by: Steven
---
src/Lexer.ts | 3 +-
src/Tokenizer.ts | 24 +++++++------
src/rules.ts | 2 +-
test/specs/new/list_loose_tasks.html | 5 ++-
test/specs/new/tasklist_blocks.html | 53 ++++++++++++++++++++++++++++
test/specs/new/tasklist_blocks.md | 40 +++++++++++++++++++++
6 files changed, 112 insertions(+), 15 deletions(-)
create mode 100644 test/specs/new/tasklist_blocks.html
create mode 100644 test/specs/new/tasklist_blocks.md
diff --git a/src/Lexer.ts b/src/Lexer.ts
index a54910ab..89d439dc 100644
--- a/src/Lexer.ts
+++ b/src/Lexer.ts
@@ -16,8 +16,9 @@ export class _Lexer {
top: boolean;
};
+ public inlineQueue: { src: string, tokens: Token[] }[];
+
private tokenizer: _Tokenizer;
- private inlineQueue: { src: string, tokens: Token[] }[];
constructor(options?: MarkedOptions) {
// TokenList cannot be created in one go
diff --git a/src/Tokenizer.ts b/src/Tokenizer.ts
index 34674e02..4f176de6 100644
--- a/src/Tokenizer.ts
+++ b/src/Tokenizer.ts
@@ -380,19 +380,10 @@ export class _Tokenizer {
}
}
- let istask: RegExpExecArray | null = null;
- // Check for task list items
- if (this.options.gfm) {
- istask = this.rules.other.listIsTask.exec(itemContents);
- if (istask) {
- itemContents = itemContents.replace(this.rules.other.listReplaceTask, '');
- }
- }
-
list.items.push({
type: 'list_item',
raw,
- task: !!istask,
+ task: !!this.options.gfm && this.rules.other.listIsTask.test(itemContents),
loose: false,
text: itemContents,
tokens: [],
@@ -417,6 +408,19 @@ export class _Tokenizer {
this.lexer.state.top = false;
item.tokens = this.lexer.blockTokens(item.text, []);
if (item.task) {
+ // Remove checkbox markdown from item tokens
+ item.text = item.text.replace(this.rules.other.listReplaceTask, '');
+ if (item.tokens[0]?.type === 'text' || item.tokens[0]?.type === 'paragraph') {
+ item.tokens[0].raw = item.tokens[0].raw.replace(this.rules.other.listReplaceTask, '');
+ item.tokens[0].text = item.tokens[0].text.replace(this.rules.other.listReplaceTask, '');
+ for (let i = this.lexer.inlineQueue.length - 1; i >= 0; i--) {
+ if (this.rules.other.listIsTask.test(this.lexer.inlineQueue[i].src)) {
+ this.lexer.inlineQueue[i].src = this.lexer.inlineQueue[i].src.replace(this.rules.other.listReplaceTask, '');
+ break;
+ }
+ }
+ }
+
const taskRaw = this.rules.other.listTaskCheckbox.exec(item.raw);
if (taskRaw) {
const checkboxToken: Tokens.Checkbox = {
diff --git a/src/rules.ts b/src/rules.ts
index acd62073..2f95b80b 100644
--- a/src/rules.ts
+++ b/src/rules.ts
@@ -46,7 +46,7 @@ export const other = {
blockquoteSetextReplace2: /^ {0,3}>[ \t]?/gm,
listReplaceTabs: /^\t+/,
listReplaceNesting: /^ {1,4}(?=( {4})*[^ ])/g,
- listIsTask: /^\[[ xX]\] /,
+ listIsTask: /^\[[ xX]\] +\S/,
listReplaceTask: /^\[[ xX]\] +/,
listTaskCheckbox: /\[[ xX]\]/,
anyLine: /\n.*\n/,
diff --git a/test/specs/new/list_loose_tasks.html b/test/specs/new/list_loose_tasks.html
index f7bc47eb..1416f042 100644
--- a/test/specs/new/list_loose_tasks.html
+++ b/test/specs/new/list_loose_tasks.html
@@ -6,10 +6,9 @@
Task1
-
-Task2
+
Task2
-
+[ ]
diff --git a/test/specs/new/tasklist_blocks.html b/test/specs/new/tasklist_blocks.html
new file mode 100644
index 00000000..6a8fb31f
--- /dev/null
+++ b/test/specs/new/tasklist_blocks.html
@@ -0,0 +1,53 @@
+tight
+
+loose
+
diff --git a/test/specs/new/tasklist_blocks.md b/test/specs/new/tasklist_blocks.md
new file mode 100644
index 00000000..488a4612
--- /dev/null
+++ b/test/specs/new/tasklist_blocks.md
@@ -0,0 +1,40 @@
+# tight
+
+- [x] # heading
+- [x] - list
+- [x] > blockquote
+- [x] ---
+- [x] ```
+ ```
+- [x] [def]: https://example.com
+- [x]
+ *html*
+
+ *html*
+- [x] | a | b |
+ |---|---|
+ | 1 | 1 |
+
+# loose
+
+- [x] # heading
+
+- [x] - list
+
+- [x] > blockquote
+
+- [x] ---
+
+- [x] ```
+ ```
+
+- [x] [def]: https://example.com
+
+- [x]
+ *html*
+
+ *html*
+
+- [x] | a | b |
+ |---|---|
+ | 1 | 1 |