marked 11.0.1 → 11.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/lib/marked.d.cts CHANGED
@@ -335,6 +335,23 @@ declare class _Tokenizer {
335
335
  url(src: string): Tokens.Link | undefined;
336
336
  inlineText(src: string): Tokens.Text | undefined;
337
337
  }
338
+ declare class _Hooks {
339
+ options: MarkedOptions;
340
+ constructor(options?: MarkedOptions);
341
+ static passThroughHooks: Set<string>;
342
+ /**
343
+ * Process markdown before marked
344
+ */
345
+ preprocess(markdown: string): string;
346
+ /**
347
+ * Process HTML after marked is finished
348
+ */
349
+ postprocess(html: string): string;
350
+ /**
351
+ * Process all tokens before walk tokens
352
+ */
353
+ processAllTokens(tokens: Token[] | TokensList): Token[] | TokensList;
354
+ }
338
355
  export interface TokenizerThis {
339
356
  lexer: _Lexer;
340
357
  }
@@ -356,6 +373,10 @@ export interface RendererExtension {
356
373
  renderer: RendererExtensionFunction;
357
374
  }
358
375
  export type TokenizerAndRendererExtension = TokenizerExtension | RendererExtension | (TokenizerExtension & RendererExtension);
376
+ export type HooksApi = Omit<_Hooks, "constructor" | "options">;
377
+ export type HooksObject = {
378
+ [K in keyof HooksApi]?: (...args: Parameters<HooksApi[K]>) => ReturnType<HooksApi[K]> | Promise<ReturnType<HooksApi[K]>>;
379
+ };
359
380
  export type RendererApi = Omit<_Renderer, "constructor" | "options">;
360
381
  export type RendererObject = {
361
382
  [K in keyof RendererApi]?: (...args: Parameters<RendererApi[K]>) => ReturnType<RendererApi[K]> | false;
@@ -384,13 +405,10 @@ export interface MarkedExtension {
384
405
  /**
385
406
  * Hooks are methods that hook into some part of marked.
386
407
  * preprocess is called to process markdown before sending it to marked.
408
+ * processAllTokens is called with the TokensList before walkTokens.
387
409
  * postprocess is called to process html after marked has finished parsing.
388
410
  */
389
- hooks?: {
390
- preprocess: (markdown: string) => string | Promise<string>;
391
- postprocess: (html: string) => string | Promise<string>;
392
- options?: MarkedOptions;
393
- } | null;
411
+ hooks?: HooksObject | undefined | null;
394
412
  /**
395
413
  * Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.
396
414
  */
@@ -417,7 +435,11 @@ export interface MarkedExtension {
417
435
  */
418
436
  walkTokens?: ((token: Token) => void | Promise<void>) | undefined | null;
419
437
  }
420
- export interface MarkedOptions extends Omit<MarkedExtension, "renderer" | "tokenizer" | "extensions" | "walkTokens"> {
438
+ export interface MarkedOptions extends Omit<MarkedExtension, "hooks" | "renderer" | "tokenizer" | "extensions" | "walkTokens"> {
439
+ /**
440
+ * Hooks are methods that hook into some part of marked.
441
+ */
442
+ hooks?: _Hooks | undefined | null;
421
443
  /**
422
444
  * Type: object Default: new Renderer()
423
445
  *
@@ -532,19 +554,6 @@ declare class _Lexer {
532
554
  */
533
555
  inlineTokens(src: string, tokens?: Token[]): Token[];
534
556
  }
535
- declare class _Hooks {
536
- options: MarkedOptions;
537
- constructor(options?: MarkedOptions);
538
- static passThroughHooks: Set<string>;
539
- /**
540
- * Process markdown before marked
541
- */
542
- preprocess(markdown: string): string;
543
- /**
544
- * Process HTML after marked is finished
545
- */
546
- postprocess(html: string): string;
547
- }
548
557
  declare function _getDefaults(): MarkedOptions;
549
558
  declare let _defaults: MarkedOptions;
550
559
  export type MaybePromise = void | Promise<void>;
package/lib/marked.d.ts CHANGED
@@ -335,6 +335,23 @@ declare class _Tokenizer {
335
335
  url(src: string): Tokens.Link | undefined;
336
336
  inlineText(src: string): Tokens.Text | undefined;
337
337
  }
338
+ declare class _Hooks {
339
+ options: MarkedOptions;
340
+ constructor(options?: MarkedOptions);
341
+ static passThroughHooks: Set<string>;
342
+ /**
343
+ * Process markdown before marked
344
+ */
345
+ preprocess(markdown: string): string;
346
+ /**
347
+ * Process HTML after marked is finished
348
+ */
349
+ postprocess(html: string): string;
350
+ /**
351
+ * Process all tokens before walk tokens
352
+ */
353
+ processAllTokens(tokens: Token[] | TokensList): Token[] | TokensList;
354
+ }
338
355
  export interface TokenizerThis {
339
356
  lexer: _Lexer;
340
357
  }
@@ -356,6 +373,10 @@ export interface RendererExtension {
356
373
  renderer: RendererExtensionFunction;
357
374
  }
358
375
  export type TokenizerAndRendererExtension = TokenizerExtension | RendererExtension | (TokenizerExtension & RendererExtension);
376
+ export type HooksApi = Omit<_Hooks, "constructor" | "options">;
377
+ export type HooksObject = {
378
+ [K in keyof HooksApi]?: (...args: Parameters<HooksApi[K]>) => ReturnType<HooksApi[K]> | Promise<ReturnType<HooksApi[K]>>;
379
+ };
359
380
  export type RendererApi = Omit<_Renderer, "constructor" | "options">;
360
381
  export type RendererObject = {
361
382
  [K in keyof RendererApi]?: (...args: Parameters<RendererApi[K]>) => ReturnType<RendererApi[K]> | false;
@@ -384,13 +405,10 @@ export interface MarkedExtension {
384
405
  /**
385
406
  * Hooks are methods that hook into some part of marked.
386
407
  * preprocess is called to process markdown before sending it to marked.
408
+ * processAllTokens is called with the TokensList before walkTokens.
387
409
  * postprocess is called to process html after marked has finished parsing.
388
410
  */
389
- hooks?: {
390
- preprocess: (markdown: string) => string | Promise<string>;
391
- postprocess: (html: string) => string | Promise<string>;
392
- options?: MarkedOptions;
393
- } | null;
411
+ hooks?: HooksObject | undefined | null;
394
412
  /**
395
413
  * Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.
396
414
  */
@@ -417,7 +435,11 @@ export interface MarkedExtension {
417
435
  */
418
436
  walkTokens?: ((token: Token) => void | Promise<void>) | undefined | null;
419
437
  }
420
- export interface MarkedOptions extends Omit<MarkedExtension, "renderer" | "tokenizer" | "extensions" | "walkTokens"> {
438
+ export interface MarkedOptions extends Omit<MarkedExtension, "hooks" | "renderer" | "tokenizer" | "extensions" | "walkTokens"> {
439
+ /**
440
+ * Hooks are methods that hook into some part of marked.
441
+ */
442
+ hooks?: _Hooks | undefined | null;
421
443
  /**
422
444
  * Type: object Default: new Renderer()
423
445
  *
@@ -532,19 +554,6 @@ declare class _Lexer {
532
554
  */
533
555
  inlineTokens(src: string, tokens?: Token[]): Token[];
534
556
  }
535
- declare class _Hooks {
536
- options: MarkedOptions;
537
- constructor(options?: MarkedOptions);
538
- static passThroughHooks: Set<string>;
539
- /**
540
- * Process markdown before marked
541
- */
542
- preprocess(markdown: string): string;
543
- /**
544
- * Process HTML after marked is finished
545
- */
546
- postprocess(html: string): string;
547
- }
548
557
  declare function _getDefaults(): MarkedOptions;
549
558
  declare let _defaults: MarkedOptions;
550
559
  export type MaybePromise = void | Promise<void>;
package/lib/marked.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * marked v11.0.1 - a markdown parser
2
+ * marked v11.1.1 - a markdown parser
3
3
  * Copyright (c) 2011-2023, Christopher Jeffrey. (MIT Licensed)
4
4
  * https://github.com/markedjs/marked
5
5
  */
@@ -1260,10 +1260,11 @@ class _Lexer {
1260
1260
  src = src
1261
1261
  .replace(/\r\n|\r/g, '\n');
1262
1262
  this.blockTokens(src, this.tokens);
1263
- let next;
1264
- while (next = this.inlineQueue.shift()) {
1263
+ for (let i = 0; i < this.inlineQueue.length; i++) {
1264
+ const next = this.inlineQueue[i];
1265
1265
  this.inlineTokens(next.src, next.tokens);
1266
1266
  }
1267
+ this.inlineQueue = [];
1267
1268
  return this.tokens;
1268
1269
  }
1269
1270
  blockTokens(src, tokens = []) {
@@ -2022,7 +2023,8 @@ class _Hooks {
2022
2023
  }
2023
2024
  static passThroughHooks = new Set([
2024
2025
  'preprocess',
2025
- 'postprocess'
2026
+ 'postprocess',
2027
+ 'processAllTokens'
2026
2028
  ]);
2027
2029
  /**
2028
2030
  * Process markdown before marked
@@ -2036,6 +2038,12 @@ class _Hooks {
2036
2038
  postprocess(html) {
2037
2039
  return html;
2038
2040
  }
2041
+ /**
2042
+ * Process all tokens before walk tokens
2043
+ */
2044
+ processAllTokens(tokens) {
2045
+ return tokens;
2046
+ }
2039
2047
  }
2040
2048
 
2041
2049
  class Marked {
@@ -2222,6 +2230,7 @@ class Marked {
2222
2230
  const hooksFunc = pack.hooks[hooksProp];
2223
2231
  const prevHook = hooks[hooksProp];
2224
2232
  if (_Hooks.passThroughHooks.has(prop)) {
2233
+ // @ts-expect-error cannot type hook function dynamically
2225
2234
  hooks[hooksProp] = (arg) => {
2226
2235
  if (this.defaults.async) {
2227
2236
  return Promise.resolve(hooksFunc.call(hooks, arg)).then(ret => {
@@ -2233,6 +2242,7 @@ class Marked {
2233
2242
  };
2234
2243
  }
2235
2244
  else {
2245
+ // @ts-expect-error cannot type hook function dynamically
2236
2246
  hooks[hooksProp] = (...args) => {
2237
2247
  let ret = hooksFunc.apply(hooks, args);
2238
2248
  if (ret === false) {
@@ -2297,6 +2307,7 @@ class Marked {
2297
2307
  if (opt.async) {
2298
2308
  return Promise.resolve(opt.hooks ? opt.hooks.preprocess(src) : src)
2299
2309
  .then(src => lexer(src, opt))
2310
+ .then(tokens => opt.hooks ? opt.hooks.processAllTokens(tokens) : tokens)
2300
2311
  .then(tokens => opt.walkTokens ? Promise.all(this.walkTokens(tokens, opt.walkTokens)).then(() => tokens) : tokens)
2301
2312
  .then(tokens => parser(tokens, opt))
2302
2313
  .then(html => opt.hooks ? opt.hooks.postprocess(html) : html)
@@ -2306,7 +2317,10 @@ class Marked {
2306
2317
  if (opt.hooks) {
2307
2318
  src = opt.hooks.preprocess(src);
2308
2319
  }
2309
- const tokens = lexer(src, opt);
2320
+ let tokens = lexer(src, opt);
2321
+ if (opt.hooks) {
2322
+ tokens = opt.hooks.processAllTokens(tokens);
2323
+ }
2310
2324
  if (opt.walkTokens) {
2311
2325
  this.walkTokens(tokens, opt.walkTokens);
2312
2326
  }