Skip to content

Commit 149daa6

Browse files
committed
fix: remove space token from list items
1 parent 97278e4 commit 149daa6

6 files changed

Lines changed: 108 additions & 29 deletions

File tree

lib/marked.esm.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -532,39 +532,51 @@ 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
}
@@ -583,12 +595,15 @@ var Tokenizer_1 = class Tokenizer {
583595
: item.replace(/^ {1,4}/gm, '');
584596
}
585597

598+
// trim item newlines at end
599+
item = item.replace(/\n+$/, '');
600+
586601
// Determine whether item is loose or not.
587602
// Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
588603
// for discount behavior.
589-
loose = next || /\n\n(?!\s*$)/.test(item);
604+
loose = next || /\n\n(?!\s*$)/.test(raw);
590605
if (i !== l - 1) {
591-
next = item.charAt(item.length - 1) === '\n';
606+
next = raw.charAt(raw.length - 1) === '\n';
592607
if (!loose) loose = next;
593608
}
594609

@@ -1072,7 +1087,7 @@ block.item = edit$1(block.item, 'gm')
10721087
.replace(/bull/g, block.bullet)
10731088
.getRegex();
10741089

1075-
block.listItemStart = edit$1(/^( *)(bull)/)
1090+
block.listItemStart = edit$1(/^( *)(bull) */)
10761091
.replace('bull', block.bullet)
10771092
.getRegex();
10781093

lib/marked.js

Lines changed: 7 additions & 5 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
}
@@ -683,15 +683,17 @@
683683
if (~item.indexOf('\n ')) {
684684
space -= item.length;
685685
item = !this.options.pedantic ? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '') : item.replace(/^ {1,4}/gm, '');
686-
} // Determine whether item is loose or not.
686+
} // trim item newlines at end
687+
688+
689+
item = item.replace(/\n+$/, ''); // Determine whether item is loose or not.
687690
// Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
688691
// for discount behavior.
689692

690-
691-
loose = next || /\n\n(?!\s*$)/.test(item);
693+
loose = next || /\n\n(?!\s*$)/.test(raw);
692694

693695
if (i !== l - 1) {
694-
next = item.charAt(item.length - 1) === '\n';
696+
next = raw.charAt(raw.length - 1) === '\n';
695697
if (!loose) loose = next;
696698
}
697699

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: 6 additions & 3 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
}
@@ -283,12 +283,15 @@ module.exports = class Tokenizer {
283283
: item.replace(/^ {1,4}/gm, '');
284284
}
285285

286+
// trim item newlines at end
287+
item = item.replace(/\n+$/, '');
288+
286289
// Determine whether item is loose or not.
287290
// Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
288291
// for discount behavior.
289-
loose = next || /\n\n(?!\s*$)/.test(item);
292+
loose = next || /\n\n(?!\s*$)/.test(raw);
290293
if (i !== l - 1) {
291-
next = item.charAt(item.length - 1) === '\n';
294+
next = raw.charAt(raw.length - 1) === '\n';
292295
if (!loose) loose = next;
293296
}
294297

test/unit/Lexer-spec.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ a | b
326326
task: false,
327327
checked: undefined,
328328
loose: false,
329-
text: 'item 2\n',
329+
text: 'item 2',
330330
tokens: [{
331331
type: 'text',
332332
raw: 'item 2',
@@ -390,6 +390,66 @@ a | b
390390
});
391391
});
392392

393+
it('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: `

test/unit/marked-spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,6 @@ br
465465
['list_item', '- list'],
466466
['text', 'list'],
467467
['text', 'list'],
468-
['space', ''],
469468
['html', '<div>html</div>'],
470469
['paragraph', '[link](https://example.com)'],
471470
['link', '[link](https://example.com)'],

0 commit comments

Comments
 (0)