marked 2.1.3 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/marked.js CHANGED
@@ -284,7 +284,15 @@
284
284
  }
285
285
  }),
286
286
  cells = row.split(/ \|/);
287
- var i = 0;
287
+ var i = 0; // First/last cell in a row cannot be empty if it has no leading/trailing pipe
288
+
289
+ if (!cells[0].trim()) {
290
+ cells.shift();
291
+ }
292
+
293
+ if (!cells[cells.length - 1].trim()) {
294
+ cells.pop();
295
+ }
288
296
 
289
297
  if (cells.length > count) {
290
298
  cells.splice(count);
@@ -403,19 +411,23 @@
403
411
  _escape = helpers.escape,
404
412
  findClosingBracket = helpers.findClosingBracket;
405
413
 
406
- function outputLink(cap, link, raw) {
414
+ function outputLink(cap, link, raw, lexer) {
407
415
  var href = link.href;
408
416
  var title = link.title ? _escape(link.title) : null;
409
417
  var text = cap[1].replace(/\\([\[\]])/g, '$1');
410
418
 
411
419
  if (cap[0].charAt(0) !== '!') {
412
- return {
420
+ lexer.state.inLink = true;
421
+ var token = {
413
422
  type: 'link',
414
423
  raw: raw,
415
424
  href: href,
416
425
  title: title,
417
- text: text
426
+ text: text,
427
+ tokens: lexer.inlineTokens(text, [])
418
428
  };
429
+ lexer.state.inLink = false;
430
+ return token;
419
431
  } else {
420
432
  return {
421
433
  type: 'image',
@@ -526,51 +538,15 @@
526
538
  }
527
539
  }
528
540
 
529
- return {
541
+ var token = {
530
542
  type: 'heading',
531
543
  raw: cap[0],
532
544
  depth: cap[1].length,
533
- text: text
534
- };
535
- }
536
- };
537
-
538
- _proto.nptable = function nptable(src) {
539
- var cap = this.rules.block.nptable.exec(src);
540
-
541
- if (cap) {
542
- var item = {
543
- type: 'table',
544
- header: splitCells(cap[1].replace(/^ *| *\| *$/g, '')),
545
- align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
546
- cells: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : [],
547
- raw: cap[0]
545
+ text: text,
546
+ tokens: []
548
547
  };
549
-
550
- if (item.header.length === item.align.length) {
551
- var l = item.align.length;
552
- var i;
553
-
554
- for (i = 0; i < l; i++) {
555
- if (/^ *-+: *$/.test(item.align[i])) {
556
- item.align[i] = 'right';
557
- } else if (/^ *:-+: *$/.test(item.align[i])) {
558
- item.align[i] = 'center';
559
- } else if (/^ *:-+ *$/.test(item.align[i])) {
560
- item.align[i] = 'left';
561
- } else {
562
- item.align[i] = null;
563
- }
564
- }
565
-
566
- l = item.cells.length;
567
-
568
- for (i = 0; i < l; i++) {
569
- item.cells[i] = splitCells(item.cells[i], item.header.length);
570
- }
571
-
572
- return item;
573
- }
548
+ this.lexer.inline(token.text, token.tokens);
549
+ return token;
574
550
  }
575
551
  };
576
552
 
@@ -593,6 +569,7 @@
593
569
  return {
594
570
  type: 'blockquote',
595
571
  raw: cap[0],
572
+ tokens: this.lexer.blockTokens(text, []),
596
573
  text: text
597
574
  };
598
575
  }
@@ -602,121 +579,150 @@
602
579
  var cap = this.rules.block.list.exec(src);
603
580
 
604
581
  if (cap) {
605
- var raw = cap[0];
606
- var bull = cap[2];
582
+ var raw, istask, ischecked, indent, i, blankLine, endsWithBlankLine, line, lines, itemContents;
583
+ var bull = cap[1].trim();
607
584
  var isordered = bull.length > 1;
608
585
  var list = {
609
586
  type: 'list',
610
- raw: raw,
587
+ raw: '',
611
588
  ordered: isordered,
612
589
  start: isordered ? +bull.slice(0, -1) : '',
613
590
  loose: false,
614
591
  items: []
615
- }; // Get each top-level item.
616
-
617
- var itemMatch = cap[0].match(this.rules.block.item);
618
- var next = false,
619
- item,
620
- space,
621
- bcurr,
622
- bnext,
623
- addBack,
624
- loose,
625
- istask,
626
- ischecked,
627
- endMatch;
628
- var l = itemMatch.length;
629
- bcurr = this.rules.block.listItemStart.exec(itemMatch[0]);
630
-
631
- for (var i = 0; i < l; i++) {
632
- item = itemMatch[i];
633
- raw = item;
634
-
635
- if (!this.options.pedantic) {
636
- // Determine if current item contains the end of the list
637
- endMatch = item.match(new RegExp('\\n\\s*\\n {0,' + (bcurr[0].length - 1) + '}\\S'));
638
-
639
- if (endMatch) {
640
- addBack = item.length - endMatch.index + itemMatch.slice(i + 1).join('\n').length;
641
- list.raw = list.raw.substring(0, list.raw.length - addBack);
642
- item = item.substring(0, endMatch.index);
643
- raw = item;
644
- l = i + 1;
645
- }
646
- } // Determine whether the next list item belongs here.
647
- // Backpedal if it does not belong in this list.
592
+ };
593
+ bull = isordered ? "\\d{1,9}\\" + bull.slice(-1) : "\\" + bull;
648
594
 
595
+ if (this.options.pedantic) {
596
+ bull = isordered ? bull : '[*+-]';
597
+ } // Get next list item
649
598
 
650
- if (i !== l - 1) {
651
- bnext = this.rules.block.listItemStart.exec(itemMatch[i + 1]);
652
599
 
653
- if (!this.options.pedantic ? bnext[1].length >= bcurr[0].length || bnext[1].length > 3 : bnext[1].length > bcurr[1].length) {
654
- // nested list or continuation
655
- itemMatch.splice(i, 2, itemMatch[i] + (!this.options.pedantic && bnext[1].length < bcurr[0].length && !itemMatch[i].match(/\n$/) ? '' : '\n') + itemMatch[i + 1]);
656
- i--;
657
- l--;
658
- continue;
659
- } else if ( // different bullet style
660
- !this.options.pedantic || this.options.smartLists ? bnext[2][bnext[2].length - 1] !== bull[bull.length - 1] : isordered === (bnext[2].length === 1)) {
661
- addBack = itemMatch.slice(i + 1).join('\n').length;
662
- list.raw = list.raw.substring(0, list.raw.length - addBack);
663
- i = l - 1;
664
- }
600
+ var itemRegex = new RegExp("^( {0,3}" + bull + ")((?: [^\\n]*| *)(?:\\n[^\\n]*)*(?:\\n|$))"); // Get each top-level item
601
+
602
+ while (src) {
603
+ if (this.rules.block.hr.test(src)) {
604
+ // End list if we encounter an HR (possibly move into itemRegex?)
605
+ break;
606
+ }
607
+
608
+ if (!(cap = itemRegex.exec(src))) {
609
+ break;
610
+ }
665
611
 
666
- bcurr = bnext;
667
- } // Remove the list item's bullet
668
- // so it is seen as the next token.
612
+ lines = cap[2].split('\n');
669
613
 
614
+ if (this.options.pedantic) {
615
+ indent = 2;
616
+ itemContents = lines[0].trimLeft();
617
+ } else {
618
+ indent = cap[2].search(/[^ ]/); // Find first non-space char
670
619
 
671
- space = item.length;
672
- item = item.replace(/^ *([*+-]|\d+[.)]) ?/, ''); // Outdent whatever the
673
- // list item contains. Hacky.
620
+ indent = cap[1].length + (indent > 4 ? 1 : indent); // intented code blocks after 4 spaces; indent is always 1
674
621
 
675
- if (~item.indexOf('\n ')) {
676
- space -= item.length;
677
- item = !this.options.pedantic ? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '') : item.replace(/^ {1,4}/gm, '');
678
- } // trim item newlines at end
622
+ itemContents = lines[0].slice(indent - cap[1].length);
623
+ }
679
624
 
625
+ blankLine = false;
626
+ raw = cap[0];
680
627
 
681
- item = rtrim(item, '\n');
628
+ if (!lines[0] && /^ *$/.test(lines[1])) {
629
+ // items begin with at most one blank line
630
+ raw = cap[1] + lines.slice(0, 2).join('\n') + '\n';
631
+ list.loose = true;
632
+ lines = [];
633
+ }
682
634
 
683
- if (i !== l - 1) {
684
- raw = raw + '\n';
685
- } // Determine whether item is loose or not.
686
- // Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
687
- // for discount behavior.
635
+ var nextBulletRegex = new RegExp("^ {0," + Math.min(3, indent - 1) + "}(?:[*+-]|\\d{1,9}[.)])");
636
+
637
+ for (i = 1; i < lines.length; i++) {
638
+ line = lines[i];
639
+
640
+ if (this.options.pedantic) {
641
+ // Re-align to follow commonmark nesting rules
642
+ line = line.replace(/^ {1,4}(?=( {4})*[^ ])/g, ' ');
643
+ } // End list item if found start of new bullet
644
+
645
+
646
+ if (nextBulletRegex.test(line)) {
647
+ raw = cap[1] + lines.slice(0, i).join('\n') + '\n';
648
+ break;
649
+ } // Until we encounter a blank line, item contents do not need indentation
650
+
651
+
652
+ if (!blankLine) {
653
+ if (!line.trim()) {
654
+ // Check if current line is empty
655
+ blankLine = true;
656
+ } // Dedent if possible
657
+
658
+
659
+ if (line.search(/[^ ]/) >= indent) {
660
+ itemContents += '\n' + line.slice(indent);
661
+ } else {
662
+ itemContents += '\n' + line;
663
+ }
688
664
 
665
+ continue;
666
+ } // Dedent this line
689
667
 
690
- loose = next || /\n\n(?!\s*$)/.test(raw);
691
668
 
692
- if (i !== l - 1) {
693
- next = raw.slice(-2) === '\n\n';
694
- if (!loose) loose = next;
669
+ if (line.search(/[^ ]/) >= indent || !line.trim()) {
670
+ itemContents += '\n' + line.slice(indent);
671
+ continue;
672
+ } else {
673
+ // Line was not properly indented; end of this item
674
+ raw = cap[1] + lines.slice(0, i).join('\n') + '\n';
675
+ break;
676
+ }
695
677
  }
696
678
 
697
- if (loose) {
698
- list.loose = true;
679
+ if (!list.loose) {
680
+ // If the previous item ended with a blank line, the list is loose
681
+ if (endsWithBlankLine) {
682
+ list.loose = true;
683
+ } else if (/\n *\n *$/.test(raw)) {
684
+ endsWithBlankLine = true;
685
+ }
699
686
  } // Check for task list items
700
687
 
701
688
 
702
689
  if (this.options.gfm) {
703
- istask = /^\[[ xX]\] /.test(item);
704
- ischecked = undefined;
690
+ istask = /^\[[ xX]\] /.exec(itemContents);
705
691
 
706
692
  if (istask) {
707
- ischecked = item[1] !== ' ';
708
- item = item.replace(/^\[[ xX]\] +/, '');
693
+ ischecked = istask[0] !== '[ ] ';
694
+ itemContents = itemContents.replace(/^\[[ xX]\] +/, '');
709
695
  }
710
696
  }
711
697
 
712
698
  list.items.push({
713
699
  type: 'list_item',
714
700
  raw: raw,
715
- task: istask,
701
+ task: !!istask,
716
702
  checked: ischecked,
717
- loose: loose,
718
- text: item
703
+ loose: false,
704
+ text: itemContents
719
705
  });
706
+ list.raw += raw;
707
+ src = src.slice(raw.length);
708
+ } // Do not consume newlines at end of final item. Alternatively, make itemRegex *start* with any newlines to simplify/speed up endsWithBlankLine logic
709
+
710
+
711
+ list.items[list.items.length - 1].raw = raw.trimRight();
712
+ list.items[list.items.length - 1].text = itemContents.trimRight();
713
+ list.raw = list.raw.trimRight();
714
+ var l = list.items.length; // Item child tokens handled here at end because we needed to have the final item to trim it first
715
+
716
+ for (i = 0; i < l; i++) {
717
+ this.lexer.state.top = false;
718
+ list.items[i].tokens = this.lexer.blockTokens(list.items[i].text, []);
719
+
720
+ if (list.items[i].tokens.some(function (t) {
721
+ return t.type === 'space';
722
+ })) {
723
+ list.loose = true;
724
+ list.items[i].loose = true;
725
+ }
720
726
  }
721
727
 
722
728
  return list;
@@ -727,12 +733,21 @@
727
733
  var cap = this.rules.block.html.exec(src);
728
734
 
729
735
  if (cap) {
730
- return {
731
- type: this.options.sanitize ? 'paragraph' : 'html',
736
+ var token = {
737
+ type: 'html',
732
738
  raw: cap[0],
733
739
  pre: !this.options.sanitizer && (cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style'),
734
- text: this.options.sanitize ? this.options.sanitizer ? this.options.sanitizer(cap[0]) : _escape(cap[0]) : cap[0]
740
+ text: cap[0]
735
741
  };
742
+
743
+ if (this.options.sanitize) {
744
+ token.type = 'paragraph';
745
+ token.text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : _escape(cap[0]);
746
+ token.tokens = [];
747
+ this.lexer.inline(token.text, token.tokens);
748
+ }
749
+
750
+ return token;
736
751
  }
737
752
  };
738
753
 
@@ -758,15 +773,19 @@
758
773
  if (cap) {
759
774
  var item = {
760
775
  type: 'table',
761
- header: splitCells(cap[1].replace(/^ *| *\| *$/g, '')),
776
+ header: splitCells(cap[1]).map(function (c) {
777
+ return {
778
+ text: c
779
+ };
780
+ }),
762
781
  align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
763
- cells: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : []
782
+ rows: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : []
764
783
  };
765
784
 
766
785
  if (item.header.length === item.align.length) {
767
786
  item.raw = cap[0];
768
787
  var l = item.align.length;
769
- var i;
788
+ var i, j, k, row;
770
789
 
771
790
  for (i = 0; i < l; i++) {
772
791
  if (/^ *-+: *$/.test(item.align[i])) {
@@ -780,10 +799,35 @@
780
799
  }
781
800
  }
782
801
 
783
- l = item.cells.length;
802
+ l = item.rows.length;
784
803
 
785
804
  for (i = 0; i < l; i++) {
786
- item.cells[i] = splitCells(item.cells[i].replace(/^ *\| *| *\| *$/g, ''), item.header.length);
805
+ item.rows[i] = splitCells(item.rows[i], item.header.length).map(function (c) {
806
+ return {
807
+ text: c
808
+ };
809
+ });
810
+ } // parse child tokens inside headers and cells
811
+ // header child tokens
812
+
813
+
814
+ l = item.header.length;
815
+
816
+ for (j = 0; j < l; j++) {
817
+ item.header[j].tokens = [];
818
+ this.lexer.inlineTokens(item.header[j].text, item.header[j].tokens);
819
+ } // cell child tokens
820
+
821
+
822
+ l = item.rows.length;
823
+
824
+ for (j = 0; j < l; j++) {
825
+ row = item.rows[j];
826
+
827
+ for (k = 0; k < row.length; k++) {
828
+ row[k].tokens = [];
829
+ this.lexer.inlineTokens(row[k].text, row[k].tokens);
830
+ }
787
831
  }
788
832
 
789
833
  return item;
@@ -795,12 +839,15 @@
795
839
  var cap = this.rules.block.lheading.exec(src);
796
840
 
797
841
  if (cap) {
798
- return {
842
+ var token = {
799
843
  type: 'heading',
800
844
  raw: cap[0],
801
845
  depth: cap[2].charAt(0) === '=' ? 1 : 2,
802
- text: cap[1]
846
+ text: cap[1],
847
+ tokens: []
803
848
  };
849
+ this.lexer.inline(token.text, token.tokens);
850
+ return token;
804
851
  }
805
852
  };
806
853
 
@@ -808,11 +855,14 @@
808
855
  var cap = this.rules.block.paragraph.exec(src);
809
856
 
810
857
  if (cap) {
811
- return {
858
+ var token = {
812
859
  type: 'paragraph',
813
860
  raw: cap[0],
814
- text: cap[1].charAt(cap[1].length - 1) === '\n' ? cap[1].slice(0, -1) : cap[1]
861
+ text: cap[1].charAt(cap[1].length - 1) === '\n' ? cap[1].slice(0, -1) : cap[1],
862
+ tokens: []
815
863
  };
864
+ this.lexer.inline(token.text, token.tokens);
865
+ return token;
816
866
  }
817
867
  };
818
868
 
@@ -820,11 +870,14 @@
820
870
  var cap = this.rules.block.text.exec(src);
821
871
 
822
872
  if (cap) {
823
- return {
873
+ var token = {
824
874
  type: 'text',
825
875
  raw: cap[0],
826
- text: cap[0]
876
+ text: cap[0],
877
+ tokens: []
827
878
  };
879
+ this.lexer.inline(token.text, token.tokens);
880
+ return token;
828
881
  }
829
882
  };
830
883
 
@@ -840,27 +893,27 @@
840
893
  }
841
894
  };
842
895
 
843
- _proto.tag = function tag(src, inLink, inRawBlock) {
896
+ _proto.tag = function tag(src) {
844
897
  var cap = this.rules.inline.tag.exec(src);
845
898
 
846
899
  if (cap) {
847
- if (!inLink && /^<a /i.test(cap[0])) {
848
- inLink = true;
849
- } else if (inLink && /^<\/a>/i.test(cap[0])) {
850
- inLink = false;
900
+ if (!this.lexer.state.inLink && /^<a /i.test(cap[0])) {
901
+ this.lexer.state.inLink = true;
902
+ } else if (this.lexer.state.inLink && /^<\/a>/i.test(cap[0])) {
903
+ this.lexer.state.inLink = false;
851
904
  }
852
905
 
853
- if (!inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
854
- inRawBlock = true;
855
- } else if (inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
856
- inRawBlock = false;
906
+ if (!this.lexer.state.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
907
+ this.lexer.state.inRawBlock = true;
908
+ } else if (this.lexer.state.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
909
+ this.lexer.state.inRawBlock = false;
857
910
  }
858
911
 
859
912
  return {
860
913
  type: this.options.sanitize ? 'text' : 'html',
861
914
  raw: cap[0],
862
- inLink: inLink,
863
- inRawBlock: inRawBlock,
915
+ inLink: this.lexer.state.inLink,
916
+ inRawBlock: this.lexer.state.inRawBlock,
864
917
  text: this.options.sanitize ? this.options.sanitizer ? this.options.sanitizer(cap[0]) : _escape(cap[0]) : cap[0]
865
918
  };
866
919
  }
@@ -926,7 +979,7 @@
926
979
  return outputLink(cap, {
927
980
  href: href ? href.replace(this.rules.inline._escapes, '$1') : href,
928
981
  title: title ? title.replace(this.rules.inline._escapes, '$1') : title
929
- }, cap[0]);
982
+ }, cap[0], this.lexer);
930
983
  }
931
984
  };
932
985
 
@@ -946,7 +999,7 @@
946
999
  };
947
1000
  }
948
1001
 
949
- return outputLink(cap, link, cap[0]);
1002
+ return outputLink(cap, link, cap[0], this.lexer);
950
1003
  }
951
1004
  };
952
1005
 
@@ -997,18 +1050,23 @@
997
1050
  rLength = Math.min(rLength, rLength + delimTotal + midDelimTotal); // Create `em` if smallest delimiter has odd char count. *a***
998
1051
 
999
1052
  if (Math.min(lLength, rLength) % 2) {
1053
+ var _text = src.slice(1, lLength + match.index + rLength);
1054
+
1000
1055
  return {
1001
1056
  type: 'em',
1002
1057
  raw: src.slice(0, lLength + match.index + rLength + 1),
1003
- text: src.slice(1, lLength + match.index + rLength)
1058
+ text: _text,
1059
+ tokens: this.lexer.inlineTokens(_text, [])
1004
1060
  };
1005
1061
  } // Create 'strong' if smallest delimiter has even char count. **a***
1006
1062
 
1007
1063
 
1064
+ var text = src.slice(2, lLength + match.index + rLength - 1);
1008
1065
  return {
1009
1066
  type: 'strong',
1010
1067
  raw: src.slice(0, lLength + match.index + rLength + 1),
1011
- text: src.slice(2, lLength + match.index + rLength - 1)
1068
+ text: text,
1069
+ tokens: this.lexer.inlineTokens(text, [])
1012
1070
  };
1013
1071
  }
1014
1072
  }
@@ -1053,7 +1111,8 @@
1053
1111
  return {
1054
1112
  type: 'del',
1055
1113
  raw: cap[0],
1056
- text: cap[2]
1114
+ text: cap[2],
1115
+ tokens: this.lexer.inlineTokens(cap[2], [])
1057
1116
  };
1058
1117
  }
1059
1118
  };
@@ -1127,13 +1186,13 @@
1127
1186
  }
1128
1187
  };
1129
1188
 
1130
- _proto.inlineText = function inlineText(src, inRawBlock, smartypants) {
1189
+ _proto.inlineText = function inlineText(src, smartypants) {
1131
1190
  var cap = this.rules.inline.text.exec(src);
1132
1191
 
1133
1192
  if (cap) {
1134
1193
  var text;
1135
1194
 
1136
- if (inRawBlock) {
1195
+ if (this.lexer.state.inRawBlock) {
1137
1196
  text = this.options.sanitize ? this.options.sanitizer ? this.options.sanitizer(cap[0]) : _escape(cap[0]) : cap[0];
1138
1197
  } else {
1139
1198
  text = _escape(this.options.smartypants ? smartypants(cap[0]) : cap[0]);
@@ -1160,11 +1219,11 @@
1160
1219
  var block$1 = {
1161
1220
  newline: /^(?: *(?:\n|$))+/,
1162
1221
  code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
1163
- fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/,
1222
+ fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
1164
1223
  hr: /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,
1165
1224
  heading: /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,
1166
1225
  blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
1167
- list: /^( {0,3})(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?! {0,3}bull )\n*|\s*$)/,
1226
+ list: /^( {0,3}bull)( [^\n]+?)?(?:\n|$)/,
1168
1227
  html: '^ {0,3}(?:' // optional indentation
1169
1228
  + '<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)' // (1)
1170
1229
  + '|comment[^\\n]*(\\n+|$)' // (2)
@@ -1176,7 +1235,6 @@
1176
1235
  + '|</(?!script|pre|style|textarea)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (7) closing tag
1177
1236
  + ')',
1178
1237
  def: /^ {0,3}\[(label)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)(title))? *(?:\n+|$)/,
1179
- nptable: noopTest,
1180
1238
  table: noopTest,
1181
1239
  lheading: /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,
1182
1240
  // regex template, placeholders will be replaced according to different paragraph
@@ -1188,8 +1246,6 @@
1188
1246
  block$1._title = /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/;
1189
1247
  block$1.def = edit(block$1.def).replace('label', block$1._label).replace('title', block$1._title).getRegex();
1190
1248
  block$1.bullet = /(?:[*+-]|\d{1,9}[.)])/;
1191
- block$1.item = /^( *)(bull) ?[^\n]*(?:\n(?! *bull ?)[^\n]*)*/;
1192
- block$1.item = edit(block$1.item, 'gm').replace(/bull/g, block$1.bullet).getRegex();
1193
1249
  block$1.listItemStart = edit(/^( *)(bull) */).replace('bull', block$1.bullet).getRegex();
1194
1250
  block$1.list = edit(block$1.list).replace(/bull/g, block$1.bullet).replace('hr', '\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))').replace('def', '\\n+(?=' + block$1.def.source + ')').getRegex();
1195
1251
  block$1._tag = 'address|article|aside|base|basefont|blockquote|body|caption' + '|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption' + '|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe' + '|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option' + '|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr' + '|track|ul';
@@ -1210,18 +1266,11 @@
1210
1266
  */
1211
1267
 
1212
1268
  block$1.gfm = merge$1({}, block$1.normal, {
1213
- nptable: '^ *([^|\\n ].*\\|.*)\\n' // Header
1214
- + ' {0,3}([-:]+ *\\|[-| :]*)' // Align
1215
- + '(?:\\n((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)',
1216
- // Cells
1217
- table: '^ *\\|(.+)\\n' // Header
1218
- + ' {0,3}\\|?( *[-:]+[-| :]*)' // Align
1219
- + '(?:\\n *((?:(?!\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
1269
+ table: '^ *([^\\n ].*\\|.*)\\n' // Header
1270
+ + ' {0,3}(?:\\| *)?(:?-+:? *(?:\\| *:?-+:? *)*)(?:\\| *)?' // Align
1271
+ + '(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
1220
1272
 
1221
1273
  });
1222
- block$1.gfm.nptable = edit(block$1.gfm.nptable).replace('hr', block$1.hr).replace('heading', ' {0,3}#{1,6} ').replace('blockquote', ' {0,3}>').replace('code', ' {4}[^\\n]').replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n').replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
1223
- .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)').replace('tag', block$1._tag) // tables can be interrupted by type (6) html blocks
1224
- .getRegex();
1225
1274
  block$1.gfm.table = edit(block$1.gfm.table).replace('hr', block$1.hr).replace('heading', ' {0,3}#{1,6} ').replace('blockquote', ' {0,3}>').replace('code', ' {4}[^\\n]').replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n').replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
1226
1275
  .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)').replace('tag', block$1._tag) // tables can be interrupted by type (6) html blocks
1227
1276
  .getRegex();
@@ -1399,6 +1448,13 @@
1399
1448
  this.options.tokenizer = this.options.tokenizer || new Tokenizer$1();
1400
1449
  this.tokenizer = this.options.tokenizer;
1401
1450
  this.tokenizer.options = this.options;
1451
+ this.tokenizer.lexer = this;
1452
+ this.inlineQueue = [];
1453
+ this.state = {
1454
+ inLink: false,
1455
+ inRawBlock: false,
1456
+ top: true
1457
+ };
1402
1458
  var rules = {
1403
1459
  block: block.normal,
1404
1460
  inline: inline.normal
@@ -1449,8 +1505,13 @@
1449
1505
 
1450
1506
  _proto.lex = function lex(src) {
1451
1507
  src = src.replace(/\r\n|\r/g, '\n').replace(/\t/g, ' ');
1452
- this.blockTokens(src, this.tokens, true);
1453
- this.inline(this.tokens);
1508
+ this.blockTokens(src, this.tokens);
1509
+ var next;
1510
+
1511
+ while (next = this.inlineQueue.shift()) {
1512
+ this.inlineTokens(next.src, next.tokens);
1513
+ }
1514
+
1454
1515
  return this.tokens;
1455
1516
  }
1456
1517
  /**
@@ -1458,26 +1519,24 @@
1458
1519
  */
1459
1520
  ;
1460
1521
 
1461
- _proto.blockTokens = function blockTokens(src, tokens, top) {
1522
+ _proto.blockTokens = function blockTokens(src, tokens) {
1462
1523
  var _this = this;
1463
1524
 
1464
1525
  if (tokens === void 0) {
1465
1526
  tokens = [];
1466
1527
  }
1467
1528
 
1468
- if (top === void 0) {
1469
- top = true;
1470
- }
1471
-
1472
1529
  if (this.options.pedantic) {
1473
1530
  src = src.replace(/^ +$/gm, '');
1474
1531
  }
1475
1532
 
1476
- var token, i, l, lastToken, cutSrc, lastParagraphClipped;
1533
+ var token, lastToken, cutSrc, lastParagraphClipped;
1477
1534
 
1478
1535
  while (src) {
1479
1536
  if (this.options.extensions && this.options.extensions.block && this.options.extensions.block.some(function (extTokenizer) {
1480
- if (token = extTokenizer.call(_this, src, tokens)) {
1537
+ if (token = extTokenizer.call({
1538
+ lexer: _this
1539
+ }, src, tokens)) {
1481
1540
  src = src.substring(token.raw.length);
1482
1541
  tokens.push(token);
1483
1542
  return true;
@@ -1504,9 +1563,10 @@
1504
1563
  src = src.substring(token.raw.length);
1505
1564
  lastToken = tokens[tokens.length - 1]; // An indented code block cannot interrupt a paragraph.
1506
1565
 
1507
- if (lastToken && lastToken.type === 'paragraph') {
1566
+ if (lastToken && (lastToken.type === 'paragraph' || lastToken.type === 'text')) {
1508
1567
  lastToken.raw += '\n' + token.raw;
1509
1568
  lastToken.text += '\n' + token.text;
1569
+ this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
1510
1570
  } else {
1511
1571
  tokens.push(token);
1512
1572
  }
@@ -1526,13 +1586,6 @@
1526
1586
  src = src.substring(token.raw.length);
1527
1587
  tokens.push(token);
1528
1588
  continue;
1529
- } // table no leading pipe (gfm)
1530
-
1531
-
1532
- if (token = this.tokenizer.nptable(src)) {
1533
- src = src.substring(token.raw.length);
1534
- tokens.push(token);
1535
- continue;
1536
1589
  } // hr
1537
1590
 
1538
1591
 
@@ -1545,7 +1598,6 @@
1545
1598
 
1546
1599
  if (token = this.tokenizer.blockquote(src)) {
1547
1600
  src = src.substring(token.raw.length);
1548
- token.tokens = this.blockTokens(token.text, [], top);
1549
1601
  tokens.push(token);
1550
1602
  continue;
1551
1603
  } // list
@@ -1553,12 +1605,6 @@
1553
1605
 
1554
1606
  if (token = this.tokenizer.list(src)) {
1555
1607
  src = src.substring(token.raw.length);
1556
- l = token.items.length;
1557
-
1558
- for (i = 0; i < l; i++) {
1559
- token.items[i].tokens = this.blockTokens(token.items[i].text, [], false);
1560
- }
1561
-
1562
1608
  tokens.push(token);
1563
1609
  continue;
1564
1610
  } // html
@@ -1571,10 +1617,15 @@
1571
1617
  } // def
1572
1618
 
1573
1619
 
1574
- if (top && (token = this.tokenizer.def(src))) {
1620
+ if (token = this.tokenizer.def(src)) {
1575
1621
  src = src.substring(token.raw.length);
1622
+ lastToken = tokens[tokens.length - 1];
1576
1623
 
1577
- if (!this.tokens.links[token.tag]) {
1624
+ if (lastToken && (lastToken.type === 'paragraph' || lastToken.type === 'text')) {
1625
+ lastToken.raw += '\n' + token.raw;
1626
+ lastToken.text += '\n' + token.raw;
1627
+ this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
1628
+ } else if (!this.tokens.links[token.tag]) {
1578
1629
  this.tokens.links[token.tag] = {
1579
1630
  href: token.href,
1580
1631
  title: token.title
@@ -1609,7 +1660,9 @@
1609
1660
  var tempStart = void 0;
1610
1661
 
1611
1662
  _this.options.extensions.startBlock.forEach(function (getStartIndex) {
1612
- tempStart = getStartIndex.call(this, tempSrc);
1663
+ tempStart = getStartIndex.call({
1664
+ lexer: this
1665
+ }, tempSrc);
1613
1666
 
1614
1667
  if (typeof tempStart === 'number' && tempStart >= 0) {
1615
1668
  startIndex = Math.min(startIndex, tempStart);
@@ -1622,12 +1675,14 @@
1622
1675
  })();
1623
1676
  }
1624
1677
 
1625
- if (top && (token = this.tokenizer.paragraph(cutSrc))) {
1678
+ if (this.state.top && (token = this.tokenizer.paragraph(cutSrc))) {
1626
1679
  lastToken = tokens[tokens.length - 1];
1627
1680
 
1628
1681
  if (lastParagraphClipped && lastToken.type === 'paragraph') {
1629
1682
  lastToken.raw += '\n' + token.raw;
1630
1683
  lastToken.text += '\n' + token.text;
1684
+ this.inlineQueue.pop();
1685
+ this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
1631
1686
  } else {
1632
1687
  tokens.push(token);
1633
1688
  }
@@ -1645,6 +1700,8 @@
1645
1700
  if (lastToken && lastToken.type === 'text') {
1646
1701
  lastToken.raw += '\n' + token.raw;
1647
1702
  lastToken.text += '\n' + token.text;
1703
+ this.inlineQueue.pop();
1704
+ this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
1648
1705
  } else {
1649
1706
  tokens.push(token);
1650
1707
  }
@@ -1664,97 +1721,28 @@
1664
1721
  }
1665
1722
  }
1666
1723
 
1724
+ this.state.top = true;
1667
1725
  return tokens;
1668
1726
  };
1669
1727
 
1670
- _proto.inline = function inline(tokens) {
1671
- var i, j, k, l2, row, token;
1672
- var l = tokens.length;
1673
-
1674
- for (i = 0; i < l; i++) {
1675
- token = tokens[i];
1676
-
1677
- switch (token.type) {
1678
- case 'paragraph':
1679
- case 'text':
1680
- case 'heading':
1681
- {
1682
- token.tokens = [];
1683
- this.inlineTokens(token.text, token.tokens);
1684
- break;
1685
- }
1686
-
1687
- case 'table':
1688
- {
1689
- token.tokens = {
1690
- header: [],
1691
- cells: []
1692
- }; // header
1693
-
1694
- l2 = token.header.length;
1695
-
1696
- for (j = 0; j < l2; j++) {
1697
- token.tokens.header[j] = [];
1698
- this.inlineTokens(token.header[j], token.tokens.header[j]);
1699
- } // cells
1700
-
1701
-
1702
- l2 = token.cells.length;
1703
-
1704
- for (j = 0; j < l2; j++) {
1705
- row = token.cells[j];
1706
- token.tokens.cells[j] = [];
1707
-
1708
- for (k = 0; k < row.length; k++) {
1709
- token.tokens.cells[j][k] = [];
1710
- this.inlineTokens(row[k], token.tokens.cells[j][k]);
1711
- }
1712
- }
1713
-
1714
- break;
1715
- }
1716
-
1717
- case 'blockquote':
1718
- {
1719
- this.inline(token.tokens);
1720
- break;
1721
- }
1722
-
1723
- case 'list':
1724
- {
1725
- l2 = token.items.length;
1726
-
1727
- for (j = 0; j < l2; j++) {
1728
- this.inline(token.items[j].tokens);
1729
- }
1730
-
1731
- break;
1732
- }
1733
- }
1734
- }
1735
-
1736
- return tokens;
1728
+ _proto.inline = function inline(src, tokens) {
1729
+ this.inlineQueue.push({
1730
+ src: src,
1731
+ tokens: tokens
1732
+ });
1737
1733
  }
1738
1734
  /**
1739
1735
  * Lexing/Compiling
1740
1736
  */
1741
1737
  ;
1742
1738
 
1743
- _proto.inlineTokens = function inlineTokens(src, tokens, inLink, inRawBlock) {
1739
+ _proto.inlineTokens = function inlineTokens(src, tokens) {
1744
1740
  var _this2 = this;
1745
1741
 
1746
1742
  if (tokens === void 0) {
1747
1743
  tokens = [];
1748
1744
  }
1749
1745
 
1750
- if (inLink === void 0) {
1751
- inLink = false;
1752
- }
1753
-
1754
- if (inRawBlock === void 0) {
1755
- inRawBlock = false;
1756
- }
1757
-
1758
1746
  var token, lastToken, cutSrc; // String with links masked to avoid interference with em and strong
1759
1747
 
1760
1748
  var maskedSrc = src;
@@ -1791,7 +1779,9 @@
1791
1779
  keepPrevChar = false; // extensions
1792
1780
 
1793
1781
  if (this.options.extensions && this.options.extensions.inline && this.options.extensions.inline.some(function (extTokenizer) {
1794
- if (token = extTokenizer.call(_this2, src, tokens)) {
1782
+ if (token = extTokenizer.call({
1783
+ lexer: _this2
1784
+ }, src, tokens)) {
1795
1785
  src = src.substring(token.raw.length);
1796
1786
  tokens.push(token);
1797
1787
  return true;
@@ -1810,10 +1800,8 @@
1810
1800
  } // tag
1811
1801
 
1812
1802
 
1813
- if (token = this.tokenizer.tag(src, inLink, inRawBlock)) {
1803
+ if (token = this.tokenizer.tag(src)) {
1814
1804
  src = src.substring(token.raw.length);
1815
- inLink = token.inLink;
1816
- inRawBlock = token.inRawBlock;
1817
1805
  lastToken = tokens[tokens.length - 1];
1818
1806
 
1819
1807
  if (lastToken && token.type === 'text' && lastToken.type === 'text') {
@@ -1829,11 +1817,6 @@
1829
1817
 
1830
1818
  if (token = this.tokenizer.link(src)) {
1831
1819
  src = src.substring(token.raw.length);
1832
-
1833
- if (token.type === 'link') {
1834
- token.tokens = this.inlineTokens(token.text, [], true, inRawBlock);
1835
- }
1836
-
1837
1820
  tokens.push(token);
1838
1821
  continue;
1839
1822
  } // reflink, nolink
@@ -1843,10 +1826,7 @@
1843
1826
  src = src.substring(token.raw.length);
1844
1827
  lastToken = tokens[tokens.length - 1];
1845
1828
 
1846
- if (token.type === 'link') {
1847
- token.tokens = this.inlineTokens(token.text, [], true, inRawBlock);
1848
- tokens.push(token);
1849
- } else if (lastToken && token.type === 'text' && lastToken.type === 'text') {
1829
+ if (lastToken && token.type === 'text' && lastToken.type === 'text') {
1850
1830
  lastToken.raw += token.raw;
1851
1831
  lastToken.text += token.text;
1852
1832
  } else {
@@ -1859,7 +1839,6 @@
1859
1839
 
1860
1840
  if (token = this.tokenizer.emStrong(src, maskedSrc, prevChar)) {
1861
1841
  src = src.substring(token.raw.length);
1862
- token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
1863
1842
  tokens.push(token);
1864
1843
  continue;
1865
1844
  } // code
@@ -1881,7 +1860,6 @@
1881
1860
 
1882
1861
  if (token = this.tokenizer.del(src)) {
1883
1862
  src = src.substring(token.raw.length);
1884
- token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
1885
1863
  tokens.push(token);
1886
1864
  continue;
1887
1865
  } // autolink
@@ -1894,7 +1872,7 @@
1894
1872
  } // url (gfm)
1895
1873
 
1896
1874
 
1897
- if (!inLink && (token = this.tokenizer.url(src, mangle))) {
1875
+ if (!this.state.inLink && (token = this.tokenizer.url(src, mangle))) {
1898
1876
  src = src.substring(token.raw.length);
1899
1877
  tokens.push(token);
1900
1878
  continue;
@@ -1911,7 +1889,9 @@
1911
1889
  var tempStart = void 0;
1912
1890
 
1913
1891
  _this2.options.extensions.startInline.forEach(function (getStartIndex) {
1914
- tempStart = getStartIndex.call(this, tempSrc);
1892
+ tempStart = getStartIndex.call({
1893
+ lexer: this
1894
+ }, tempSrc);
1915
1895
 
1916
1896
  if (typeof tempStart === 'number' && tempStart >= 0) {
1917
1897
  startIndex = Math.min(startIndex, tempStart);
@@ -1924,7 +1904,7 @@
1924
1904
  })();
1925
1905
  }
1926
1906
 
1927
- if (token = this.tokenizer.inlineText(cutSrc, inRawBlock, smartypants)) {
1907
+ if (token = this.tokenizer.inlineText(cutSrc, smartypants)) {
1928
1908
  src = src.substring(token.raw.length);
1929
1909
 
1930
1910
  if (token.raw.slice(-1) !== '_') {
@@ -2308,7 +2288,9 @@
2308
2288
  token = tokens[i]; // Run any renderer extensions
2309
2289
 
2310
2290
  if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[token.type]) {
2311
- ret = this.options.extensions.renderers[token.type].call(this, token);
2291
+ ret = this.options.extensions.renderers[token.type].call({
2292
+ parser: this
2293
+ }, token);
2312
2294
 
2313
2295
  if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'html', 'paragraph', 'text'].includes(token.type)) {
2314
2296
  out += ret || '';
@@ -2348,7 +2330,7 @@
2348
2330
  l2 = token.header.length;
2349
2331
 
2350
2332
  for (j = 0; j < l2; j++) {
2351
- cell += this.renderer.tablecell(this.parseInline(token.tokens.header[j]), {
2333
+ cell += this.renderer.tablecell(this.parseInline(token.header[j].tokens), {
2352
2334
  header: true,
2353
2335
  align: token.align[j]
2354
2336
  });
@@ -2356,15 +2338,15 @@
2356
2338
 
2357
2339
  header += this.renderer.tablerow(cell);
2358
2340
  body = '';
2359
- l2 = token.cells.length;
2341
+ l2 = token.rows.length;
2360
2342
 
2361
2343
  for (j = 0; j < l2; j++) {
2362
- row = token.tokens.cells[j];
2344
+ row = token.rows[j];
2363
2345
  cell = '';
2364
2346
  l3 = row.length;
2365
2347
 
2366
2348
  for (k = 0; k < l3; k++) {
2367
- cell += this.renderer.tablecell(this.parseInline(row[k]), {
2349
+ cell += this.renderer.tablecell(this.parseInline(row[k].tokens), {
2368
2350
  header: false,
2369
2351
  align: token.align[k]
2370
2352
  });
@@ -2402,7 +2384,7 @@
2402
2384
  checkbox = this.renderer.checkbox(checked);
2403
2385
 
2404
2386
  if (loose) {
2405
- if (item.tokens.length > 0 && item.tokens[0].type === 'text') {
2387
+ if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
2406
2388
  item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
2407
2389
 
2408
2390
  if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
@@ -2486,7 +2468,9 @@
2486
2468
  token = tokens[i]; // Run any renderer extensions
2487
2469
 
2488
2470
  if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[token.type]) {
2489
- ret = this.options.extensions.renderers[token.type].call(this, token);
2471
+ ret = this.options.extensions.renderers[token.type].call({
2472
+ parser: this
2473
+ }, token);
2490
2474
 
2491
2475
  if (ret !== false || !['escape', 'html', 'link', 'image', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(token.type)) {
2492
2476
  out += ret || '';
@@ -2888,17 +2872,17 @@
2888
2872
  switch (token.type) {
2889
2873
  case 'table':
2890
2874
  {
2891
- for (var _iterator2 = _createForOfIteratorHelperLoose(token.tokens.header), _step2; !(_step2 = _iterator2()).done;) {
2875
+ for (var _iterator2 = _createForOfIteratorHelperLoose(token.header), _step2; !(_step2 = _iterator2()).done;) {
2892
2876
  var cell = _step2.value;
2893
- marked.walkTokens(cell, callback);
2877
+ marked.walkTokens(cell.tokens, callback);
2894
2878
  }
2895
2879
 
2896
- for (var _iterator3 = _createForOfIteratorHelperLoose(token.tokens.cells), _step3; !(_step3 = _iterator3()).done;) {
2880
+ for (var _iterator3 = _createForOfIteratorHelperLoose(token.rows), _step3; !(_step3 = _iterator3()).done;) {
2897
2881
  var row = _step3.value;
2898
2882
 
2899
2883
  for (var _iterator4 = _createForOfIteratorHelperLoose(row), _step4; !(_step4 = _iterator4()).done;) {
2900
2884
  var _cell = _step4.value;
2901
- marked.walkTokens(_cell, callback);
2885
+ marked.walkTokens(_cell.tokens, callback);
2902
2886
  }
2903
2887
  }
2904
2888