Skip to content

Commit

Permalink
fix: loose list items are loose (#2672)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech authored Dec 7, 2022
1 parent bd9a114 commit df4eb0e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 19 deletions.
28 changes: 11 additions & 17 deletions src/Tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,25 +315,19 @@ export class Tokenizer {
for (i = 0; i < l; i++) {
this.lexer.state.top = false;
list.items[i].tokens = this.lexer.blockTokens(list.items[i].text, []);
const spacers = list.items[i].tokens.filter(t => t.type === 'space');
const hasMultipleLineBreaks = spacers.every(t => {
const chars = t.raw.split('');
let lineBreaks = 0;
for (const char of chars) {
if (char === '\n') {
lineBreaks += 1;
}
if (lineBreaks > 1) {
return true;
}
}

return false;
});
if (!list.loose) {
// Check if list should be loose
const spacers = list.items[i].tokens.filter(t => t.type === 'space');
const hasMultipleLineBreaks = spacers.length > 0 && spacers.some(t => /\n.*\n/.test(t.raw));

if (!list.loose && spacers.length && hasMultipleLineBreaks) {
// Having a single line break doesn't mean a list is loose. A single line break is terminating the last list item
list.loose = true;
list.loose = hasMultipleLineBreaks;
}
}

// Set all items to loose if list is loose
if (list.loose) {
for (i = 0; i < l; i++) {
list.items[i].loose = true;
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/specs/new/list_loose.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<ul>
<li>
<p>item 1</p>
</li>
<li>
<p>item 2</p>
<p>still item 2</p>
</li>
</ul>
5 changes: 5 additions & 0 deletions test/specs/new/list_loose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- item 1
-
item 2

still item 2
44 changes: 42 additions & 2 deletions test/unit/Lexer-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,10 +605,49 @@ paragraph
loose: true,
items: [
jasmine.objectContaining({
raw: '- item 1\n\n'
raw: '- item 1\n\n',
loose: true
}),
jasmine.objectContaining({
raw: '- item 2'
raw: '- item 2',
loose: true
})
]
})
])
});
});

it('end loose', () => {
expectTokens({
md: `
- item 1
- item 2
item 2a
- item 3
`,
tokens: jasmine.arrayContaining([
jasmine.objectContaining({
type: 'space',
raw: '\n'
}),
jasmine.objectContaining({
type: 'list',
raw: '- item 1\n- item 2\n\n item 2a\n- item 3\n',
loose: true,
items: [
jasmine.objectContaining({
raw: '- item 1\n',
loose: true
}),
jasmine.objectContaining({
raw: '- item 2\n\n item 2a\n',
loose: true
}),
jasmine.objectContaining({
raw: '- item 3',
loose: true
})
]
})
Expand All @@ -634,6 +673,7 @@ paragraph
items: [
jasmine.objectContaining({
raw: '- item 1\n - item 2',
loose: false,
tokens: jasmine.arrayContaining([
jasmine.objectContaining({
raw: 'item 1\n'
Expand Down

1 comment on commit df4eb0e

@vercel
Copy link

@vercel vercel bot commented on df4eb0e Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.