fix: fix block elements in task item (#3828)

Co-authored-by: Steven <steven@ceriously.com>
This commit is contained in:
Tony Brix
2025-11-20 09:56:01 -07:00
committed by GitHub
co-authored by Steven
parent 1e47df2ac6
commit 921ee22102
6 changed files with 112 additions and 15 deletions
+2 -1
View File
@@ -16,8 +16,9 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
top: boolean;
};
public inlineQueue: { src: string, tokens: Token[] }[];
private tokenizer: _Tokenizer<ParserOutput, RendererOutput>;
private inlineQueue: { src: string, tokens: Token[] }[];
constructor(options?: MarkedOptions<ParserOutput, RendererOutput>) {
// TokenList cannot be created in one go
+14 -10
View File
@@ -380,19 +380,10 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
}
}
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<ParserOutput = string, RendererOutput = string> {
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 = {
+1 -1
View File
@@ -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/,
+2 -3
View File
@@ -6,10 +6,9 @@
<p><input type="checkbox" checked="" disabled=""> Task1</p>
</li>
<li>
<p><input type="checkbox" disabled=""></p>
<pre>Task2</pre>
<p><input type="checkbox" disabled=""> <pre>Task2</pre></p>
</li>
<li>
<p><input type="checkbox" disabled=""></p>
<p>[ ]</p>
</li>
</ul>
+53
View File
@@ -0,0 +1,53 @@
<h1>tight</h1>
<ul>
<li><input checked="" disabled="" type="checkbox"> # heading</li>
<li><input checked="" disabled="" type="checkbox"> - list</li>
<li><input checked="" disabled="" type="checkbox"> &gt; blockquote</li>
<li><input checked="" disabled="" type="checkbox"> ---</li>
<li><input checked="" disabled="" type="checkbox"> ```
<pre><code></code></pre>
</li>
<li><input checked="" disabled="" type="checkbox"> [def]: <a href="https://example.com">https://example.com</a></li>
<li><input checked="" disabled="" type="checkbox"> <div>
<em>html</em>
</div>
*html*
</li>
<li><input checked="" disabled="" type="checkbox"> | a | b |
|---|---|
| 1 | 1 |</li>
</ul>
<h1>loose</h1>
<ul>
<li>
<p><input checked="" disabled="" type="checkbox"> # heading</p>
</li>
<li>
<p><input checked="" disabled="" type="checkbox"> - list</p>
</li>
<li>
<p><input checked="" disabled="" type="checkbox"> &gt; blockquote</p>
</li>
<li>
<p><input checked="" disabled="" type="checkbox"> ---</p>
</li>
<li>
<p><input checked="" disabled="" type="checkbox"> ```</p>
<pre><code>
</code></pre>
</li>
<li>
<p><input checked="" disabled="" type="checkbox"> [def]: <a href="https://example.com">https://example.com</a></p>
</li>
<li>
<p><input checked="" disabled="" type="checkbox"> <div>
<em>html</em></p>
</div>
*html*
</li>
<li>
<p><input checked="" disabled="" type="checkbox"> | a | b |
|---|---|
| 1 | 1 |</p>
</li>
</ul>
+40
View File
@@ -0,0 +1,40 @@
# tight
- [x] # heading
- [x] - list
- [x] > blockquote
- [x] ---
- [x] ```
```
- [x] [def]: https://example.com
- [x] <div>
*html*
</div>
*html*
- [x] | a | b |
|---|---|
| 1 | 1 |
# loose
- [x] # heading
- [x] - list
- [x] > blockquote
- [x] ---
- [x] ```
```
- [x] [def]: https://example.com
- [x] <div>
*html*
</div>
*html*
- [x] | a | b |
|---|---|
| 1 | 1 |