Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: loose list items are loose #2672

Merged
merged 3 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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