Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit f51c24c

Browse files
authored
Prepend a line ending if the p tag is removed and there is an element before it in a *tight* list (#513)
* Prepend a line ending if the p tag is removed * Ignore task list
1 parent dd3e3a1 commit f51c24c

File tree

7 files changed

+22
-9
lines changed

7 files changed

+22
-9
lines changed

lib/src/block_syntaxes/list_syntax.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ abstract class ListSyntax extends BlockSyntax {
250250
final anyEmptyLines = _removeTrailingEmptyLines(items);
251251
var anyEmptyLinesBetweenBlocks = false;
252252
var containsTaskList = false;
253+
const taskListClass = 'task-list-item';
253254

254255
for (final item in items) {
255256
Element? checkboxToInsert;
@@ -267,7 +268,7 @@ abstract class ListSyntax extends BlockSyntax {
267268
final itemElement = checkboxToInsert == null
268269
? Element('li', children)
269270
: (Element('li', [checkboxToInsert, ...children])
270-
..attributes['class'] = 'task-list-item');
271+
..attributes['class'] = taskListClass);
271272

272273
itemNodes.add(itemElement);
273274
anyEmptyLinesBetweenBlocks =
@@ -282,14 +283,24 @@ abstract class ListSyntax extends BlockSyntax {
282283
// We must post-process the list items, converting any top-level paragraph
283284
// elements to just text elements.
284285
for (final item in itemNodes) {
286+
final isTaskList = item.attributes['class'] == taskListClass;
285287
final children = item.children;
286288
if (children != null) {
289+
Node? lastNode;
287290
for (var i = 0; i < children.length; i++) {
288291
final child = children[i];
289292
if (child is Element && child.tag == 'p') {
290-
children.removeAt(i);
291-
children.insertAll(i, child.children!);
293+
final childContent = child.children!;
294+
if (lastNode is Element && !isTaskList) {
295+
childContent.insert(0, Text('\n'));
296+
}
297+
298+
children
299+
..removeAt(i)
300+
..insertAll(i, childContent);
292301
}
302+
303+
lastNode = child;
293304
}
294305
}
295306
}

test/common_mark/list_items.unit

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,5 +581,6 @@ continued here.</p>
581581
<h1>Foo</h1>
582582
</li>
583583
<li>
584-
<h2>Bar</h2>baz</li>
584+
<h2>Bar</h2>
585+
baz</li>
585586
</ul>

test/gfm/list_items.unit

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,5 +581,6 @@ continued here.</p>
581581
<h1>Foo</h1>
582582
</li>
583583
<li>
584-
<h2>Bar</h2>baz</li>
584+
<h2>Bar</h2>
585+
baz</li>
585586
</ul>

tool/common_mark_stats.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@
565565
"297": "strict",
566566
"298": "strict",
567567
"299": "strict",
568-
"300": "loose"
568+
"300": "strict"
569569
},
570570
"Lists": {
571571
"301": "strict",

tool/common_mark_stats.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
3 of 3 – 100.0% Textual content
2626
19 of 19 – 100.0% Thematic breaks
2727
652 of 652 – 100.0% TOTAL
28-
643 of 652 – 98.6% TOTAL Strict
28+
644 of 652 – 98.8% TOTAL Strict

tool/gfm_stats.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@
578578
"275": "strict",
579579
"276": "strict",
580580
"277": "strict",
581-
"278": "loose"
581+
"278": "strict"
582582
},
583583
"Lists": {
584584
"281": "strict",

tool/gfm_stats.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929
3 of 3 – 100.0% Textual content
3030
19 of 19 – 100.0% Thematic breaks
3131
670 of 670 – 100.0% TOTAL
32-
657 of 670 – 98.1% TOTAL Strict
32+
658 of 670 – 98.2% TOTAL Strict

0 commit comments

Comments
 (0)