Skip to content

Commit

Permalink
fix: remove space token from last list item
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Feb 22, 2021
1 parent 97278e4 commit 4ed3edb
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 22 deletions.
48 changes: 31 additions & 17 deletions lib/marked.esm.js
Expand Up @@ -532,47 +532,61 @@ var Tokenizer_1 = class Tokenizer {
addBack,
loose,
istask,
ischecked;
ischecked,
endMatch;

let l = itemMatch.length;
bcurr = this.rules.block.listItemStart.exec(itemMatch[0]);
for (let i = 0; i < l; i++) {
item = itemMatch[i];
raw = item;

if (!this.options.pedantic) {
// Determine if current item contains the end of the list
endMatch = item.match(new RegExp('\\n\\s*\\n {0,' + (bcurr[0].length - 1) + '}\\S'));
if (endMatch) {
addBack = item.length - endMatch.index + itemMatch.slice(i + 1).join('\n').length;
list.raw = list.raw.substring(0, list.raw.length - addBack);

item = item.substring(0, endMatch.index);
raw = item;
l = i + 1;
}
}

// Determine whether the next list item belongs here.
// Backpedal if it does not belong in this list.
if (i !== l - 1) {
bnext = this.rules.block.listItemStart.exec(itemMatch[i + 1]);
if (
!this.options.pedantic
? bnext[1].length > bcurr[0].length || bnext[1].length > 3
? bnext[1].length >= bcurr[0].length || bnext[1].length > 3
: bnext[1].length > bcurr[1].length
) {
// nested list
itemMatch.splice(i, 2, itemMatch[i] + '\n' + itemMatch[i + 1]);
// nested list or continuation
itemMatch.splice(i, 2, itemMatch[i] + (!this.options.pedantic && bnext[1].length < bcurr[0].length && !itemMatch[i].match(/\n$/) ? '' : '\n') + itemMatch[i + 1]);
i--;
l--;
continue;
} else {
if (
// different bullet style
!this.options.pedantic || this.options.smartLists
? bnext[2][bnext[2].length - 1] !== bull[bull.length - 1]
: isordered === (bnext[2].length === 1)
) {
addBack = itemMatch.slice(i + 1).join('\n');
list.raw = list.raw.substring(0, list.raw.length - addBack.length);
i = l - 1;
}
} else if (
// different bullet style
!this.options.pedantic || this.options.smartLists
? bnext[2][bnext[2].length - 1] !== bull[bull.length - 1]
: isordered === (bnext[2].length === 1)
) {
addBack = itemMatch.slice(i + 1).join('\n').length;
list.raw = list.raw.substring(0, list.raw.length - addBack);
i = l - 1;
}
bcurr = bnext;
}

// Remove the list item's bullet
// so it is seen as the next token.
space = item.length;
item = item.replace(/^ *([*+-]|\d+[.)]) ?/, '');
item = item.replace(/^ *([*+-]|\d+[.)]) ?|\n+$/g, '');

console.log([raw, item]);

// Outdent whatever the
// list item contains. Hacky.
Expand Down Expand Up @@ -1072,7 +1086,7 @@ block.item = edit$1(block.item, 'gm')
.replace(/bull/g, block.bullet)
.getRegex();

block.listItemStart = edit$1(/^( *)(bull)/)
block.listItemStart = edit$1(/^( *)(bull) */)
.replace('bull', block.bullet)
.getRegex();

Expand Down
5 changes: 3 additions & 2 deletions lib/marked.js
Expand Up @@ -647,7 +647,7 @@
if (endMatch) {
addBack = item.length - endMatch.index + itemMatch.slice(i + 1).join('\n').length;
list.raw = list.raw.substring(0, list.raw.length - addBack);
item = item.substring(0, endMatch.index + 1);
item = item.substring(0, endMatch.index);
raw = item;
l = i + 1;
}
Expand Down Expand Up @@ -677,7 +677,8 @@


space = item.length;
item = item.replace(/^ *([*+-]|\d+[.)]) ?/, ''); // Outdent whatever the
item = item.replace(/^ *([*+-]|\d+[.)]) ?|\n+$/g, '');
console.log([raw, item]); // Outdent whatever the
// list item contains. Hacky.

if (~item.indexOf('\n ')) {
Expand Down
2 changes: 1 addition & 1 deletion marked.min.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/Tokenizer.js
Expand Up @@ -236,7 +236,7 @@ module.exports = class Tokenizer {
addBack = item.length - endMatch.index + itemMatch.slice(i + 1).join('\n').length;
list.raw = list.raw.substring(0, list.raw.length - addBack);

item = item.substring(0, endMatch.index + 1);
item = item.substring(0, endMatch.index);
raw = item;
l = i + 1;
}
Expand Down Expand Up @@ -272,7 +272,9 @@ module.exports = class Tokenizer {
// Remove the list item's bullet
// so it is seen as the next token.
space = item.length;
item = item.replace(/^ *([*+-]|\d+[.)]) ?/, '');
item = item.replace(/^ *([*+-]|\d+[.)]) ?|\n+$/g, '');

console.log([raw, item]);

// Outdent whatever the
// list item contains. Hacky.
Expand Down
60 changes: 60 additions & 0 deletions test/unit/Lexer-spec.js
Expand Up @@ -390,6 +390,66 @@ a | b
});
});

fit('space after list', () => {
expectTokens({
md: `
- item 1
- item 2
paragraph
`,
tokens: [
{
type: 'list',
raw: '- item 1\n- item 2\n\n',
ordered: false,
start: '',
loose: false,
items: [
{
type: 'list_item',
raw: '- item 1',
task: false,
checked: undefined,
loose: false,
text: 'item 1',
tokens: [{
type: 'text',
raw: 'item 1',
text: 'item 1',
tokens: [{ type: 'text', raw: 'item 1', text: 'item 1' }]
}]
},
{
type: 'list_item',
raw: '- item 2\n\n',
task: false,
checked: undefined,
loose: false,
text: 'item 2',
tokens: [{
type: 'text',
raw: 'item 2',
text: 'item 2',
tokens: [{ type: 'text', raw: 'item 2', text: 'item 2' }]
}]
}
]
},
{
type: 'paragraph',
raw: 'paragraph',
text: 'paragraph',
tokens: [{
type: 'text',
raw: 'paragraph',
text: 'paragraph'
}]
}
]
});
});

it('start', () => {
expectTokens({
md: `
Expand Down

0 comments on commit 4ed3edb

Please sign in to comment.