marked 13.0.3 → 14.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
@@ -1,5 +1,5 @@
1
1
  /**
2
- * marked v13.0.3 - a markdown parser
2
+ * marked v14.1.0 - a markdown parser
3
3
  * Copyright (c) 2011-2024, Christopher Jeffrey. (MIT Licensed)
4
4
  * https://github.com/markedjs/marked
5
5
  */
@@ -61,21 +61,6 @@ function escape$1(html, encode) {
61
61
  }
62
62
  return html;
63
63
  }
64
- const unescapeTest = /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig;
65
- function unescape(html) {
66
- // explicitly match decimal, hex, and named HTML entities
67
- return html.replace(unescapeTest, (_, n) => {
68
- n = n.toLowerCase();
69
- if (n === 'colon')
70
- return ':';
71
- if (n.charAt(0) === '#') {
72
- return n.charAt(1) === 'x'
73
- ? String.fromCharCode(parseInt(n.substring(2), 16))
74
- : String.fromCharCode(+n.substring(1));
75
- }
76
- return '';
77
- });
78
- }
79
64
  const caret = /(^|[^\[])\^/g;
80
65
  function edit(regex, opt) {
81
66
  let source = typeof regex === 'string' ? regex : regex.source;
@@ -2093,6 +2078,7 @@ class _Parser {
2093
2078
 
2094
2079
  class _Hooks {
2095
2080
  options;
2081
+ block;
2096
2082
  constructor(options) {
2097
2083
  this.options = options || exports.defaults;
2098
2084
  }
@@ -2119,13 +2105,25 @@ class _Hooks {
2119
2105
  processAllTokens(tokens) {
2120
2106
  return tokens;
2121
2107
  }
2108
+ /**
2109
+ * Provide function to tokenize markdown
2110
+ */
2111
+ provideLexer() {
2112
+ return this.block ? _Lexer.lex : _Lexer.lexInline;
2113
+ }
2114
+ /**
2115
+ * Provide function to parse tokens
2116
+ */
2117
+ provideParser() {
2118
+ return this.block ? _Parser.parse : _Parser.parseInline;
2119
+ }
2122
2120
  }
2123
2121
 
2124
2122
  class Marked {
2125
2123
  defaults = _getDefaults();
2126
2124
  options = this.setOptions;
2127
- parse = this.#parseMarkdown(_Lexer.lex, _Parser.parse);
2128
- parseInline = this.#parseMarkdown(_Lexer.lexInline, _Parser.parseInline);
2125
+ parse = this.parseMarkdown(true);
2126
+ parseInline = this.parseMarkdown(false);
2129
2127
  Parser = _Parser;
2130
2128
  Renderer = _Renderer;
2131
2129
  TextRenderer = _TextRenderer;
@@ -2253,11 +2251,7 @@ class Marked {
2253
2251
  continue;
2254
2252
  }
2255
2253
  const rendererProp = prop;
2256
- let rendererFunc = pack.renderer[rendererProp];
2257
- if (!pack.useNewRenderer) {
2258
- // TODO: Remove this in next major version
2259
- rendererFunc = this.#convertRendererFunction(rendererFunc, rendererProp, renderer);
2260
- }
2254
+ const rendererFunc = pack.renderer[rendererProp];
2261
2255
  const prevRenderer = renderer[rendererProp];
2262
2256
  // Replace renderer with func to run extension, but fall back if false
2263
2257
  renderer[rendererProp] = (...args) => {
@@ -2302,8 +2296,8 @@ class Marked {
2302
2296
  if (!(prop in hooks)) {
2303
2297
  throw new Error(`hook '${prop}' does not exist`);
2304
2298
  }
2305
- if (prop === 'options') {
2306
- // ignore options property
2299
+ if (['options', 'block'].includes(prop)) {
2300
+ // ignore options and block properties
2307
2301
  continue;
2308
2302
  }
2309
2303
  const hooksProp = prop;
@@ -2351,215 +2345,6 @@ class Marked {
2351
2345
  });
2352
2346
  return this;
2353
2347
  }
2354
- // TODO: Remove this in next major release
2355
- #convertRendererFunction(func, prop, renderer) {
2356
- switch (prop) {
2357
- case 'heading':
2358
- return function (token) {
2359
- if (!token.type || token.type !== prop) {
2360
- // @ts-ignore
2361
- // eslint-disable-next-line prefer-rest-params
2362
- return func.apply(this, arguments);
2363
- }
2364
- return func.call(this, renderer.parser.parseInline(token.tokens), token.depth, unescape(renderer.parser.parseInline(token.tokens, renderer.parser.textRenderer)));
2365
- };
2366
- case 'code':
2367
- return function (token) {
2368
- if (!token.type || token.type !== prop) {
2369
- // @ts-ignore
2370
- // eslint-disable-next-line prefer-rest-params
2371
- return func.apply(this, arguments);
2372
- }
2373
- return func.call(this, token.text, token.lang, !!token.escaped);
2374
- };
2375
- case 'table':
2376
- return function (token) {
2377
- if (!token.type || token.type !== prop) {
2378
- // @ts-ignore
2379
- // eslint-disable-next-line prefer-rest-params
2380
- return func.apply(this, arguments);
2381
- }
2382
- let header = '';
2383
- // header
2384
- let cell = '';
2385
- for (let j = 0; j < token.header.length; j++) {
2386
- cell += this.tablecell({
2387
- text: token.header[j].text,
2388
- tokens: token.header[j].tokens,
2389
- header: true,
2390
- align: token.align[j],
2391
- });
2392
- }
2393
- header += this.tablerow({ text: cell });
2394
- let body = '';
2395
- for (let j = 0; j < token.rows.length; j++) {
2396
- const row = token.rows[j];
2397
- cell = '';
2398
- for (let k = 0; k < row.length; k++) {
2399
- cell += this.tablecell({
2400
- text: row[k].text,
2401
- tokens: row[k].tokens,
2402
- header: false,
2403
- align: token.align[k],
2404
- });
2405
- }
2406
- body += this.tablerow({ text: cell });
2407
- }
2408
- return func.call(this, header, body);
2409
- };
2410
- case 'blockquote':
2411
- return function (token) {
2412
- if (!token.type || token.type !== prop) {
2413
- // @ts-ignore
2414
- // eslint-disable-next-line prefer-rest-params
2415
- return func.apply(this, arguments);
2416
- }
2417
- const body = this.parser.parse(token.tokens);
2418
- return func.call(this, body);
2419
- };
2420
- case 'list':
2421
- return function (token) {
2422
- if (!token.type || token.type !== prop) {
2423
- // @ts-ignore
2424
- // eslint-disable-next-line prefer-rest-params
2425
- return func.apply(this, arguments);
2426
- }
2427
- const ordered = token.ordered;
2428
- const start = token.start;
2429
- const loose = token.loose;
2430
- let body = '';
2431
- for (let j = 0; j < token.items.length; j++) {
2432
- const item = token.items[j];
2433
- const checked = item.checked;
2434
- const task = item.task;
2435
- let itemBody = '';
2436
- if (item.task) {
2437
- const checkbox = this.checkbox({ checked: !!checked });
2438
- if (loose) {
2439
- if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
2440
- item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
2441
- if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
2442
- item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
2443
- }
2444
- }
2445
- else {
2446
- item.tokens.unshift({
2447
- type: 'text',
2448
- text: checkbox + ' ',
2449
- });
2450
- }
2451
- }
2452
- else {
2453
- itemBody += checkbox + ' ';
2454
- }
2455
- }
2456
- itemBody += this.parser.parse(item.tokens, loose);
2457
- body += this.listitem({
2458
- type: 'list_item',
2459
- raw: itemBody,
2460
- text: itemBody,
2461
- task,
2462
- checked: !!checked,
2463
- loose,
2464
- tokens: item.tokens,
2465
- });
2466
- }
2467
- return func.call(this, body, ordered, start);
2468
- };
2469
- case 'html':
2470
- return function (token) {
2471
- if (!token.type || token.type !== prop) {
2472
- // @ts-ignore
2473
- // eslint-disable-next-line prefer-rest-params
2474
- return func.apply(this, arguments);
2475
- }
2476
- return func.call(this, token.text, token.block);
2477
- };
2478
- case 'paragraph':
2479
- return function (token) {
2480
- if (!token.type || token.type !== prop) {
2481
- // @ts-ignore
2482
- // eslint-disable-next-line prefer-rest-params
2483
- return func.apply(this, arguments);
2484
- }
2485
- return func.call(this, this.parser.parseInline(token.tokens));
2486
- };
2487
- case 'escape':
2488
- return function (token) {
2489
- if (!token.type || token.type !== prop) {
2490
- // @ts-ignore
2491
- // eslint-disable-next-line prefer-rest-params
2492
- return func.apply(this, arguments);
2493
- }
2494
- return func.call(this, token.text);
2495
- };
2496
- case 'link':
2497
- return function (token) {
2498
- if (!token.type || token.type !== prop) {
2499
- // @ts-ignore
2500
- // eslint-disable-next-line prefer-rest-params
2501
- return func.apply(this, arguments);
2502
- }
2503
- return func.call(this, token.href, token.title, this.parser.parseInline(token.tokens));
2504
- };
2505
- case 'image':
2506
- return function (token) {
2507
- if (!token.type || token.type !== prop) {
2508
- // @ts-ignore
2509
- // eslint-disable-next-line prefer-rest-params
2510
- return func.apply(this, arguments);
2511
- }
2512
- return func.call(this, token.href, token.title, token.text);
2513
- };
2514
- case 'strong':
2515
- return function (token) {
2516
- if (!token.type || token.type !== prop) {
2517
- // @ts-ignore
2518
- // eslint-disable-next-line prefer-rest-params
2519
- return func.apply(this, arguments);
2520
- }
2521
- return func.call(this, this.parser.parseInline(token.tokens));
2522
- };
2523
- case 'em':
2524
- return function (token) {
2525
- if (!token.type || token.type !== prop) {
2526
- // @ts-ignore
2527
- // eslint-disable-next-line prefer-rest-params
2528
- return func.apply(this, arguments);
2529
- }
2530
- return func.call(this, this.parser.parseInline(token.tokens));
2531
- };
2532
- case 'codespan':
2533
- return function (token) {
2534
- if (!token.type || token.type !== prop) {
2535
- // @ts-ignore
2536
- // eslint-disable-next-line prefer-rest-params
2537
- return func.apply(this, arguments);
2538
- }
2539
- return func.call(this, token.text);
2540
- };
2541
- case 'del':
2542
- return function (token) {
2543
- if (!token.type || token.type !== prop) {
2544
- // @ts-ignore
2545
- // eslint-disable-next-line prefer-rest-params
2546
- return func.apply(this, arguments);
2547
- }
2548
- return func.call(this, this.parser.parseInline(token.tokens));
2549
- };
2550
- case 'text':
2551
- return function (token) {
2552
- if (!token.type || token.type !== prop) {
2553
- // @ts-ignore
2554
- // eslint-disable-next-line prefer-rest-params
2555
- return func.apply(this, arguments);
2556
- }
2557
- return func.call(this, token.text);
2558
- };
2559
- // do nothing
2560
- }
2561
- return func;
2562
- }
2563
2348
  setOptions(opt) {
2564
2349
  this.defaults = { ...this.defaults, ...opt };
2565
2350
  return this;
@@ -2570,18 +2355,16 @@ class Marked {
2570
2355
  parser(tokens, options) {
2571
2356
  return _Parser.parse(tokens, options ?? this.defaults);
2572
2357
  }
2573
- #parseMarkdown(lexer, parser) {
2574
- return (src, options) => {
2358
+ parseMarkdown(blockType) {
2359
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2360
+ const parse = (src, options) => {
2575
2361
  const origOpt = { ...options };
2576
2362
  const opt = { ...this.defaults, ...origOpt };
2577
- // Show warning if an extension set async to true but the parse was called with async: false
2363
+ const throwError = this.onError(!!opt.silent, !!opt.async);
2364
+ // throw error if an extension set async to true but parse was called with async: false
2578
2365
  if (this.defaults.async === true && origOpt.async === false) {
2579
- if (!opt.silent) {
2580
- console.warn('marked(): The async option was set to true by an extension. The async: false option sent to parse will be ignored.');
2581
- }
2582
- opt.async = true;
2366
+ return throwError(new Error('marked(): The async option was set to true by an extension. Remove async: false from the parse options object to return a Promise.'));
2583
2367
  }
2584
- const throwError = this.#onError(!!opt.silent, !!opt.async);
2585
2368
  // throw error in case of non string input
2586
2369
  if (typeof src === 'undefined' || src === null) {
2587
2370
  return throwError(new Error('marked(): input parameter is undefined or null'));
@@ -2592,7 +2375,10 @@ class Marked {
2592
2375
  }
2593
2376
  if (opt.hooks) {
2594
2377
  opt.hooks.options = opt;
2378
+ opt.hooks.block = blockType;
2595
2379
  }
2380
+ const lexer = opt.hooks ? opt.hooks.provideLexer() : (blockType ? _Lexer.lex : _Lexer.lexInline);
2381
+ const parser = opt.hooks ? opt.hooks.provideParser() : (blockType ? _Parser.parse : _Parser.parseInline);
2596
2382
  if (opt.async) {
2597
2383
  return Promise.resolve(opt.hooks ? opt.hooks.preprocess(src) : src)
2598
2384
  .then(src => lexer(src, opt))
@@ -2623,8 +2409,9 @@ class Marked {
2623
2409
  return throwError(e);
2624
2410
  }
2625
2411
  };
2412
+ return parse;
2626
2413
  }
2627
- #onError(silent, async) {
2414
+ onError(silent, async) {
2628
2415
  return (e) => {
2629
2416
  e.message += '\nPlease report this to https://github.com/markedjs/marked.';
2630
2417
  if (silent) {