marked 4.2.12 → 4.3.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
@@ -1,5 +1,5 @@
1
1
  /**
2
- * marked v4.2.12 - a markdown parser
2
+ * marked v4.3.0 - a markdown parser
3
3
  * Copyright (c) 2011-2023, Christopher Jeffrey. (MIT Licensed)
4
4
  * https://github.com/markedjs/marked
5
5
  */
@@ -28,6 +28,20 @@ function _createClass(Constructor, protoProps, staticProps) {
28
28
  });
29
29
  return Constructor;
30
30
  }
31
+ function _extends() {
32
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
33
+ for (var i = 1; i < arguments.length; i++) {
34
+ var source = arguments[i];
35
+ for (var key in source) {
36
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
37
+ target[key] = source[key];
38
+ }
39
+ }
40
+ }
41
+ return target;
42
+ };
43
+ return _extends.apply(this, arguments);
44
+ }
31
45
  function _unsupportedIterableToArray(o, minLen) {
32
46
  if (!o) return;
33
47
  if (typeof o === "string") return _arrayLikeToArray(o, minLen);
@@ -84,6 +98,7 @@ function getDefaults() {
84
98
  headerIds: true,
85
99
  headerPrefix: '',
86
100
  highlight: null,
101
+ hooks: null,
87
102
  langPrefix: 'language-',
88
103
  mangle: true,
89
104
  pedantic: false,
@@ -238,20 +253,6 @@ function resolveUrl(base, href) {
238
253
  var noopTest = {
239
254
  exec: function noopTest() {}
240
255
  };
241
- function merge(obj) {
242
- var i = 1,
243
- target,
244
- key;
245
- for (; i < arguments.length; i++) {
246
- target = arguments[i];
247
- for (key in target) {
248
- if (Object.prototype.hasOwnProperty.call(target, key)) {
249
- obj[key] = target[key];
250
- }
251
- }
252
- }
253
- return obj;
254
- }
255
256
  function splitCells(tableRow, count) {
256
257
  // ensure that every cell-delimiting pipe has a space
257
258
  // before it to distinguish it from an escaped pipe
@@ -1101,7 +1102,7 @@ var Tokenizer = /*#__PURE__*/function () {
1101
1102
  var block = {
1102
1103
  newline: /^(?: *(?:\n|$))+/,
1103
1104
  code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
1104
- fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
1105
+ fences: /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
1105
1106
  hr: /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/,
1106
1107
  heading: /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,
1107
1108
  blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
@@ -1143,13 +1144,13 @@ block.blockquote = edit(block.blockquote).replace('paragraph', block.paragraph).
1143
1144
  * Normal Block Grammar
1144
1145
  */
1145
1146
 
1146
- block.normal = merge({}, block);
1147
+ block.normal = _extends({}, block);
1147
1148
 
1148
1149
  /**
1149
1150
  * GFM Block Grammar
1150
1151
  */
1151
1152
 
1152
- block.gfm = merge({}, block.normal, {
1153
+ block.gfm = _extends({}, block.normal, {
1153
1154
  table: '^ *([^\\n ].*\\|.*)\\n' // Header
1154
1155
  + ' {0,3}(?:\\| *)?(:?-+:? *(?:\\| *:?-+:? *)*)(?:\\| *)?' // Align
1155
1156
  + '(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
@@ -1167,7 +1168,7 @@ block.gfm.paragraph = edit(block._paragraph).replace('hr', block.hr).replace('he
1167
1168
  * Pedantic grammar (original John Gruber's loose markdown specification)
1168
1169
  */
1169
1170
 
1170
- block.pedantic = merge({}, block.normal, {
1171
+ block.pedantic = _extends({}, block.normal, {
1171
1172
  html: edit('^ *(?:comment *(?:\\n|\\s*$)' + '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
1172
1173
  + '|<tag(?:"[^"]*"|\'[^\']*\'|\\s[^\'"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))').replace('comment', block._comment).replace(/tag/g, '(?!(?:' + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub' + '|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)' + '\\b)\\w+(?!:|[^\\w\\s@]*@)\\b').getRegex(),
1173
1174
  def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
@@ -1242,13 +1243,13 @@ inline.reflinkSearch = edit(inline.reflinkSearch, 'g').replace('reflink', inline
1242
1243
  * Normal Inline Grammar
1243
1244
  */
1244
1245
 
1245
- inline.normal = merge({}, inline);
1246
+ inline.normal = _extends({}, inline);
1246
1247
 
1247
1248
  /**
1248
1249
  * Pedantic Inline Grammar
1249
1250
  */
1250
1251
 
1251
- inline.pedantic = merge({}, inline.normal, {
1252
+ inline.pedantic = _extends({}, inline.normal, {
1252
1253
  strong: {
1253
1254
  start: /^__|\*\*/,
1254
1255
  middle: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
@@ -1269,7 +1270,7 @@ inline.pedantic = merge({}, inline.normal, {
1269
1270
  * GFM Inline Grammar
1270
1271
  */
1271
1272
 
1272
- inline.gfm = merge({}, inline.normal, {
1273
+ inline.gfm = _extends({}, inline.normal, {
1273
1274
  escape: edit(inline.escape).replace('])', '~|])').getRegex(),
1274
1275
  _extended_email: /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,
1275
1276
  url: /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,
@@ -1282,7 +1283,7 @@ inline.gfm.url = edit(inline.gfm.url, 'i').replace('email', inline.gfm._extended
1282
1283
  * GFM + Line Breaks Inline Grammar
1283
1284
  */
1284
1285
 
1285
- inline.breaks = merge({}, inline.gfm, {
1286
+ inline.breaks = _extends({}, inline.gfm, {
1286
1287
  br: edit(inline.br).replace('{2,}', '*').getRegex(),
1287
1288
  text: edit(inline.gfm.text).replace('\\b_', '\\b_| {2,}\\n').replace(/\{2,\}/g, '*').getRegex()
1288
1289
  });
@@ -2357,98 +2358,170 @@ var Parser = /*#__PURE__*/function () {
2357
2358
  return Parser;
2358
2359
  }();
2359
2360
 
2360
- /**
2361
- * Marked
2362
- */
2363
- function marked(src, opt, callback) {
2364
- // throw error in case of non string input
2365
- if (typeof src === 'undefined' || src === null) {
2366
- throw new Error('marked(): input parameter is undefined or null');
2367
- }
2368
- if (typeof src !== 'string') {
2369
- throw new Error('marked(): input parameter is of type ' + Object.prototype.toString.call(src) + ', string expected');
2361
+ var Hooks = /*#__PURE__*/function () {
2362
+ function Hooks(options) {
2363
+ this.options = options || exports.defaults;
2370
2364
  }
2371
- if (typeof opt === 'function') {
2372
- callback = opt;
2373
- opt = null;
2365
+ var _proto = Hooks.prototype;
2366
+ /**
2367
+ * Process markdown before marked
2368
+ */
2369
+ _proto.preprocess = function preprocess(markdown) {
2370
+ return markdown;
2374
2371
  }
2375
- opt = merge({}, marked.defaults, opt || {});
2376
- checkSanitizeDeprecation(opt);
2377
- if (callback) {
2378
- var highlight = opt.highlight;
2379
- var tokens;
2380
- try {
2381
- tokens = Lexer.lex(src, opt);
2382
- } catch (e) {
2383
- return callback(e);
2384
- }
2385
- var done = function done(err) {
2386
- var out;
2387
- if (!err) {
2388
- try {
2389
- if (opt.walkTokens) {
2390
- marked.walkTokens(tokens, opt.walkTokens);
2391
- }
2392
- out = Parser.parse(tokens, opt);
2393
- } catch (e) {
2394
- err = e;
2372
+
2373
+ /**
2374
+ * Process HTML after marked is finished
2375
+ */;
2376
+ _proto.postprocess = function postprocess(html) {
2377
+ return html;
2378
+ };
2379
+ return Hooks;
2380
+ }();
2381
+ Hooks.passThroughHooks = new Set(['preprocess', 'postprocess']);
2382
+
2383
+ function onError(silent, async, callback) {
2384
+ return function (e) {
2385
+ e.message += '\nPlease report this to https://github.com/markedjs/marked.';
2386
+ if (silent) {
2387
+ var msg = '<p>An error occurred:</p><pre>' + escape(e.message + '', true) + '</pre>';
2388
+ if (async) {
2389
+ return Promise.resolve(msg);
2390
+ }
2391
+ if (callback) {
2392
+ callback(null, msg);
2393
+ return;
2394
+ }
2395
+ return msg;
2396
+ }
2397
+ if (async) {
2398
+ return Promise.reject(e);
2399
+ }
2400
+ if (callback) {
2401
+ callback(e);
2402
+ return;
2403
+ }
2404
+ throw e;
2405
+ };
2406
+ }
2407
+ function parseMarkdown(lexer, parser) {
2408
+ return function (src, opt, callback) {
2409
+ if (typeof opt === 'function') {
2410
+ callback = opt;
2411
+ opt = null;
2412
+ }
2413
+ var origOpt = _extends({}, opt);
2414
+ opt = _extends({}, marked.defaults, origOpt);
2415
+ var throwError = onError(opt.silent, opt.async, callback);
2416
+
2417
+ // throw error in case of non string input
2418
+ if (typeof src === 'undefined' || src === null) {
2419
+ return throwError(new Error('marked(): input parameter is undefined or null'));
2420
+ }
2421
+ if (typeof src !== 'string') {
2422
+ return throwError(new Error('marked(): input parameter is of type ' + Object.prototype.toString.call(src) + ', string expected'));
2423
+ }
2424
+ checkSanitizeDeprecation(opt);
2425
+ if (opt.hooks) {
2426
+ opt.hooks.options = opt;
2427
+ }
2428
+ if (callback) {
2429
+ var highlight = opt.highlight;
2430
+ var tokens;
2431
+ try {
2432
+ if (opt.hooks) {
2433
+ src = opt.hooks.preprocess(src);
2395
2434
  }
2435
+ tokens = lexer(src, opt);
2436
+ } catch (e) {
2437
+ return throwError(e);
2396
2438
  }
2397
- opt.highlight = highlight;
2398
- return err ? callback(err) : callback(null, out);
2399
- };
2400
- if (!highlight || highlight.length < 3) {
2401
- return done();
2402
- }
2403
- delete opt.highlight;
2404
- if (!tokens.length) return done();
2405
- var pending = 0;
2406
- marked.walkTokens(tokens, function (token) {
2407
- if (token.type === 'code') {
2408
- pending++;
2409
- setTimeout(function () {
2410
- highlight(token.text, token.lang, function (err, code) {
2411
- if (err) {
2412
- return done(err);
2413
- }
2414
- if (code != null && code !== token.text) {
2415
- token.text = code;
2416
- token.escaped = true;
2439
+ var done = function done(err) {
2440
+ var out;
2441
+ if (!err) {
2442
+ try {
2443
+ if (opt.walkTokens) {
2444
+ marked.walkTokens(tokens, opt.walkTokens);
2417
2445
  }
2418
- pending--;
2419
- if (pending === 0) {
2420
- done();
2446
+ out = parser(tokens, opt);
2447
+ if (opt.hooks) {
2448
+ out = opt.hooks.postprocess(out);
2421
2449
  }
2422
- });
2423
- }, 0);
2450
+ } catch (e) {
2451
+ err = e;
2452
+ }
2453
+ }
2454
+ opt.highlight = highlight;
2455
+ return err ? throwError(err) : callback(null, out);
2456
+ };
2457
+ if (!highlight || highlight.length < 3) {
2458
+ return done();
2424
2459
  }
2425
- });
2426
- if (pending === 0) {
2427
- done();
2428
- }
2429
- return;
2430
- }
2431
- function onError(e) {
2432
- e.message += '\nPlease report this to https://github.com/markedjs/marked.';
2433
- if (opt.silent) {
2434
- return '<p>An error occurred:</p><pre>' + escape(e.message + '', true) + '</pre>';
2460
+ delete opt.highlight;
2461
+ if (!tokens.length) return done();
2462
+ var pending = 0;
2463
+ marked.walkTokens(tokens, function (token) {
2464
+ if (token.type === 'code') {
2465
+ pending++;
2466
+ setTimeout(function () {
2467
+ highlight(token.text, token.lang, function (err, code) {
2468
+ if (err) {
2469
+ return done(err);
2470
+ }
2471
+ if (code != null && code !== token.text) {
2472
+ token.text = code;
2473
+ token.escaped = true;
2474
+ }
2475
+ pending--;
2476
+ if (pending === 0) {
2477
+ done();
2478
+ }
2479
+ });
2480
+ }, 0);
2481
+ }
2482
+ });
2483
+ if (pending === 0) {
2484
+ done();
2485
+ }
2486
+ return;
2487
+ }
2488
+ if (opt.async) {
2489
+ return Promise.resolve(opt.hooks ? opt.hooks.preprocess(src) : src).then(function (src) {
2490
+ return lexer(src, opt);
2491
+ }).then(function (tokens) {
2492
+ return opt.walkTokens ? Promise.all(marked.walkTokens(tokens, opt.walkTokens)).then(function () {
2493
+ return tokens;
2494
+ }) : tokens;
2495
+ }).then(function (tokens) {
2496
+ return parser(tokens, opt);
2497
+ }).then(function (html) {
2498
+ return opt.hooks ? opt.hooks.postprocess(html) : html;
2499
+ })["catch"](throwError);
2435
2500
  }
2436
- throw e;
2437
- }
2438
- try {
2439
- var _tokens = Lexer.lex(src, opt);
2440
- if (opt.walkTokens) {
2441
- if (opt.async) {
2442
- return Promise.all(marked.walkTokens(_tokens, opt.walkTokens)).then(function () {
2443
- return Parser.parse(_tokens, opt);
2444
- })["catch"](onError);
2501
+ try {
2502
+ if (opt.hooks) {
2503
+ src = opt.hooks.preprocess(src);
2445
2504
  }
2446
- marked.walkTokens(_tokens, opt.walkTokens);
2505
+ var _tokens = lexer(src, opt);
2506
+ if (opt.walkTokens) {
2507
+ marked.walkTokens(_tokens, opt.walkTokens);
2508
+ }
2509
+ var html = parser(_tokens, opt);
2510
+ if (opt.hooks) {
2511
+ html = opt.hooks.postprocess(html);
2512
+ }
2513
+ return html;
2514
+ } catch (e) {
2515
+ return throwError(e);
2447
2516
  }
2448
- return Parser.parse(_tokens, opt);
2449
- } catch (e) {
2450
- onError(e);
2451
- }
2517
+ };
2518
+ }
2519
+
2520
+ /**
2521
+ * Marked
2522
+ */
2523
+ function marked(src, opt, callback) {
2524
+ return parseMarkdown(Lexer.lex, Parser.parse)(src, opt, callback);
2452
2525
  }
2453
2526
 
2454
2527
  /**
@@ -2456,7 +2529,7 @@ function marked(src, opt, callback) {
2456
2529
  */
2457
2530
 
2458
2531
  marked.options = marked.setOptions = function (opt) {
2459
- merge(marked.defaults, opt);
2532
+ marked.defaults = _extends({}, marked.defaults, opt);
2460
2533
  changeDefaults(marked.defaults);
2461
2534
  return marked;
2462
2535
  };
@@ -2477,10 +2550,10 @@ marked.use = function () {
2477
2550
  }
2478
2551
  args.forEach(function (pack) {
2479
2552
  // copy options to new object
2480
- var opts = merge({}, pack);
2553
+ var opts = _extends({}, pack);
2481
2554
 
2482
2555
  // set async to true if it was set to true before
2483
- opts.async = marked.defaults.async || opts.async;
2556
+ opts.async = marked.defaults.async || opts.async || false;
2484
2557
 
2485
2558
  // ==-- Parse "addon" extensions --== //
2486
2559
  if (pack.extensions) {
@@ -2590,6 +2663,42 @@ marked.use = function () {
2590
2663
  })();
2591
2664
  }
2592
2665
 
2666
+ // ==-- Parse Hooks extensions --== //
2667
+ if (pack.hooks) {
2668
+ (function () {
2669
+ var hooks = marked.defaults.hooks || new Hooks();
2670
+ var _loop3 = function _loop3(prop) {
2671
+ var prevHook = hooks[prop];
2672
+ if (Hooks.passThroughHooks.has(prop)) {
2673
+ hooks[prop] = function (arg) {
2674
+ if (marked.defaults.async) {
2675
+ return Promise.resolve(pack.hooks[prop].call(hooks, arg)).then(function (ret) {
2676
+ return prevHook.call(hooks, ret);
2677
+ });
2678
+ }
2679
+ var ret = pack.hooks[prop].call(hooks, arg);
2680
+ return prevHook.call(hooks, ret);
2681
+ };
2682
+ } else {
2683
+ hooks[prop] = function () {
2684
+ for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
2685
+ args[_key5] = arguments[_key5];
2686
+ }
2687
+ var ret = pack.hooks[prop].apply(hooks, args);
2688
+ if (ret === false) {
2689
+ ret = prevHook.apply(hooks, args);
2690
+ }
2691
+ return ret;
2692
+ };
2693
+ }
2694
+ };
2695
+ for (var prop in pack.hooks) {
2696
+ _loop3(prop);
2697
+ }
2698
+ opts.hooks = hooks;
2699
+ })();
2700
+ }
2701
+
2593
2702
  // ==-- Parse WalkTokens extensions --== //
2594
2703
  if (pack.walkTokens) {
2595
2704
  var _walkTokens = marked.defaults.walkTokens;
@@ -2612,7 +2721,7 @@ marked.use = function () {
2612
2721
 
2613
2722
  marked.walkTokens = function (tokens, callback) {
2614
2723
  var values = [];
2615
- var _loop3 = function _loop3() {
2724
+ var _loop4 = function _loop4() {
2616
2725
  var token = _step.value;
2617
2726
  values = values.concat(callback.call(marked, token));
2618
2727
  switch (token.type) {
@@ -2650,7 +2759,7 @@ marked.walkTokens = function (tokens, callback) {
2650
2759
  }
2651
2760
  };
2652
2761
  for (var _iterator = _createForOfIteratorHelperLoose(tokens), _step; !(_step = _iterator()).done;) {
2653
- _loop3();
2762
+ _loop4();
2654
2763
  }
2655
2764
  return values;
2656
2765
  };
@@ -2659,30 +2768,7 @@ marked.walkTokens = function (tokens, callback) {
2659
2768
  * Parse Inline
2660
2769
  * @param {string} src
2661
2770
  */
2662
- marked.parseInline = function (src, opt) {
2663
- // throw error in case of non string input
2664
- if (typeof src === 'undefined' || src === null) {
2665
- throw new Error('marked.parseInline(): input parameter is undefined or null');
2666
- }
2667
- if (typeof src !== 'string') {
2668
- throw new Error('marked.parseInline(): input parameter is of type ' + Object.prototype.toString.call(src) + ', string expected');
2669
- }
2670
- opt = merge({}, marked.defaults, opt || {});
2671
- checkSanitizeDeprecation(opt);
2672
- try {
2673
- var tokens = Lexer.lexInline(src, opt);
2674
- if (opt.walkTokens) {
2675
- marked.walkTokens(tokens, opt.walkTokens);
2676
- }
2677
- return Parser.parseInline(tokens, opt);
2678
- } catch (e) {
2679
- e.message += '\nPlease report this to https://github.com/markedjs/marked.';
2680
- if (opt.silent) {
2681
- return '<p>An error occurred:</p><pre>' + escape(e.message + '', true) + '</pre>';
2682
- }
2683
- throw e;
2684
- }
2685
- };
2771
+ marked.parseInline = parseMarkdown(Lexer.lexInline, Parser.parseInline);
2686
2772
 
2687
2773
  /**
2688
2774
  * Expose
@@ -2695,6 +2781,7 @@ marked.Lexer = Lexer;
2695
2781
  marked.lexer = Lexer.lex;
2696
2782
  marked.Tokenizer = Tokenizer;
2697
2783
  marked.Slugger = Slugger;
2784
+ marked.Hooks = Hooks;
2698
2785
  marked.parse = marked;
2699
2786
  var options = marked.options;
2700
2787
  var setOptions = marked.setOptions;
@@ -2705,6 +2792,7 @@ var parse = marked;
2705
2792
  var parser = Parser.parse;
2706
2793
  var lexer = Lexer.lex;
2707
2794
 
2795
+ exports.Hooks = Hooks;
2708
2796
  exports.Lexer = Lexer;
2709
2797
  exports.Parser = Parser;
2710
2798
  exports.Renderer = Renderer;