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