marked 7.0.1 → 7.0.2

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.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  declare module "Tokens" {
2
- export type Token = (Tokens.Space | Tokens.Code | Tokens.Heading | Tokens.Table | Tokens.Hr | Tokens.Blockquote | Tokens.List | Tokens.ListItem | Tokens.Paragraph | Tokens.HTML | Tokens.Text | Tokens.Def | Tokens.Escape | Tokens.Tag | Tokens.Image | Tokens.Link | Tokens.Strong | Tokens.Em | Tokens.Codespan | Tokens.Br | Tokens.Del) & {
2
+ export type Token = (Tokens.Space | Tokens.Code | Tokens.Heading | Tokens.Table | Tokens.Hr | Tokens.Blockquote | Tokens.List | Tokens.ListItem | Tokens.Paragraph | Tokens.HTML | Tokens.Text | Tokens.Def | Tokens.Escape | Tokens.Tag | Tokens.Image | Tokens.Link | Tokens.Strong | Tokens.Em | Tokens.Codespan | Tokens.Br | Tokens.Del | Tokens.Generic) & {
3
3
  loose?: boolean;
4
4
  tokens?: Token[];
5
5
  };
@@ -25,7 +25,7 @@ declare module "Tokens" {
25
25
  }
26
26
  interface Table {
27
27
  type: 'table';
28
- raw?: string;
28
+ raw: string;
29
29
  align: Array<'center' | 'left' | 'right' | null>;
30
30
  header: TableCell[];
31
31
  rows: TableCell[][];
@@ -156,7 +156,7 @@ declare module "Tokens" {
156
156
  };
157
157
  }
158
158
  declare module "Tokenizer" {
159
- import { _Lexer } from "Lexer";
159
+ import type { _Lexer } from "Lexer";
160
160
  import type { Links, Tokens } from "Tokens";
161
161
  import type { MarkedOptions } from "MarkedOptions";
162
162
  /**
@@ -309,7 +309,7 @@ declare module "Instance" {
309
309
  export class Marked {
310
310
  #private;
311
311
  defaults: MarkedOptions;
312
- options: (opt: any) => this;
312
+ options: (opt: MarkedOptions) => this;
313
313
  parse: (src: string, optOrCallback?: MarkedOptions | ResultCallback | undefined | null, callback?: ResultCallback | undefined) => string | Promise<string | undefined> | undefined;
314
314
  parseInline: (src: string, optOrCallback?: MarkedOptions | ResultCallback | undefined | null, callback?: ResultCallback | undefined) => string | Promise<string | undefined> | undefined;
315
315
  Parser: typeof _Parser;
@@ -327,7 +327,7 @@ declare module "Instance" {
327
327
  */
328
328
  walkTokens<T = void>(tokens: Token[] | TokensList, callback: (token: Token) => T | T[]): T[];
329
329
  use(...args: MarkedExtension[]): this;
330
- setOptions(opt: any): this;
330
+ setOptions(opt: MarkedOptions): this;
331
331
  }
332
332
  }
333
333
  declare module "helpers" {
@@ -345,7 +345,7 @@ declare module "helpers" {
345
345
  export const noopTest: {
346
346
  exec: () => null;
347
347
  };
348
- export function splitCells(tableRow: string, count: number): string[];
348
+ export function splitCells(tableRow: string, count?: number): string[];
349
349
  /**
350
350
  * Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
351
351
  * /c*$/ is vulnerable to REDOS.
@@ -358,101 +358,9 @@ declare module "helpers" {
358
358
  export function findClosingBracket(str: string, b: string): number;
359
359
  export function checkDeprecations(opt: MarkedOptions, callback?: ResultCallback): void;
360
360
  }
361
- declare module "marked" {
362
- import { _Lexer } from "Lexer";
363
- import { _Parser } from "Parser";
364
- import { _Tokenizer } from "Tokenizer";
365
- import { _Renderer } from "Renderer";
366
- import { _TextRenderer } from "TextRenderer";
367
- import { _Slugger } from "Slugger";
368
- import { _Hooks } from "Hooks";
369
- import { _getDefaults } from "defaults";
370
- import type { MarkedExtension, MarkedOptions } from "MarkedOptions";
371
- import type { Token, TokensList } from "Tokens";
372
- import type { ResultCallback } from "Instance";
373
- /**
374
- * Compiles markdown to HTML asynchronously.
375
- *
376
- * @param src String of markdown source to be compiled
377
- * @param options Hash of options, having async: true
378
- * @return Promise of string of compiled HTML
379
- */
380
- export function marked(src: string, options: MarkedOptions & {
381
- async: true;
382
- }): Promise<string>;
383
- /**
384
- * Compiles markdown to HTML synchronously.
385
- *
386
- * @param src String of markdown source to be compiled
387
- * @param options Optional hash of options
388
- * @return String of compiled HTML
389
- */
390
- export function marked(src: string, options?: MarkedOptions): string;
391
- /**
392
- * Compiles markdown to HTML asynchronously with a callback.
393
- *
394
- * @param src String of markdown source to be compiled
395
- * @param callback Function called when the markdownString has been fully parsed when using async highlighting
396
- */
397
- export function marked(src: string, callback: ResultCallback): void;
398
- /**
399
- * Compiles markdown to HTML asynchronously with a callback.
400
- *
401
- * @param src String of markdown source to be compiled
402
- * @param options Hash of options
403
- * @param callback Function called when the markdownString has been fully parsed when using async highlighting
404
- */
405
- export function marked(src: string, options: MarkedOptions, callback: ResultCallback): void;
406
- /**
407
- * Compiles markdown to HTML asynchronously with a callback.
408
- *
409
- * @param src String of markdown source to be compiled
410
- * @param options Hash of options
411
- * @param callback Function called when the markdownString has been fully parsed when using async highlighting
412
- */
413
- export namespace marked {
414
- var options: (options: MarkedOptions) => typeof marked;
415
- var setOptions: (options: MarkedOptions) => typeof marked;
416
- var getDefaults: typeof _getDefaults;
417
- var defaults: MarkedOptions;
418
- var use: (...args: MarkedExtension[]) => typeof marked;
419
- var walkTokens: <T = void>(tokens: Token[] | TokensList, callback: (token: Token) => T | T[]) => T[];
420
- var parseInline: (src: string, optOrCallback?: MarkedOptions | ResultCallback | null | undefined, callback?: ResultCallback | undefined) => string | Promise<string | undefined> | undefined;
421
- var Parser: typeof _Parser;
422
- var parser: typeof _Parser.parse;
423
- var Renderer: typeof _Renderer;
424
- var TextRenderer: typeof _TextRenderer;
425
- var Lexer: typeof _Lexer;
426
- var lexer: typeof _Lexer.lex;
427
- var Tokenizer: typeof _Tokenizer;
428
- var Slugger: typeof _Slugger;
429
- var Hooks: typeof _Hooks;
430
- var parse: typeof marked;
431
- }
432
- export const options: (options: MarkedOptions) => typeof marked;
433
- export const setOptions: (options: MarkedOptions) => typeof marked;
434
- export const use: (...args: MarkedExtension[]) => typeof marked;
435
- export const walkTokens: <T = void>(tokens: Token[] | TokensList, callback: (token: Token) => T | T[]) => T[];
436
- export const parseInline: (src: string, optOrCallback?: MarkedOptions | ResultCallback | null | undefined, callback?: ResultCallback | undefined) => string | Promise<string | undefined> | undefined;
437
- export const parse: typeof marked;
438
- export const parser: typeof _Parser.parse;
439
- export const lexer: typeof _Lexer.lex;
440
- export { _defaults as defaults, _getDefaults as getDefaults } from "defaults";
441
- export { _Lexer as Lexer } from "Lexer";
442
- export { _Parser as Parser } from "Parser";
443
- export { _Tokenizer as Tokenizer } from "Tokenizer";
444
- export { _Renderer as Renderer } from "Renderer";
445
- export { _TextRenderer as TextRenderer } from "TextRenderer";
446
- export { _Slugger as Slugger } from "Slugger";
447
- export { _Hooks as Hooks } from "Hooks";
448
- export { Marked } from "Instance";
449
- export type * from "MarkedOptions";
450
- export type * from "rules";
451
- export type * from "Tokens";
452
- }
453
361
  declare module "Renderer" {
454
362
  import type { MarkedOptions } from "MarkedOptions";
455
- import { Slugger } from "marked";
363
+ import type { _Slugger } from "Slugger";
456
364
  /**
457
365
  * Renderer
458
366
  */
@@ -462,7 +370,7 @@ declare module "Renderer" {
462
370
  code(code: string, infostring: string | undefined, escaped: boolean): string;
463
371
  blockquote(quote: string): string;
464
372
  html(html: string, block?: boolean): string;
465
- heading(text: string, level: number, raw: string, slugger: Slugger): string;
373
+ heading(text: string, level: number, raw: string, slugger: _Slugger): string;
466
374
  hr(): string;
467
375
  list(body: string, ordered: boolean, start: number | ''): string;
468
376
  listitem(text: string, task: boolean, checked: boolean): string;
@@ -522,10 +430,10 @@ declare module "Parser" {
522
430
  }
523
431
  declare module "MarkedOptions" {
524
432
  import type { Token, Tokens, TokensList } from "Tokens";
525
- import { _Parser } from "Parser";
526
- import { _Lexer } from "Lexer";
527
- import { _Renderer } from "Renderer";
528
- import { _Tokenizer } from "Tokenizer";
433
+ import type { _Parser } from "Parser";
434
+ import type { _Lexer } from "Lexer";
435
+ import type { _Renderer } from "Renderer";
436
+ import type { _Tokenizer } from "Tokenizer";
529
437
  export interface SluggerOptions {
530
438
  /** Generates the next unique slug without updating the internal accumulator. */
531
439
  dryrun?: boolean;
@@ -537,15 +445,16 @@ declare module "MarkedOptions" {
537
445
  name: string;
538
446
  level: 'block' | 'inline';
539
447
  start?: ((this: TokenizerThis, src: string) => number | void) | undefined;
540
- tokenizer: (this: TokenizerThis, src: string, tokens: Token[] | TokensList) => Tokens.Generic | void;
448
+ tokenizer: (this: TokenizerThis, src: string, tokens: Token[] | TokensList) => Tokens.Generic | undefined;
541
449
  childTokens?: string[] | undefined;
542
450
  }
543
451
  export interface RendererThis {
544
452
  parser: _Parser;
545
453
  }
454
+ export type RendererExtensionFunction = (this: RendererThis, token: Tokens.Generic) => string | false | undefined;
546
455
  export interface RendererExtension {
547
456
  name: string;
548
- renderer: (this: RendererThis, token: Tokens.Generic) => string | false | undefined;
457
+ renderer: RendererExtensionFunction;
549
458
  }
550
459
  export type TokenizerAndRendererExtension = TokenizerExtension | RendererExtension | (TokenizerExtension & RendererExtension);
551
460
  type RendererApi = Omit<_Renderer, 'constructor' | 'options'>;
@@ -602,8 +511,8 @@ declare module "MarkedOptions" {
602
511
  * postprocess is called to process html after marked has finished parsing.
603
512
  */
604
513
  hooks?: {
605
- preprocess: (markdown: string) => string;
606
- postprocess: (html: string | undefined) => string | undefined;
514
+ preprocess: (markdown: string) => string | Promise<string>;
515
+ postprocess: (html: string) => string | Promise<string>;
607
516
  options?: MarkedOptions;
608
517
  } | null;
609
518
  /**
@@ -688,7 +597,7 @@ declare module "MarkedOptions" {
688
597
  * Add tokenizers and renderers to marked
689
598
  */
690
599
  extensions?: (TokenizerAndRendererExtension[] & {
691
- renderers: Record<string, (this: RendererThis, token: Tokens.Generic) => string | false | undefined>;
600
+ renderers: Record<string, RendererExtensionFunction>;
692
601
  childTokens: Record<string, string[]>;
693
602
  block: any[];
694
603
  inline: any[];
@@ -719,6 +628,98 @@ declare module "Hooks" {
719
628
  /**
720
629
  * Process HTML after marked is finished
721
630
  */
722
- postprocess(html: string | undefined): string | undefined;
631
+ postprocess(html: string): string;
723
632
  }
724
633
  }
634
+ declare module "marked" {
635
+ import { _Lexer } from "Lexer";
636
+ import { _Parser } from "Parser";
637
+ import { _Tokenizer } from "Tokenizer";
638
+ import { _Renderer } from "Renderer";
639
+ import { _TextRenderer } from "TextRenderer";
640
+ import { _Slugger } from "Slugger";
641
+ import { _Hooks } from "Hooks";
642
+ import { _getDefaults } from "defaults";
643
+ import type { MarkedExtension, MarkedOptions } from "MarkedOptions";
644
+ import type { Token, TokensList } from "Tokens";
645
+ import type { ResultCallback } from "Instance";
646
+ /**
647
+ * Compiles markdown to HTML asynchronously.
648
+ *
649
+ * @param src String of markdown source to be compiled
650
+ * @param options Hash of options, having async: true
651
+ * @return Promise of string of compiled HTML
652
+ */
653
+ export function marked(src: string, options: MarkedOptions & {
654
+ async: true;
655
+ }): Promise<string>;
656
+ /**
657
+ * Compiles markdown to HTML synchronously.
658
+ *
659
+ * @param src String of markdown source to be compiled
660
+ * @param options Optional hash of options
661
+ * @return String of compiled HTML
662
+ */
663
+ export function marked(src: string, options?: MarkedOptions): string;
664
+ /**
665
+ * Compiles markdown to HTML asynchronously with a callback.
666
+ *
667
+ * @param src String of markdown source to be compiled
668
+ * @param callback Function called when the markdownString has been fully parsed when using async highlighting
669
+ */
670
+ export function marked(src: string, callback: ResultCallback): void;
671
+ /**
672
+ * Compiles markdown to HTML asynchronously with a callback.
673
+ *
674
+ * @param src String of markdown source to be compiled
675
+ * @param options Hash of options
676
+ * @param callback Function called when the markdownString has been fully parsed when using async highlighting
677
+ */
678
+ export function marked(src: string, options: MarkedOptions, callback: ResultCallback): void;
679
+ /**
680
+ * Compiles markdown to HTML asynchronously with a callback.
681
+ *
682
+ * @param src String of markdown source to be compiled
683
+ * @param options Hash of options
684
+ * @param callback Function called when the markdownString has been fully parsed when using async highlighting
685
+ */
686
+ export namespace marked {
687
+ var options: (options: MarkedOptions) => typeof marked;
688
+ var setOptions: (options: MarkedOptions) => typeof marked;
689
+ var getDefaults: typeof _getDefaults;
690
+ var defaults: MarkedOptions;
691
+ var use: (...args: MarkedExtension[]) => typeof marked;
692
+ var walkTokens: <T = void>(tokens: Token[] | TokensList, callback: (token: Token) => T | T[]) => T[];
693
+ var parseInline: (src: string, optOrCallback?: MarkedOptions | ResultCallback | null | undefined, callback?: ResultCallback | undefined) => string | Promise<string | undefined> | undefined;
694
+ var Parser: typeof _Parser;
695
+ var parser: typeof _Parser.parse;
696
+ var Renderer: typeof _Renderer;
697
+ var TextRenderer: typeof _TextRenderer;
698
+ var Lexer: typeof _Lexer;
699
+ var lexer: typeof _Lexer.lex;
700
+ var Tokenizer: typeof _Tokenizer;
701
+ var Slugger: typeof _Slugger;
702
+ var Hooks: typeof _Hooks;
703
+ var parse: typeof marked;
704
+ }
705
+ export const options: (options: MarkedOptions) => typeof marked;
706
+ export const setOptions: (options: MarkedOptions) => typeof marked;
707
+ export const use: (...args: MarkedExtension[]) => typeof marked;
708
+ export const walkTokens: <T = void>(tokens: Token[] | TokensList, callback: (token: Token) => T | T[]) => T[];
709
+ export const parseInline: (src: string, optOrCallback?: MarkedOptions | ResultCallback | null | undefined, callback?: ResultCallback | undefined) => string | Promise<string | undefined> | undefined;
710
+ export const parse: typeof marked;
711
+ export const parser: typeof _Parser.parse;
712
+ export const lexer: typeof _Lexer.lex;
713
+ export { _defaults as defaults, _getDefaults as getDefaults } from "defaults";
714
+ export { _Lexer as Lexer } from "Lexer";
715
+ export { _Parser as Parser } from "Parser";
716
+ export { _Tokenizer as Tokenizer } from "Tokenizer";
717
+ export { _Renderer as Renderer } from "Renderer";
718
+ export { _TextRenderer as TextRenderer } from "TextRenderer";
719
+ export { _Slugger as Slugger } from "Slugger";
720
+ export { _Hooks as Hooks } from "Hooks";
721
+ export { Marked } from "Instance";
722
+ export type * from "MarkedOptions";
723
+ export type * from "rules";
724
+ export type * from "Tokens";
725
+ }
package/lib/marked.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * marked v7.0.1 - a markdown parser
2
+ * marked v7.0.2 - a markdown parser
3
3
  * Copyright (c) 2011-2023, Christopher Jeffrey. (MIT Licensed)
4
4
  * https://github.com/markedjs/marked
5
5
  */
@@ -189,12 +189,14 @@ function splitCells(tableRow, count) {
189
189
  if (cells.length > 0 && !cells[cells.length - 1].trim()) {
190
190
  cells.pop();
191
191
  }
192
- if (cells.length > count) {
193
- cells.splice(count);
194
- }
195
- else {
196
- while (cells.length < count)
197
- cells.push('');
192
+ if (count) {
193
+ if (cells.length > count) {
194
+ cells.splice(count);
195
+ }
196
+ else {
197
+ while (cells.length < count)
198
+ cells.push('');
199
+ }
198
200
  }
199
201
  for (; i < cells.length; i++) {
200
202
  // leading or trailing whitespace is ignored per the gfm spec
@@ -627,8 +629,7 @@ class _Tokenizer {
627
629
  if (cap) {
628
630
  const item = {
629
631
  type: 'table',
630
- // splitCells expects a number as second argument
631
- // @ts-expect-error
632
+ raw: cap[0],
632
633
  header: splitCells(cap[1]).map(c => {
633
634
  return { text: c };
634
635
  }),
@@ -636,7 +637,6 @@ class _Tokenizer {
636
637
  rows: cap[3] && cap[3].trim() ? cap[3].replace(/\n[ \t]*$/, '').split('\n') : []
637
638
  };
638
639
  if (item.header.length === item.align.length) {
639
- item.raw = cap[0];
640
640
  let l = item.align.length;
641
641
  let i, j, k, row;
642
642
  for (i = 0; i < l; i++) {
@@ -1406,7 +1406,10 @@ class _Lexer {
1406
1406
  return leading + ' '.repeat(tabs.length);
1407
1407
  });
1408
1408
  }
1409
- let token, lastToken, cutSrc, lastParagraphClipped;
1409
+ let token;
1410
+ let lastToken;
1411
+ let cutSrc;
1412
+ let lastParagraphClipped;
1410
1413
  while (src) {
1411
1414
  if (this.options.extensions
1412
1415
  && this.options.extensions.block
@@ -2256,6 +2259,7 @@ class Marked {
2256
2259
  default: {
2257
2260
  if (this.defaults.extensions && this.defaults.extensions.childTokens && this.defaults.extensions.childTokens[token.type]) { // Walk any extensions
2258
2261
  this.defaults.extensions.childTokens[token.type].forEach((childTokens) => {
2262
+ // @ts-expect-error we assume token[childToken] is an array of tokens but we can't be sure
2259
2263
  values = values.concat(this.walkTokens(token[childTokens], callback));
2260
2264
  });
2261
2265
  }
@@ -2335,14 +2339,16 @@ class Marked {
2335
2339
  if (pack.renderer) {
2336
2340
  const renderer = this.defaults.renderer || new _Renderer(this.defaults);
2337
2341
  for (const prop in pack.renderer) {
2338
- const prevRenderer = renderer[prop];
2342
+ const rendererFunc = pack.renderer[prop];
2343
+ const rendererKey = prop;
2344
+ const prevRenderer = renderer[rendererKey];
2339
2345
  // Replace renderer with func to run extension, but fall back if false
2340
- renderer[prop] = (...args) => {
2341
- let ret = pack.renderer[prop].apply(renderer, args);
2346
+ renderer[rendererKey] = (...args) => {
2347
+ let ret = rendererFunc.apply(renderer, args);
2342
2348
  if (ret === false) {
2343
2349
  ret = prevRenderer.apply(renderer, args);
2344
2350
  }
2345
- return ret;
2351
+ return ret || '';
2346
2352
  };
2347
2353
  }
2348
2354
  opts.renderer = renderer;
@@ -2350,10 +2356,12 @@ class Marked {
2350
2356
  if (pack.tokenizer) {
2351
2357
  const tokenizer = this.defaults.tokenizer || new _Tokenizer(this.defaults);
2352
2358
  for (const prop in pack.tokenizer) {
2353
- const prevTokenizer = tokenizer[prop];
2359
+ const tokenizerFunc = pack.tokenizer[prop];
2360
+ const tokenizerKey = prop;
2361
+ const prevTokenizer = tokenizer[tokenizerKey];
2354
2362
  // Replace tokenizer with func to run extension, but fall back if false
2355
- tokenizer[prop] = (...args) => {
2356
- let ret = pack.tokenizer[prop].apply(tokenizer, args);
2363
+ tokenizer[tokenizerKey] = (...args) => {
2364
+ let ret = tokenizerFunc.apply(tokenizer, args);
2357
2365
  if (ret === false) {
2358
2366
  ret = prevTokenizer.apply(tokenizer, args);
2359
2367
  }
@@ -2366,21 +2374,23 @@ class Marked {
2366
2374
  if (pack.hooks) {
2367
2375
  const hooks = this.defaults.hooks || new _Hooks();
2368
2376
  for (const prop in pack.hooks) {
2369
- const prevHook = hooks[prop];
2377
+ const hooksFunc = pack.hooks[prop];
2378
+ const hooksKey = prop;
2379
+ const prevHook = hooks[hooksKey];
2370
2380
  if (_Hooks.passThroughHooks.has(prop)) {
2371
- hooks[prop] = (arg) => {
2381
+ hooks[hooksKey] = (arg) => {
2372
2382
  if (this.defaults.async) {
2373
- return Promise.resolve(pack.hooks[prop].call(hooks, arg)).then(ret => {
2383
+ return Promise.resolve(hooksFunc.call(hooks, arg)).then(ret => {
2374
2384
  return prevHook.call(hooks, ret);
2375
2385
  });
2376
2386
  }
2377
- const ret = pack.hooks[prop].call(hooks, arg);
2387
+ const ret = hooksFunc.call(hooks, arg);
2378
2388
  return prevHook.call(hooks, ret);
2379
2389
  };
2380
2390
  }
2381
2391
  else {
2382
- hooks[prop] = (...args) => {
2383
- let ret = pack.hooks[prop].apply(hooks, args);
2392
+ hooks[hooksKey] = (...args) => {
2393
+ let ret = hooksFunc.apply(hooks, args);
2384
2394
  if (ret === false) {
2385
2395
  ret = prevHook.apply(hooks, args);
2386
2396
  }