marked 4.0.19 → 4.1.1

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/README.md CHANGED
@@ -47,6 +47,7 @@ npm install -g marked
47
47
 
48
48
  ```sh
49
49
  npm install marked
50
+ npm install @types/marked # For TypeScript projects
50
51
  ```
51
52
 
52
53
  ## Usage
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,
@@ -86,7 +87,6 @@ function getDefaults() {
86
87
  sanitize: false,
87
88
  sanitizer: null,
88
89
  silent: false,
89
- smartLists: false,
90
90
  smartypants: false,
91
91
  tokenizer: null,
92
92
  walkTokens: null,
@@ -412,7 +412,7 @@ function outputLink(cap, link, raw, lexer) {
412
412
  href: href,
413
413
  title: title,
414
414
  text: text,
415
- tokens: lexer.inlineTokens(text, [])
415
+ tokens: lexer.inlineTokens(text)
416
416
  };
417
417
  lexer.state.inLink = false;
418
418
  return token;
@@ -520,15 +520,13 @@ var Tokenizer = /*#__PURE__*/function () {
520
520
  }
521
521
  }
522
522
 
523
- var token = {
523
+ return {
524
524
  type: 'heading',
525
525
  raw: cap[0],
526
526
  depth: cap[1].length,
527
527
  text: text,
528
- tokens: []
528
+ tokens: this.lexer.inline(text)
529
529
  };
530
- this.lexer.inline(token.text, token.tokens);
531
- return token;
532
530
  }
533
531
  };
534
532
 
@@ -758,10 +756,10 @@ var Tokenizer = /*#__PURE__*/function () {
758
756
  };
759
757
 
760
758
  if (this.options.sanitize) {
759
+ var text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
761
760
  token.type = 'paragraph';
762
- token.text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
763
- token.tokens = [];
764
- this.lexer.inline(token.text, token.tokens);
761
+ token.text = text;
762
+ token.tokens = this.lexer.inline(text);
765
763
  }
766
764
 
767
765
  return token;
@@ -831,8 +829,7 @@ var Tokenizer = /*#__PURE__*/function () {
831
829
  l = item.header.length;
832
830
 
833
831
  for (j = 0; j < l; j++) {
834
- item.header[j].tokens = [];
835
- this.lexer.inline(item.header[j].text, item.header[j].tokens);
832
+ item.header[j].tokens = this.lexer.inline(item.header[j].text);
836
833
  } // cell child tokens
837
834
 
838
835
 
@@ -842,8 +839,7 @@ var Tokenizer = /*#__PURE__*/function () {
842
839
  row = item.rows[j];
843
840
 
844
841
  for (k = 0; k < row.length; k++) {
845
- row[k].tokens = [];
846
- this.lexer.inline(row[k].text, row[k].tokens);
842
+ row[k].tokens = this.lexer.inline(row[k].text);
847
843
  }
848
844
  }
849
845
 
@@ -856,15 +852,13 @@ var Tokenizer = /*#__PURE__*/function () {
856
852
  var cap = this.rules.block.lheading.exec(src);
857
853
 
858
854
  if (cap) {
859
- var token = {
855
+ return {
860
856
  type: 'heading',
861
857
  raw: cap[0],
862
858
  depth: cap[2].charAt(0) === '=' ? 1 : 2,
863
859
  text: cap[1],
864
- tokens: []
860
+ tokens: this.lexer.inline(cap[1])
865
861
  };
866
- this.lexer.inline(token.text, token.tokens);
867
- return token;
868
862
  }
869
863
  };
870
864
 
@@ -872,14 +866,13 @@ var Tokenizer = /*#__PURE__*/function () {
872
866
  var cap = this.rules.block.paragraph.exec(src);
873
867
 
874
868
  if (cap) {
875
- var token = {
869
+ var text = cap[1].charAt(cap[1].length - 1) === '\n' ? cap[1].slice(0, -1) : cap[1];
870
+ return {
876
871
  type: 'paragraph',
877
872
  raw: cap[0],
878
- text: cap[1].charAt(cap[1].length - 1) === '\n' ? cap[1].slice(0, -1) : cap[1],
879
- tokens: []
873
+ text: text,
874
+ tokens: this.lexer.inline(text)
880
875
  };
881
- this.lexer.inline(token.text, token.tokens);
882
- return token;
883
876
  }
884
877
  };
885
878
 
@@ -887,14 +880,12 @@ var Tokenizer = /*#__PURE__*/function () {
887
880
  var cap = this.rules.block.text.exec(src);
888
881
 
889
882
  if (cap) {
890
- var token = {
883
+ return {
891
884
  type: 'text',
892
885
  raw: cap[0],
893
886
  text: cap[0],
894
- tokens: []
887
+ tokens: this.lexer.inline(cap[0])
895
888
  };
896
- this.lexer.inline(token.text, token.tokens);
897
- return token;
898
889
  }
899
890
  };
900
891
 
@@ -1073,7 +1064,7 @@ var Tokenizer = /*#__PURE__*/function () {
1073
1064
  type: 'em',
1074
1065
  raw: src.slice(0, lLength + match.index + rLength + 1),
1075
1066
  text: _text,
1076
- tokens: this.lexer.inlineTokens(_text, [])
1067
+ tokens: this.lexer.inlineTokens(_text)
1077
1068
  };
1078
1069
  } // Create 'strong' if smallest delimiter has even char count. **a***
1079
1070
 
@@ -1083,7 +1074,7 @@ var Tokenizer = /*#__PURE__*/function () {
1083
1074
  type: 'strong',
1084
1075
  raw: src.slice(0, lLength + match.index + rLength + 1),
1085
1076
  text: text,
1086
- tokens: this.lexer.inlineTokens(text, [])
1077
+ tokens: this.lexer.inlineTokens(text)
1087
1078
  };
1088
1079
  }
1089
1080
  }
@@ -1129,7 +1120,7 @@ var Tokenizer = /*#__PURE__*/function () {
1129
1120
  type: 'del',
1130
1121
  raw: cap[0],
1131
1122
  text: cap[2],
1132
- tokens: this.lexer.inlineTokens(cap[2], [])
1123
+ tokens: this.lexer.inlineTokens(cap[2])
1133
1124
  };
1134
1125
  }
1135
1126
  };
@@ -2728,22 +2719,32 @@ function marked(src, opt, callback) {
2728
2719
  return;
2729
2720
  }
2730
2721
 
2722
+ function onError(e) {
2723
+ e.message += '\nPlease report this to https://github.com/markedjs/marked.';
2724
+
2725
+ if (opt.silent) {
2726
+ return '<p>An error occurred:</p><pre>' + escape(e.message + '', true) + '</pre>';
2727
+ }
2728
+
2729
+ throw e;
2730
+ }
2731
+
2731
2732
  try {
2732
2733
  var _tokens = Lexer.lex(src, opt);
2733
2734
 
2734
2735
  if (opt.walkTokens) {
2736
+ if (opt.async) {
2737
+ return Promise.all(marked.walkTokens(_tokens, opt.walkTokens)).then(function () {
2738
+ return Parser.parse(_tokens, opt);
2739
+ })["catch"](onError);
2740
+ }
2741
+
2735
2742
  marked.walkTokens(_tokens, opt.walkTokens);
2736
2743
  }
2737
2744
 
2738
2745
  return Parser.parse(_tokens, opt);
2739
2746
  } catch (e) {
2740
- e.message += '\nPlease report this to https://github.com/markedjs/marked.';
2741
-
2742
- if (opt.silent) {
2743
- return '<p>An error occurred:</p><pre>' + escape(e.message + '', true) + '</pre>';
2744
- }
2745
-
2746
- throw e;
2747
+ onError(e);
2747
2748
  }
2748
2749
  }
2749
2750
  /**
@@ -2909,11 +2910,14 @@ marked.use = function () {
2909
2910
  var _walkTokens = marked.defaults.walkTokens;
2910
2911
 
2911
2912
  opts.walkTokens = function (token) {
2912
- pack.walkTokens.call(this, token);
2913
+ var values = [];
2914
+ values.push(pack.walkTokens.call(this, token));
2913
2915
 
2914
2916
  if (_walkTokens) {
2915
- _walkTokens.call(this, token);
2917
+ values = values.concat(_walkTokens.call(this, token));
2916
2918
  }
2919
+
2920
+ return values;
2917
2921
  };
2918
2922
  }
2919
2923
 
@@ -2930,16 +2934,18 @@ marked.use = function () {
2930
2934
 
2931
2935
 
2932
2936
  marked.walkTokens = function (tokens, callback) {
2937
+ var values = [];
2938
+
2933
2939
  var _loop3 = function _loop3() {
2934
2940
  var token = _step.value;
2935
- callback.call(marked, token);
2941
+ values = values.concat(callback.call(marked, token));
2936
2942
 
2937
2943
  switch (token.type) {
2938
2944
  case 'table':
2939
2945
  {
2940
2946
  for (var _iterator2 = _createForOfIteratorHelperLoose(token.header), _step2; !(_step2 = _iterator2()).done;) {
2941
2947
  var cell = _step2.value;
2942
- marked.walkTokens(cell.tokens, callback);
2948
+ values = values.concat(marked.walkTokens(cell.tokens, callback));
2943
2949
  }
2944
2950
 
2945
2951
  for (var _iterator3 = _createForOfIteratorHelperLoose(token.rows), _step3; !(_step3 = _iterator3()).done;) {
@@ -2947,7 +2953,7 @@ marked.walkTokens = function (tokens, callback) {
2947
2953
 
2948
2954
  for (var _iterator4 = _createForOfIteratorHelperLoose(row), _step4; !(_step4 = _iterator4()).done;) {
2949
2955
  var _cell = _step4.value;
2950
- marked.walkTokens(_cell.tokens, callback);
2956
+ values = values.concat(marked.walkTokens(_cell.tokens, callback));
2951
2957
  }
2952
2958
  }
2953
2959
 
@@ -2956,7 +2962,7 @@ marked.walkTokens = function (tokens, callback) {
2956
2962
 
2957
2963
  case 'list':
2958
2964
  {
2959
- marked.walkTokens(token.items, callback);
2965
+ values = values.concat(marked.walkTokens(token.items, callback));
2960
2966
  break;
2961
2967
  }
2962
2968
 
@@ -2965,10 +2971,10 @@ marked.walkTokens = function (tokens, callback) {
2965
2971
  if (marked.defaults.extensions && marked.defaults.extensions.childTokens && marked.defaults.extensions.childTokens[token.type]) {
2966
2972
  // Walk any extensions
2967
2973
  marked.defaults.extensions.childTokens[token.type].forEach(function (childTokens) {
2968
- marked.walkTokens(token[childTokens], callback);
2974
+ values = values.concat(marked.walkTokens(token[childTokens], callback));
2969
2975
  });
2970
2976
  } else if (token.tokens) {
2971
- marked.walkTokens(token.tokens, callback);
2977
+ values = values.concat(marked.walkTokens(token.tokens, callback));
2972
2978
  }
2973
2979
  }
2974
2980
  }
@@ -2977,6 +2983,8 @@ marked.walkTokens = function (tokens, callback) {
2977
2983
  for (var _iterator = _createForOfIteratorHelperLoose(tokens), _step; !(_step = _iterator()).done;) {
2978
2984
  _loop3();
2979
2985
  }
2986
+
2987
+ return values;
2980
2988
  };
2981
2989
  /**
2982
2990
  * 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,
@@ -25,7 +26,6 @@ function getDefaults() {
25
26
  sanitize: false,
26
27
  sanitizer: null,
27
28
  silent: false,
28
- smartLists: false,
29
29
  smartypants: false,
30
30
  tokenizer: null,
31
31
  walkTokens: null,
@@ -329,7 +329,7 @@ function outputLink(cap, link, raw, lexer) {
329
329
  href,
330
330
  title,
331
331
  text,
332
- tokens: lexer.inlineTokens(text, [])
332
+ tokens: lexer.inlineTokens(text)
333
333
  };
334
334
  lexer.state.inLink = false;
335
335
  return token;
@@ -435,15 +435,13 @@ class Tokenizer {
435
435
  }
436
436
  }
437
437
 
438
- const token = {
438
+ return {
439
439
  type: 'heading',
440
440
  raw: cap[0],
441
441
  depth: cap[1].length,
442
442
  text,
443
- tokens: []
443
+ tokens: this.lexer.inline(text)
444
444
  };
445
- this.lexer.inline(token.text, token.tokens);
446
- return token;
447
445
  }
448
446
  }
449
447
 
@@ -665,10 +663,10 @@ class Tokenizer {
665
663
  text: cap[0]
666
664
  };
667
665
  if (this.options.sanitize) {
666
+ const text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
668
667
  token.type = 'paragraph';
669
- token.text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
670
- token.tokens = [];
671
- this.lexer.inline(token.text, token.tokens);
668
+ token.text = text;
669
+ token.tokens = this.lexer.inline(text);
672
670
  }
673
671
  return token;
674
672
  }
@@ -726,8 +724,7 @@ class Tokenizer {
726
724
  // header child tokens
727
725
  l = item.header.length;
728
726
  for (j = 0; j < l; j++) {
729
- item.header[j].tokens = [];
730
- this.lexer.inline(item.header[j].text, item.header[j].tokens);
727
+ item.header[j].tokens = this.lexer.inline(item.header[j].text);
731
728
  }
732
729
 
733
730
  // cell child tokens
@@ -735,8 +732,7 @@ class Tokenizer {
735
732
  for (j = 0; j < l; j++) {
736
733
  row = item.rows[j];
737
734
  for (k = 0; k < row.length; k++) {
738
- row[k].tokens = [];
739
- this.lexer.inline(row[k].text, row[k].tokens);
735
+ row[k].tokens = this.lexer.inline(row[k].text);
740
736
  }
741
737
  }
742
738
 
@@ -748,45 +744,40 @@ class Tokenizer {
748
744
  lheading(src) {
749
745
  const cap = this.rules.block.lheading.exec(src);
750
746
  if (cap) {
751
- const token = {
747
+ return {
752
748
  type: 'heading',
753
749
  raw: cap[0],
754
750
  depth: cap[2].charAt(0) === '=' ? 1 : 2,
755
751
  text: cap[1],
756
- tokens: []
752
+ tokens: this.lexer.inline(cap[1])
757
753
  };
758
- this.lexer.inline(token.text, token.tokens);
759
- return token;
760
754
  }
761
755
  }
762
756
 
763
757
  paragraph(src) {
764
758
  const cap = this.rules.block.paragraph.exec(src);
765
759
  if (cap) {
766
- const token = {
760
+ const text = cap[1].charAt(cap[1].length - 1) === '\n'
761
+ ? cap[1].slice(0, -1)
762
+ : cap[1];
763
+ return {
767
764
  type: 'paragraph',
768
765
  raw: cap[0],
769
- text: cap[1].charAt(cap[1].length - 1) === '\n'
770
- ? cap[1].slice(0, -1)
771
- : cap[1],
772
- tokens: []
766
+ text,
767
+ tokens: this.lexer.inline(text)
773
768
  };
774
- this.lexer.inline(token.text, token.tokens);
775
- return token;
776
769
  }
777
770
  }
778
771
 
779
772
  text(src) {
780
773
  const cap = this.rules.block.text.exec(src);
781
774
  if (cap) {
782
- const token = {
775
+ return {
783
776
  type: 'text',
784
777
  raw: cap[0],
785
778
  text: cap[0],
786
- tokens: []
779
+ tokens: this.lexer.inline(cap[0])
787
780
  };
788
- this.lexer.inline(token.text, token.tokens);
789
- return token;
790
781
  }
791
782
  }
792
783
 
@@ -955,7 +946,7 @@ class Tokenizer {
955
946
  type: 'em',
956
947
  raw: src.slice(0, lLength + match.index + rLength + 1),
957
948
  text,
958
- tokens: this.lexer.inlineTokens(text, [])
949
+ tokens: this.lexer.inlineTokens(text)
959
950
  };
960
951
  }
961
952
 
@@ -965,7 +956,7 @@ class Tokenizer {
965
956
  type: 'strong',
966
957
  raw: src.slice(0, lLength + match.index + rLength + 1),
967
958
  text,
968
- tokens: this.lexer.inlineTokens(text, [])
959
+ tokens: this.lexer.inlineTokens(text)
969
960
  };
970
961
  }
971
962
  }
@@ -1006,7 +997,7 @@ class Tokenizer {
1006
997
  type: 'del',
1007
998
  raw: cap[0],
1008
999
  text: cap[2],
1009
- tokens: this.lexer.inlineTokens(cap[2], [])
1000
+ tokens: this.lexer.inlineTokens(cap[2])
1010
1001
  };
1011
1002
  }
1012
1003
  }
@@ -2555,13 +2546,7 @@ function marked(src, opt, callback) {
2555
2546
  return;
2556
2547
  }
2557
2548
 
2558
- try {
2559
- const tokens = Lexer.lex(src, opt);
2560
- if (opt.walkTokens) {
2561
- marked.walkTokens(tokens, opt.walkTokens);
2562
- }
2563
- return Parser.parse(tokens, opt);
2564
- } catch (e) {
2549
+ function onError(e) {
2565
2550
  e.message += '\nPlease report this to https://github.com/markedjs/marked.';
2566
2551
  if (opt.silent) {
2567
2552
  return '<p>An error occurred:</p><pre>'
@@ -2570,6 +2555,23 @@ function marked(src, opt, callback) {
2570
2555
  }
2571
2556
  throw e;
2572
2557
  }
2558
+
2559
+ try {
2560
+ const tokens = Lexer.lex(src, opt);
2561
+ if (opt.walkTokens) {
2562
+ if (opt.async) {
2563
+ return Promise.all(marked.walkTokens(tokens, opt.walkTokens))
2564
+ .then(() => {
2565
+ return Parser.parse(tokens, opt);
2566
+ })
2567
+ .catch(onError);
2568
+ }
2569
+ marked.walkTokens(tokens, opt.walkTokens);
2570
+ }
2571
+ return Parser.parse(tokens, opt);
2572
+ } catch (e) {
2573
+ onError(e);
2574
+ }
2573
2575
  }
2574
2576
 
2575
2577
  /**
@@ -2686,10 +2688,12 @@ marked.use = function(...args) {
2686
2688
  if (pack.walkTokens) {
2687
2689
  const walkTokens = marked.defaults.walkTokens;
2688
2690
  opts.walkTokens = function(token) {
2689
- pack.walkTokens.call(this, token);
2691
+ let values = [];
2692
+ values.push(pack.walkTokens.call(this, token));
2690
2693
  if (walkTokens) {
2691
- walkTokens.call(this, token);
2694
+ values = values.concat(walkTokens.call(this, token));
2692
2695
  }
2696
+ return values;
2693
2697
  };
2694
2698
  }
2695
2699
 
@@ -2706,35 +2710,37 @@ marked.use = function(...args) {
2706
2710
  */
2707
2711
 
2708
2712
  marked.walkTokens = function(tokens, callback) {
2713
+ let values = [];
2709
2714
  for (const token of tokens) {
2710
- callback.call(marked, token);
2715
+ values = values.concat(callback.call(marked, token));
2711
2716
  switch (token.type) {
2712
2717
  case 'table': {
2713
2718
  for (const cell of token.header) {
2714
- marked.walkTokens(cell.tokens, callback);
2719
+ values = values.concat(marked.walkTokens(cell.tokens, callback));
2715
2720
  }
2716
2721
  for (const row of token.rows) {
2717
2722
  for (const cell of row) {
2718
- marked.walkTokens(cell.tokens, callback);
2723
+ values = values.concat(marked.walkTokens(cell.tokens, callback));
2719
2724
  }
2720
2725
  }
2721
2726
  break;
2722
2727
  }
2723
2728
  case 'list': {
2724
- marked.walkTokens(token.items, callback);
2729
+ values = values.concat(marked.walkTokens(token.items, callback));
2725
2730
  break;
2726
2731
  }
2727
2732
  default: {
2728
2733
  if (marked.defaults.extensions && marked.defaults.extensions.childTokens && marked.defaults.extensions.childTokens[token.type]) { // Walk any extensions
2729
2734
  marked.defaults.extensions.childTokens[token.type].forEach(function(childTokens) {
2730
- marked.walkTokens(token[childTokens], callback);
2735
+ values = values.concat(marked.walkTokens(token[childTokens], callback));
2731
2736
  });
2732
2737
  } else if (token.tokens) {
2733
- marked.walkTokens(token.tokens, callback);
2738
+ values = values.concat(marked.walkTokens(token.tokens, callback));
2734
2739
  }
2735
2740
  }
2736
2741
  }
2737
2742
  }
2743
+ return values;
2738
2744
  };
2739
2745
 
2740
2746
  /**