Skip to content

Commit 4ed3edb

Browse files
committed
fix: remove space token from last list item
1 parent 97278e4 commit 4ed3edb

File tree

5 files changed

+99
-22
lines changed

5 files changed

+99
-22
lines changed

lib/marked.esm.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -532,47 +532,61 @@ var Tokenizer_1 = class Tokenizer {
532532
addBack,
533533
loose,
534534
istask,
535-
ischecked;
535+
ischecked,
536+
endMatch;
536537

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

544+
if (!this.options.pedantic) {
545+
// Determine if current item contains the end of the list
546+
endMatch = item.match(new RegExp('\\n\\s*\\n {0,' + (bcurr[0].length - 1) + '}\\S'));
547+
if (endMatch) {
548+
addBack = item.length - endMatch.index + itemMatch.slice(i + 1).join('\n').length;
549+
list.raw = list.raw.substring(0, list.raw.length - addBack);
550+
551+
item = item.substring(0, endMatch.index);
552+
raw = item;
553+
l = i + 1;
554+
}
555+
}
556+
543557
// Determine whether the next list item belongs here.
544558
// Backpedal if it does not belong in this list.
545559
if (i !== l - 1) {
546560
bnext = this.rules.block.listItemStart.exec(itemMatch[i + 1]);
547561
if (
548562
!this.options.pedantic
549-
? bnext[1].length > bcurr[0].length || bnext[1].length > 3
563+
? bnext[1].length >= bcurr[0].length || bnext[1].length > 3
550564
: bnext[1].length > bcurr[1].length
551565
) {
552-
// nested list
553-
itemMatch.splice(i, 2, itemMatch[i] + '\n' + itemMatch[i + 1]);
566+
// nested list or continuation
567+
itemMatch.splice(i, 2, itemMatch[i] + (!this.options.pedantic && bnext[1].length < bcurr[0].length && !itemMatch[i].match(/\n$/) ? '' : '\n') + itemMatch[i + 1]);
554568
i--;
555569
l--;
556570
continue;
557-
} else {
558-
if (
559-
// different bullet style
560-
!this.options.pedantic || this.options.smartLists
561-
? bnext[2][bnext[2].length - 1] !== bull[bull.length - 1]
562-
: isordered === (bnext[2].length === 1)
563-
) {
564-
addBack = itemMatch.slice(i + 1).join('\n');
565-
list.raw = list.raw.substring(0, list.raw.length - addBack.length);
566-
i = l - 1;
567-
}
571+
} else if (
572+
// different bullet style
573+
!this.options.pedantic || this.options.smartLists
574+
? bnext[2][bnext[2].length - 1] !== bull[bull.length - 1]
575+
: isordered === (bnext[2].length === 1)
576+
) {
577+
addBack = itemMatch.slice(i + 1).join('\n').length;
578+
list.raw = list.raw.substring(0, list.raw.length - addBack);
579+
i = l - 1;
568580
}
569581
bcurr = bnext;
570582
}
571583

572584
// Remove the list item's bullet
573585
// so it is seen as the next token.
574586
space = item.length;
575-
item = item.replace(/^ *([*+-]|\d+[.)]) ?/, '');
587+
item = item.replace(/^ *([*+-]|\d+[.)]) ?|\n+$/g, '');
588+
589+
console.log([raw, item]);
576590

577591
// Outdent whatever the
578592
// list item contains. Hacky.
@@ -1072,7 +1086,7 @@ block.item = edit$1(block.item, 'gm')
10721086
.replace(/bull/g, block.bullet)
10731087
.getRegex();
10741088

1075-
block.listItemStart = edit$1(/^( *)(bull)/)
1089+
block.listItemStart = edit$1(/^( *)(bull) */)
10761090
.replace('bull', block.bullet)
10771091
.getRegex();
10781092

lib/marked.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@
647647
if (endMatch) {
648648
addBack = item.length - endMatch.index + itemMatch.slice(i + 1).join('\n').length;
649649
list.raw = list.raw.substring(0, list.raw.length - addBack);
650-
item = item.substring(0, endMatch.index + 1);
650+
item = item.substring(0, endMatch.index);
651651
raw = item;
652652
l = i + 1;
653653
}
@@ -677,7 +677,8 @@
677677

678678

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

683684
if (~item.indexOf('\n ')) {

marked.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Tokenizer.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ module.exports = class Tokenizer {
236236
addBack = item.length - endMatch.index + itemMatch.slice(i + 1).join('\n').length;
237237
list.raw = list.raw.substring(0, list.raw.length - addBack);
238238

239-
item = item.substring(0, endMatch.index + 1);
239+
item = item.substring(0, endMatch.index);
240240
raw = item;
241241
l = i + 1;
242242
}
@@ -272,7 +272,9 @@ module.exports = class Tokenizer {
272272
// Remove the list item's bullet
273273
// so it is seen as the next token.
274274
space = item.length;
275-
item = item.replace(/^ *([*+-]|\d+[.)]) ?/, '');
275+
item = item.replace(/^ *([*+-]|\d+[.)]) ?|\n+$/g, '');
276+
277+
console.log([raw, item]);
276278

277279
// Outdent whatever the
278280
// list item contains. Hacky.

test/unit/Lexer-spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,66 @@ a | b
390390
});
391391
});
392392

393+
fit('space after list', () => {
394+
expectTokens({
395+
md: `
396+
- item 1
397+
- item 2
398+
399+
paragraph
400+
`,
401+
tokens: [
402+
{
403+
type: 'list',
404+
raw: '- item 1\n- item 2\n\n',
405+
ordered: false,
406+
start: '',
407+
loose: false,
408+
items: [
409+
{
410+
type: 'list_item',
411+
raw: '- item 1',
412+
task: false,
413+
checked: undefined,
414+
loose: false,
415+
text: 'item 1',
416+
tokens: [{
417+
type: 'text',
418+
raw: 'item 1',
419+
text: 'item 1',
420+
tokens: [{ type: 'text', raw: 'item 1', text: 'item 1' }]
421+
}]
422+
},
423+
{
424+
type: 'list_item',
425+
raw: '- item 2\n\n',
426+
task: false,
427+
checked: undefined,
428+
loose: false,
429+
text: 'item 2',
430+
tokens: [{
431+
type: 'text',
432+
raw: 'item 2',
433+
text: 'item 2',
434+
tokens: [{ type: 'text', raw: 'item 2', text: 'item 2' }]
435+
}]
436+
}
437+
]
438+
},
439+
{
440+
type: 'paragraph',
441+
raw: 'paragraph',
442+
text: 'paragraph',
443+
tokens: [{
444+
type: 'text',
445+
raw: 'paragraph',
446+
text: 'paragraph'
447+
}]
448+
}
449+
]
450+
});
451+
});
452+
393453
it('start', () => {
394454
expectTokens({
395455
md: `

0 commit comments

Comments
 (0)