fix: place task checkboxes after list loose is finalized (#4046)

Loose nested task lists could leave checkboxes outside <p> because
checkbox placement used list.loose before spacer detection finished.
Compute the final loose state first, then place checkboxes.

Fixes #4045
This commit is contained in:
Hashim Khan
2026-08-15 12:57:45 -06:00
committed by GitHub
parent 9552b6bbca
commit b8cf7dc592
3 changed files with 44 additions and 8 deletions
+13 -8
View File
@@ -416,9 +416,22 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
list.raw = list.raw.trimEnd();
// 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
for (const item of list.items) {
this.lexer.state.top = false;
item.tokens = this.lexer.blockTokens(item.text, []);
if (!list.loose) {
// Check if list should be loose
const spacers = item.tokens.filter(t => t.type === 'space');
const hasMultipleLineBreaks = spacers.length > 0 && spacers.some(t => this.rules.other.anyLine.test(t.raw));
list.loose = hasMultipleLineBreaks;
}
}
// Second pass: place task checkboxes using the final list.loose
for (const item of list.items) {
const itemToken = item.tokens[0];
if (item.task && (itemToken?.type === 'text' || itemToken?.type === 'paragraph')) {
// Remove checkbox markdown from item tokens
@@ -460,14 +473,6 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
} else if (item.task) {
item.task = false;
}
if (!list.loose) {
// Check if list should be loose
const spacers = item.tokens.filter(t => t.type === 'space');
const hasMultipleLineBreaks = spacers.length > 0 && spacers.some(t => this.rules.other.anyLine.test(t.raw));
list.loose = hasMultipleLineBreaks;
}
}
// Set all items to loose if list is loose
@@ -0,0 +1,20 @@
<ul>
<li><p><input disabled="" type="checkbox"> Prepare the project</p>
<ul>
<li><input disabled="" type="checkbox"> Install dependencies</li>
<li><input checked="" disabled="" type="checkbox"> Update Marked</li>
<li><input disabled="" type="checkbox"> Check the rendering</li>
</ul>
</li>
<li><p><input checked="" disabled="" type="checkbox"> Run the tests</p>
<ul>
<li><p><input checked="" disabled="" type="checkbox"> Test lists</p>
</li>
<li><p><input disabled="" type="checkbox"> Test tables</p>
<ul>
<li><input disabled="" type="checkbox"> Test nested cases</li>
</ul>
</li>
</ul>
</li>
</ul>
+11
View File
@@ -0,0 +1,11 @@
* [ ] Prepare the project
* [ ] Install dependencies
* [x] Update Marked
* [ ] Check the rendering
* [x] Run the tests
* [x] Test lists
* [ ] Test tables
* [ ] Test nested cases