marked 4.0.17 → 4.1.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.cjs CHANGED
@@ -72,6 +72,7 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
72
72
 
73
73
  function getDefaults() {
74
74
  return {
75
+ async: false,
75
76
  baseUrl: null,
76
77
  breaks: false,
77
78
  extensions: null,
@@ -412,7 +413,7 @@ function outputLink(cap, link, raw, lexer) {
412
413
  href: href,
413
414
  title: title,
414
415
  text: text,
415
- tokens: lexer.inlineTokens(text, [])
416
+ tokens: lexer.inlineTokens(text)
416
417
  };
417
418
  lexer.state.inLink = false;
418
419
  return token;
@@ -520,15 +521,13 @@ var Tokenizer = /*#__PURE__*/function () {
520
521
  }
521
522
  }
522
523
 
523
- var token = {
524
+ return {
524
525
  type: 'heading',
525
526
  raw: cap[0],
526
527
  depth: cap[1].length,
527
528
  text: text,
528
- tokens: []
529
+ tokens: this.lexer.inline(text)
529
530
  };
530
- this.lexer.inline(token.text, token.tokens);
531
- return token;
532
531
  }
533
532
  };
534
533
 
@@ -622,7 +621,8 @@ var Tokenizer = /*#__PURE__*/function () {
622
621
  if (!endEarly) {
623
622
  var nextBulletRegex = new RegExp("^ {0," + Math.min(3, indent - 1) + "}(?:[*+-]|\\d{1,9}[.)])((?: [^\\n]*)?(?:\\n|$))");
624
623
  var hrRegex = new RegExp("^ {0," + Math.min(3, indent - 1) + "}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)");
625
- var fencesBeginRegex = new RegExp("^( {0," + Math.min(3, indent - 1) + "})(```|~~~)"); // Check if following lines should be included in List Item
624
+ var fencesBeginRegex = new RegExp("^ {0," + Math.min(3, indent - 1) + "}(?:```|~~~)");
625
+ var headingBeginRegex = new RegExp("^ {0," + Math.min(3, indent - 1) + "}#"); // Check if following lines should be included in List Item
626
626
 
627
627
  while (src) {
628
628
  rawLine = src.split('\n', 1)[0];
@@ -638,7 +638,7 @@ var Tokenizer = /*#__PURE__*/function () {
638
638
  } // End list item if found start of new heading
639
639
 
640
640
 
641
- if (this.rules.block.heading.test(line)) {
641
+ if (headingBeginRegex.test(line)) {
642
642
  break;
643
643
  } // End list item if found start of new bullet
644
644
 
@@ -757,10 +757,10 @@ var Tokenizer = /*#__PURE__*/function () {
757
757
  };
758
758
 
759
759
  if (this.options.sanitize) {
760
+ var text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
760
761
  token.type = 'paragraph';
761
- token.text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
762
- token.tokens = [];
763
- this.lexer.inline(token.text, token.tokens);
762
+ token.text = text;
763
+ token.tokens = this.lexer.inline(text);
764
764
  }
765
765
 
766
766
  return token;
@@ -830,8 +830,7 @@ var Tokenizer = /*#__PURE__*/function () {
830
830
  l = item.header.length;
831
831
 
832
832
  for (j = 0; j < l; j++) {
833
- item.header[j].tokens = [];
834
- this.lexer.inline(item.header[j].text, item.header[j].tokens);
833
+ item.header[j].tokens = this.lexer.inline(item.header[j].text);
835
834
  } // cell child tokens
836
835
 
837
836
 
@@ -841,8 +840,7 @@ var Tokenizer = /*#__PURE__*/function () {
841
840
  row = item.rows[j];
842
841
 
843
842
  for (k = 0; k < row.length; k++) {
844
- row[k].tokens = [];
845
- this.lexer.inline(row[k].text, row[k].tokens);
843
+ row[k].tokens = this.lexer.inline(row[k].text);
846
844
  }
847
845
  }
848
846
 
@@ -855,15 +853,13 @@ var Tokenizer = /*#__PURE__*/function () {
855
853
  var cap = this.rules.block.lheading.exec(src);
856
854
 
857
855
  if (cap) {
858
- var token = {
856
+ return {
859
857
  type: 'heading',
860
858
  raw: cap[0],
861
859
  depth: cap[2].charAt(0) === '=' ? 1 : 2,
862
860
  text: cap[1],
863
- tokens: []
861
+ tokens: this.lexer.inline(cap[1])
864
862
  };
865
- this.lexer.inline(token.text, token.tokens);
866
- return token;
867
863
  }
868
864
  };
869
865
 
@@ -871,14 +867,13 @@ var Tokenizer = /*#__PURE__*/function () {
871
867
  var cap = this.rules.block.paragraph.exec(src);
872
868
 
873
869
  if (cap) {
874
- var token = {
870
+ var text = cap[1].charAt(cap[1].length - 1) === '\n' ? cap[1].slice(0, -1) : cap[1];
871
+ return {
875
872
  type: 'paragraph',
876
873
  raw: cap[0],
877
- text: cap[1].charAt(cap[1].length - 1) === '\n' ? cap[1].slice(0, -1) : cap[1],
878
- tokens: []
874
+ text: text,
875
+ tokens: this.lexer.inline(text)
879
876
  };
880
- this.lexer.inline(token.text, token.tokens);
881
- return token;
882
877
  }
883
878
  };
884
879
 
@@ -886,14 +881,12 @@ var Tokenizer = /*#__PURE__*/function () {
886
881
  var cap = this.rules.block.text.exec(src);
887
882
 
888
883
  if (cap) {
889
- var token = {
884
+ return {
890
885
  type: 'text',
891
886
  raw: cap[0],
892
887
  text: cap[0],
893
- tokens: []
888
+ tokens: this.lexer.inline(cap[0])
894
889
  };
895
- this.lexer.inline(token.text, token.tokens);
896
- return token;
897
890
  }
898
891
  };
899
892
 
@@ -1072,7 +1065,7 @@ var Tokenizer = /*#__PURE__*/function () {
1072
1065
  type: 'em',
1073
1066
  raw: src.slice(0, lLength + match.index + rLength + 1),
1074
1067
  text: _text,
1075
- tokens: this.lexer.inlineTokens(_text, [])
1068
+ tokens: this.lexer.inlineTokens(_text)
1076
1069
  };
1077
1070
  } // Create 'strong' if smallest delimiter has even char count. **a***
1078
1071
 
@@ -1082,7 +1075,7 @@ var Tokenizer = /*#__PURE__*/function () {
1082
1075
  type: 'strong',
1083
1076
  raw: src.slice(0, lLength + match.index + rLength + 1),
1084
1077
  text: text,
1085
- tokens: this.lexer.inlineTokens(text, [])
1078
+ tokens: this.lexer.inlineTokens(text)
1086
1079
  };
1087
1080
  }
1088
1081
  }
@@ -1128,7 +1121,7 @@ var Tokenizer = /*#__PURE__*/function () {
1128
1121
  type: 'del',
1129
1122
  raw: cap[0],
1130
1123
  text: cap[2],
1131
- tokens: this.lexer.inlineTokens(cap[2], [])
1124
+ tokens: this.lexer.inlineTokens(cap[2])
1132
1125
  };
1133
1126
  }
1134
1127
  };
@@ -1746,10 +1739,15 @@ var Lexer = /*#__PURE__*/function () {
1746
1739
  };
1747
1740
 
1748
1741
  _proto.inline = function inline(src, tokens) {
1742
+ if (tokens === void 0) {
1743
+ tokens = [];
1744
+ }
1745
+
1749
1746
  this.inlineQueue.push({
1750
1747
  src: src,
1751
1748
  tokens: tokens
1752
1749
  });
1750
+ return tokens;
1753
1751
  }
1754
1752
  /**
1755
1753
  * Lexing/Compiling
@@ -2722,22 +2720,32 @@ function marked(src, opt, callback) {
2722
2720
  return;
2723
2721
  }
2724
2722
 
2723
+ function onError(e) {
2724
+ e.message += '\nPlease report this to https://github.com/markedjs/marked.';
2725
+
2726
+ if (opt.silent) {
2727
+ return '<p>An error occurred:</p><pre>' + escape(e.message + '', true) + '</pre>';
2728
+ }
2729
+
2730
+ throw e;
2731
+ }
2732
+
2725
2733
  try {
2726
2734
  var _tokens = Lexer.lex(src, opt);
2727
2735
 
2728
2736
  if (opt.walkTokens) {
2737
+ if (opt.async) {
2738
+ return Promise.all(marked.walkTokens(_tokens, opt.walkTokens)).then(function () {
2739
+ return Parser.parse(_tokens, opt);
2740
+ })["catch"](onError);
2741
+ }
2742
+
2729
2743
  marked.walkTokens(_tokens, opt.walkTokens);
2730
2744
  }
2731
2745
 
2732
2746
  return Parser.parse(_tokens, opt);
2733
2747
  } catch (e) {
2734
- e.message += '\nPlease report this to https://github.com/markedjs/marked.';
2735
-
2736
- if (opt.silent) {
2737
- return '<p>An error occurred:</p><pre>' + escape(e.message + '', true) + '</pre>';
2738
- }
2739
-
2740
- throw e;
2748
+ onError(e);
2741
2749
  }
2742
2750
  }
2743
2751
  /**
@@ -2903,11 +2911,14 @@ marked.use = function () {
2903
2911
  var _walkTokens = marked.defaults.walkTokens;
2904
2912
 
2905
2913
  opts.walkTokens = function (token) {
2906
- pack.walkTokens.call(this, token);
2914
+ var values = [];
2915
+ values.push(pack.walkTokens.call(this, token));
2907
2916
 
2908
2917
  if (_walkTokens) {
2909
- _walkTokens.call(this, token);
2918
+ values = values.concat(_walkTokens.call(this, token));
2910
2919
  }
2920
+
2921
+ return values;
2911
2922
  };
2912
2923
  }
2913
2924
 
@@ -2924,16 +2935,18 @@ marked.use = function () {
2924
2935
 
2925
2936
 
2926
2937
  marked.walkTokens = function (tokens, callback) {
2938
+ var values = [];
2939
+
2927
2940
  var _loop3 = function _loop3() {
2928
2941
  var token = _step.value;
2929
- callback.call(marked, token);
2942
+ values = values.concat(callback.call(marked, token));
2930
2943
 
2931
2944
  switch (token.type) {
2932
2945
  case 'table':
2933
2946
  {
2934
2947
  for (var _iterator2 = _createForOfIteratorHelperLoose(token.header), _step2; !(_step2 = _iterator2()).done;) {
2935
2948
  var cell = _step2.value;
2936
- marked.walkTokens(cell.tokens, callback);
2949
+ values = values.concat(marked.walkTokens(cell.tokens, callback));
2937
2950
  }
2938
2951
 
2939
2952
  for (var _iterator3 = _createForOfIteratorHelperLoose(token.rows), _step3; !(_step3 = _iterator3()).done;) {
@@ -2941,7 +2954,7 @@ marked.walkTokens = function (tokens, callback) {
2941
2954
 
2942
2955
  for (var _iterator4 = _createForOfIteratorHelperLoose(row), _step4; !(_step4 = _iterator4()).done;) {
2943
2956
  var _cell = _step4.value;
2944
- marked.walkTokens(_cell.tokens, callback);
2957
+ values = values.concat(marked.walkTokens(_cell.tokens, callback));
2945
2958
  }
2946
2959
  }
2947
2960
 
@@ -2950,7 +2963,7 @@ marked.walkTokens = function (tokens, callback) {
2950
2963
 
2951
2964
  case 'list':
2952
2965
  {
2953
- marked.walkTokens(token.items, callback);
2966
+ values = values.concat(marked.walkTokens(token.items, callback));
2954
2967
  break;
2955
2968
  }
2956
2969
 
@@ -2959,10 +2972,10 @@ marked.walkTokens = function (tokens, callback) {
2959
2972
  if (marked.defaults.extensions && marked.defaults.extensions.childTokens && marked.defaults.extensions.childTokens[token.type]) {
2960
2973
  // Walk any extensions
2961
2974
  marked.defaults.extensions.childTokens[token.type].forEach(function (childTokens) {
2962
- marked.walkTokens(token[childTokens], callback);
2975
+ values = values.concat(marked.walkTokens(token[childTokens], callback));
2963
2976
  });
2964
2977
  } else if (token.tokens) {
2965
- marked.walkTokens(token.tokens, callback);
2978
+ values = values.concat(marked.walkTokens(token.tokens, callback));
2966
2979
  }
2967
2980
  }
2968
2981
  }
@@ -2971,6 +2984,8 @@ marked.walkTokens = function (tokens, callback) {
2971
2984
  for (var _iterator = _createForOfIteratorHelperLoose(tokens), _step; !(_step = _iterator()).done;) {
2972
2985
  _loop3();
2973
2986
  }
2987
+
2988
+ return values;
2974
2989
  };
2975
2990
  /**
2976
2991
  * Parse Inline
package/lib/marked.esm.js CHANGED
@@ -11,6 +11,7 @@
11
11
 
12
12
  function getDefaults() {
13
13
  return {
14
+ async: false,
14
15
  baseUrl: null,
15
16
  breaks: false,
16
17
  extensions: null,
@@ -329,7 +330,7 @@ function outputLink(cap, link, raw, lexer) {
329
330
  href,
330
331
  title,
331
332
  text,
332
- tokens: lexer.inlineTokens(text, [])
333
+ tokens: lexer.inlineTokens(text)
333
334
  };
334
335
  lexer.state.inLink = false;
335
336
  return token;
@@ -435,15 +436,13 @@ class Tokenizer {
435
436
  }
436
437
  }
437
438
 
438
- const token = {
439
+ return {
439
440
  type: 'heading',
440
441
  raw: cap[0],
441
442
  depth: cap[1].length,
442
443
  text,
443
- tokens: []
444
+ tokens: this.lexer.inline(text)
444
445
  };
445
- this.lexer.inline(token.text, token.tokens);
446
- return token;
447
446
  }
448
447
  }
449
448
 
@@ -536,7 +535,8 @@ class Tokenizer {
536
535
  if (!endEarly) {
537
536
  const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])((?: [^\\n]*)?(?:\\n|$))`);
538
537
  const hrRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`);
539
- const fencesBeginRegex = new RegExp(`^( {0,${Math.min(3, indent - 1)}})(\`\`\`|~~~)`);
538
+ const fencesBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:\`\`\`|~~~)`);
539
+ const headingBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}#`);
540
540
 
541
541
  // Check if following lines should be included in List Item
542
542
  while (src) {
@@ -554,7 +554,7 @@ class Tokenizer {
554
554
  }
555
555
 
556
556
  // End list item if found start of new heading
557
- if (this.rules.block.heading.test(line)) {
557
+ if (headingBeginRegex.test(line)) {
558
558
  break;
559
559
  }
560
560
 
@@ -664,10 +664,10 @@ class Tokenizer {
664
664
  text: cap[0]
665
665
  };
666
666
  if (this.options.sanitize) {
667
+ const text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
667
668
  token.type = 'paragraph';
668
- token.text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
669
- token.tokens = [];
670
- this.lexer.inline(token.text, token.tokens);
669
+ token.text = text;
670
+ token.tokens = this.lexer.inline(text);
671
671
  }
672
672
  return token;
673
673
  }
@@ -725,8 +725,7 @@ class Tokenizer {
725
725
  // header child tokens
726
726
  l = item.header.length;
727
727
  for (j = 0; j < l; j++) {
728
- item.header[j].tokens = [];
729
- this.lexer.inline(item.header[j].text, item.header[j].tokens);
728
+ item.header[j].tokens = this.lexer.inline(item.header[j].text);
730
729
  }
731
730
 
732
731
  // cell child tokens
@@ -734,8 +733,7 @@ class Tokenizer {
734
733
  for (j = 0; j < l; j++) {
735
734
  row = item.rows[j];
736
735
  for (k = 0; k < row.length; k++) {
737
- row[k].tokens = [];
738
- this.lexer.inline(row[k].text, row[k].tokens);
736
+ row[k].tokens = this.lexer.inline(row[k].text);
739
737
  }
740
738
  }
741
739
 
@@ -747,45 +745,40 @@ class Tokenizer {
747
745
  lheading(src) {
748
746
  const cap = this.rules.block.lheading.exec(src);
749
747
  if (cap) {
750
- const token = {
748
+ return {
751
749
  type: 'heading',
752
750
  raw: cap[0],
753
751
  depth: cap[2].charAt(0) === '=' ? 1 : 2,
754
752
  text: cap[1],
755
- tokens: []
753
+ tokens: this.lexer.inline(cap[1])
756
754
  };
757
- this.lexer.inline(token.text, token.tokens);
758
- return token;
759
755
  }
760
756
  }
761
757
 
762
758
  paragraph(src) {
763
759
  const cap = this.rules.block.paragraph.exec(src);
764
760
  if (cap) {
765
- const token = {
761
+ const text = cap[1].charAt(cap[1].length - 1) === '\n'
762
+ ? cap[1].slice(0, -1)
763
+ : cap[1];
764
+ return {
766
765
  type: 'paragraph',
767
766
  raw: cap[0],
768
- text: cap[1].charAt(cap[1].length - 1) === '\n'
769
- ? cap[1].slice(0, -1)
770
- : cap[1],
771
- tokens: []
767
+ text,
768
+ tokens: this.lexer.inline(text)
772
769
  };
773
- this.lexer.inline(token.text, token.tokens);
774
- return token;
775
770
  }
776
771
  }
777
772
 
778
773
  text(src) {
779
774
  const cap = this.rules.block.text.exec(src);
780
775
  if (cap) {
781
- const token = {
776
+ return {
782
777
  type: 'text',
783
778
  raw: cap[0],
784
779
  text: cap[0],
785
- tokens: []
780
+ tokens: this.lexer.inline(cap[0])
786
781
  };
787
- this.lexer.inline(token.text, token.tokens);
788
- return token;
789
782
  }
790
783
  }
791
784
 
@@ -954,7 +947,7 @@ class Tokenizer {
954
947
  type: 'em',
955
948
  raw: src.slice(0, lLength + match.index + rLength + 1),
956
949
  text,
957
- tokens: this.lexer.inlineTokens(text, [])
950
+ tokens: this.lexer.inlineTokens(text)
958
951
  };
959
952
  }
960
953
 
@@ -964,7 +957,7 @@ class Tokenizer {
964
957
  type: 'strong',
965
958
  raw: src.slice(0, lLength + match.index + rLength + 1),
966
959
  text,
967
- tokens: this.lexer.inlineTokens(text, [])
960
+ tokens: this.lexer.inlineTokens(text)
968
961
  };
969
962
  }
970
963
  }
@@ -1005,7 +998,7 @@ class Tokenizer {
1005
998
  type: 'del',
1006
999
  raw: cap[0],
1007
1000
  text: cap[2],
1008
- tokens: this.lexer.inlineTokens(cap[2], [])
1001
+ tokens: this.lexer.inlineTokens(cap[2])
1009
1002
  };
1010
1003
  }
1011
1004
  }
@@ -1703,8 +1696,9 @@ class Lexer {
1703
1696
  return tokens;
1704
1697
  }
1705
1698
 
1706
- inline(src, tokens) {
1699
+ inline(src, tokens = []) {
1707
1700
  this.inlineQueue.push({ src, tokens });
1701
+ return tokens;
1708
1702
  }
1709
1703
 
1710
1704
  /**
@@ -2553,13 +2547,7 @@ function marked(src, opt, callback) {
2553
2547
  return;
2554
2548
  }
2555
2549
 
2556
- try {
2557
- const tokens = Lexer.lex(src, opt);
2558
- if (opt.walkTokens) {
2559
- marked.walkTokens(tokens, opt.walkTokens);
2560
- }
2561
- return Parser.parse(tokens, opt);
2562
- } catch (e) {
2550
+ function onError(e) {
2563
2551
  e.message += '\nPlease report this to https://github.com/markedjs/marked.';
2564
2552
  if (opt.silent) {
2565
2553
  return '<p>An error occurred:</p><pre>'
@@ -2568,6 +2556,23 @@ function marked(src, opt, callback) {
2568
2556
  }
2569
2557
  throw e;
2570
2558
  }
2559
+
2560
+ try {
2561
+ const tokens = Lexer.lex(src, opt);
2562
+ if (opt.walkTokens) {
2563
+ if (opt.async) {
2564
+ return Promise.all(marked.walkTokens(tokens, opt.walkTokens))
2565
+ .then(() => {
2566
+ return Parser.parse(tokens, opt);
2567
+ })
2568
+ .catch(onError);
2569
+ }
2570
+ marked.walkTokens(tokens, opt.walkTokens);
2571
+ }
2572
+ return Parser.parse(tokens, opt);
2573
+ } catch (e) {
2574
+ onError(e);
2575
+ }
2571
2576
  }
2572
2577
 
2573
2578
  /**
@@ -2684,10 +2689,12 @@ marked.use = function(...args) {
2684
2689
  if (pack.walkTokens) {
2685
2690
  const walkTokens = marked.defaults.walkTokens;
2686
2691
  opts.walkTokens = function(token) {
2687
- pack.walkTokens.call(this, token);
2692
+ let values = [];
2693
+ values.push(pack.walkTokens.call(this, token));
2688
2694
  if (walkTokens) {
2689
- walkTokens.call(this, token);
2695
+ values = values.concat(walkTokens.call(this, token));
2690
2696
  }
2697
+ return values;
2691
2698
  };
2692
2699
  }
2693
2700
 
@@ -2704,35 +2711,37 @@ marked.use = function(...args) {
2704
2711
  */
2705
2712
 
2706
2713
  marked.walkTokens = function(tokens, callback) {
2714
+ let values = [];
2707
2715
  for (const token of tokens) {
2708
- callback.call(marked, token);
2716
+ values = values.concat(callback.call(marked, token));
2709
2717
  switch (token.type) {
2710
2718
  case 'table': {
2711
2719
  for (const cell of token.header) {
2712
- marked.walkTokens(cell.tokens, callback);
2720
+ values = values.concat(marked.walkTokens(cell.tokens, callback));
2713
2721
  }
2714
2722
  for (const row of token.rows) {
2715
2723
  for (const cell of row) {
2716
- marked.walkTokens(cell.tokens, callback);
2724
+ values = values.concat(marked.walkTokens(cell.tokens, callback));
2717
2725
  }
2718
2726
  }
2719
2727
  break;
2720
2728
  }
2721
2729
  case 'list': {
2722
- marked.walkTokens(token.items, callback);
2730
+ values = values.concat(marked.walkTokens(token.items, callback));
2723
2731
  break;
2724
2732
  }
2725
2733
  default: {
2726
2734
  if (marked.defaults.extensions && marked.defaults.extensions.childTokens && marked.defaults.extensions.childTokens[token.type]) { // Walk any extensions
2727
2735
  marked.defaults.extensions.childTokens[token.type].forEach(function(childTokens) {
2728
- marked.walkTokens(token[childTokens], callback);
2736
+ values = values.concat(marked.walkTokens(token[childTokens], callback));
2729
2737
  });
2730
2738
  } else if (token.tokens) {
2731
- marked.walkTokens(token.tokens, callback);
2739
+ values = values.concat(marked.walkTokens(token.tokens, callback));
2732
2740
  }
2733
2741
  }
2734
2742
  }
2735
2743
  }
2744
+ return values;
2736
2745
  };
2737
2746
 
2738
2747
  /**