marked 2.1.3 → 3.0.0

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