Skip to content

Commit 921ee22

Browse files
UziTechstyfle
andauthored
fix: fix block elements in task item (#3828)
Co-authored-by: Steven <steven@ceriously.com>
1 parent 1e47df2 commit 921ee22

File tree

6 files changed

+112
-15
lines changed

6 files changed

+112
-15
lines changed

src/Lexer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
1616
top: boolean;
1717
};
1818

19+
public inlineQueue: { src: string, tokens: Token[] }[];
20+
1921
private tokenizer: _Tokenizer<ParserOutput, RendererOutput>;
20-
private inlineQueue: { src: string, tokens: Token[] }[];
2122

2223
constructor(options?: MarkedOptions<ParserOutput, RendererOutput>) {
2324
// TokenList cannot be created in one go

src/Tokenizer.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -380,19 +380,10 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
380380
}
381381
}
382382

383-
let istask: RegExpExecArray | null = null;
384-
// Check for task list items
385-
if (this.options.gfm) {
386-
istask = this.rules.other.listIsTask.exec(itemContents);
387-
if (istask) {
388-
itemContents = itemContents.replace(this.rules.other.listReplaceTask, '');
389-
}
390-
}
391-
392383
list.items.push({
393384
type: 'list_item',
394385
raw,
395-
task: !!istask,
386+
task: !!this.options.gfm && this.rules.other.listIsTask.test(itemContents),
396387
loose: false,
397388
text: itemContents,
398389
tokens: [],
@@ -417,6 +408,19 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
417408
this.lexer.state.top = false;
418409
item.tokens = this.lexer.blockTokens(item.text, []);
419410
if (item.task) {
411+
// Remove checkbox markdown from item tokens
412+
item.text = item.text.replace(this.rules.other.listReplaceTask, '');
413+
if (item.tokens[0]?.type === 'text' || item.tokens[0]?.type === 'paragraph') {
414+
item.tokens[0].raw = item.tokens[0].raw.replace(this.rules.other.listReplaceTask, '');
415+
item.tokens[0].text = item.tokens[0].text.replace(this.rules.other.listReplaceTask, '');
416+
for (let i = this.lexer.inlineQueue.length - 1; i >= 0; i--) {
417+
if (this.rules.other.listIsTask.test(this.lexer.inlineQueue[i].src)) {
418+
this.lexer.inlineQueue[i].src = this.lexer.inlineQueue[i].src.replace(this.rules.other.listReplaceTask, '');
419+
break;
420+
}
421+
}
422+
}
423+
420424
const taskRaw = this.rules.other.listTaskCheckbox.exec(item.raw);
421425
if (taskRaw) {
422426
const checkboxToken: Tokens.Checkbox = {

src/rules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const other = {
4646
blockquoteSetextReplace2: /^ {0,3}>[ \t]?/gm,
4747
listReplaceTabs: /^\t+/,
4848
listReplaceNesting: /^ {1,4}(?=( {4})*[^ ])/g,
49-
listIsTask: /^\[[ xX]\] /,
49+
listIsTask: /^\[[ xX]\] +\S/,
5050
listReplaceTask: /^\[[ xX]\] +/,
5151
listTaskCheckbox: /\[[ xX]\]/,
5252
anyLine: /\n.*\n/,

test/specs/new/list_loose_tasks.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
<p><input type="checkbox" checked="" disabled=""> Task1</p>
77
</li>
88
<li>
9-
<p><input type="checkbox" disabled=""></p>
10-
<pre>Task2</pre>
9+
<p><input type="checkbox" disabled=""> <pre>Task2</pre></p>
1110
</li>
1211
<li>
13-
<p><input type="checkbox" disabled=""></p>
12+
<p>[ ]</p>
1413
</li>
1514
</ul>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<h1>tight</h1>
2+
<ul>
3+
<li><input checked="" disabled="" type="checkbox"> # heading</li>
4+
<li><input checked="" disabled="" type="checkbox"> - list</li>
5+
<li><input checked="" disabled="" type="checkbox"> &gt; blockquote</li>
6+
<li><input checked="" disabled="" type="checkbox"> ---</li>
7+
<li><input checked="" disabled="" type="checkbox"> ```
8+
<pre><code></code></pre>
9+
</li>
10+
<li><input checked="" disabled="" type="checkbox"> [def]: <a href="https://example.com">https://example.com</a></li>
11+
<li><input checked="" disabled="" type="checkbox"> <div>
12+
<em>html</em>
13+
</div>
14+
*html*
15+
</li>
16+
<li><input checked="" disabled="" type="checkbox"> | a | b |
17+
|---|---|
18+
| 1 | 1 |</li>
19+
</ul>
20+
<h1>loose</h1>
21+
<ul>
22+
<li>
23+
<p><input checked="" disabled="" type="checkbox"> # heading</p>
24+
</li>
25+
<li>
26+
<p><input checked="" disabled="" type="checkbox"> - list</p>
27+
</li>
28+
<li>
29+
<p><input checked="" disabled="" type="checkbox"> &gt; blockquote</p>
30+
</li>
31+
<li>
32+
<p><input checked="" disabled="" type="checkbox"> ---</p>
33+
</li>
34+
<li>
35+
<p><input checked="" disabled="" type="checkbox"> ```</p>
36+
<pre><code>
37+
</code></pre>
38+
</li>
39+
<li>
40+
<p><input checked="" disabled="" type="checkbox"> [def]: <a href="https://example.com">https://example.com</a></p>
41+
</li>
42+
<li>
43+
<p><input checked="" disabled="" type="checkbox"> <div>
44+
<em>html</em></p>
45+
</div>
46+
*html*
47+
</li>
48+
<li>
49+
<p><input checked="" disabled="" type="checkbox"> | a | b |
50+
|---|---|
51+
| 1 | 1 |</p>
52+
</li>
53+
</ul>

test/specs/new/tasklist_blocks.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# tight
2+
3+
- [x] # heading
4+
- [x] - list
5+
- [x] > blockquote
6+
- [x] ---
7+
- [x] ```
8+
```
9+
- [x] [def]: https://example.com
10+
- [x] <div>
11+
*html*
12+
</div>
13+
*html*
14+
- [x] | a | b |
15+
|---|---|
16+
| 1 | 1 |
17+
18+
# loose
19+
20+
- [x] # heading
21+
22+
- [x] - list
23+
24+
- [x] > blockquote
25+
26+
- [x] ---
27+
28+
- [x] ```
29+
```
30+
31+
- [x] [def]: https://example.com
32+
33+
- [x] <div>
34+
*html*
35+
</div>
36+
*html*
37+
38+
- [x] | a | b |
39+
|---|---|
40+
| 1 | 1 |

0 commit comments

Comments
 (0)