mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: remove a task item's checkbox marker from its own text (#4215)
This commit is contained in:
+10
-4
@@ -601,8 +601,11 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
|
||||
|
||||
// Item child tokens handled here at end because we needed to have the final item to trim it first
|
||||
// First pass: tokenize items and finalize list.loose from spacers before placing checkboxes
|
||||
// Where each item's inline queue entries start, so the checkbox is removed from its own text
|
||||
const queueStarts: number[] = [];
|
||||
for (const item of list.items) {
|
||||
this.lexer.state.top = false;
|
||||
queueStarts.push(this.lexer.inlineQueue.length);
|
||||
item.tokens = this.lexer.blockTokens(item.text, []);
|
||||
|
||||
if (!list.loose) {
|
||||
@@ -615,16 +618,19 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
|
||||
}
|
||||
|
||||
// Second pass: place task checkboxes using the final list.loose
|
||||
for (const item of list.items) {
|
||||
for (let i = 0; i < list.items.length; i++) {
|
||||
const item = list.items[i];
|
||||
const itemToken = item.tokens[0];
|
||||
if (item.task && (itemToken?.type === 'text' || itemToken?.type === 'paragraph')) {
|
||||
// Remove checkbox markdown from item tokens
|
||||
item.text = item.text.replace(this.rules.other.listReplaceTask, '');
|
||||
itemToken.raw = itemToken.raw.replace(this.rules.other.listReplaceTask, '');
|
||||
itemToken.text = itemToken.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, '');
|
||||
// The first token queued its text first, so its entry is found right away
|
||||
const inlineQueue = this.lexer.inlineQueue;
|
||||
for (let j = queueStarts[i]; j < inlineQueue.length; j++) {
|
||||
if (inlineQueue[j].tokens === itemToken.tokens) {
|
||||
inlineQueue[j].src = inlineQueue[j].src.replace(this.rules.other.listReplaceTask, '');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
<ul>
|
||||
<li><p><input disabled="" type="checkbox"> a</p>
|
||||
<p>[ ] b</p>
|
||||
</li>
|
||||
<li><p><input checked="" disabled="" type="checkbox"> c</p>
|
||||
<blockquote>
|
||||
<p>[ ] d</p>
|
||||
</blockquote>
|
||||
</li>
|
||||
<li><p><input disabled="" type="checkbox"> e</p>
|
||||
<ul>
|
||||
<li><p>f</p>
|
||||
<p>[x] g</p>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
@@ -0,0 +1,10 @@
|
||||
- [ ] a
|
||||
|
||||
[ ] b
|
||||
- [x] c
|
||||
|
||||
> [ ] d
|
||||
- [ ] e
|
||||
- f
|
||||
|
||||
[x] g
|
||||
Reference in New Issue
Block a user