Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Check for line breaks not just spaces
  • Loading branch information
phillipb committed Dec 31, 2021
commit 1317eb0f78fb7bb1cbe9695679c71d0e9a5f7e01
18 changes: 16 additions & 2 deletions src/Tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,22 @@ export class Tokenizer {
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 hasMultipleSpaces = spacers.every(t => t.raw.length > 1);
if (!list.loose && spacers.length && hasMultipleSpaces) {
const hasMultipleLineBreaks = spacers.every(t => {
const chars = t.raw.split('');
let lineBreaks = 0;
for (let char of chars) {
if (char === '\n') {
lineBreaks += 1;
}
if (lineBreaks > 1) {
return true
}
}

return false
});

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.items[i].loose = true;
Expand Down
29 changes: 29 additions & 0 deletions test/unit/Lexer-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,35 @@ paragraph
});
});

it('not loose with spaces', () => {
expectTokens({
md: `- item 1
- item 2
`,
tokens: jasmine.arrayContaining([
jasmine.objectContaining({
type: 'list',
raw: '- item 1\n - item 2\n',
loose: false,
items: [
jasmine.objectContaining({
raw: '- item 1\n - item 2',
tokens: jasmine.arrayContaining([
jasmine.objectContaining({
raw: 'item 1\n'
}),
jasmine.objectContaining({
type: 'list',
raw: '- item 2'
}),
])
})
]
})
])
});
});

it('task', () => {
expectTokens({
md: `- [ ] item 1
Expand Down