marked 14.0.0 → 14.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
@@ -271,54 +271,6 @@ declare const inlineNormal: {
271
271
  url: RegExp;
272
272
  };
273
273
  export type InlineKeys = keyof typeof inlineNormal;
274
- /**
275
- * exports
276
- */
277
- export declare const block: {
278
- normal: {
279
- blockquote: RegExp;
280
- code: RegExp;
281
- def: RegExp;
282
- fences: RegExp;
283
- heading: RegExp;
284
- hr: RegExp;
285
- html: RegExp;
286
- lheading: RegExp;
287
- list: RegExp;
288
- newline: RegExp;
289
- paragraph: RegExp;
290
- table: RegExp;
291
- text: RegExp;
292
- };
293
- gfm: Record<"code" | "blockquote" | "hr" | "html" | "table" | "text" | "heading" | "list" | "paragraph" | "def" | "fences" | "lheading" | "newline", RegExp>;
294
- pedantic: Record<"code" | "blockquote" | "hr" | "html" | "table" | "text" | "heading" | "list" | "paragraph" | "def" | "fences" | "lheading" | "newline", RegExp>;
295
- };
296
- export declare const inline: {
297
- normal: {
298
- _backpedal: RegExp;
299
- anyPunctuation: RegExp;
300
- autolink: RegExp;
301
- blockSkip: RegExp;
302
- br: RegExp;
303
- code: RegExp;
304
- del: RegExp;
305
- emStrongLDelim: RegExp;
306
- emStrongRDelimAst: RegExp;
307
- emStrongRDelimUnd: RegExp;
308
- escape: RegExp;
309
- link: RegExp;
310
- nolink: RegExp;
311
- punctuation: RegExp;
312
- reflink: RegExp;
313
- reflinkSearch: RegExp;
314
- tag: RegExp;
315
- text: RegExp;
316
- url: RegExp;
317
- };
318
- gfm: Record<"link" | "code" | "url" | "br" | "del" | "text" | "escape" | "tag" | "reflink" | "autolink" | "nolink" | "_backpedal" | "anyPunctuation" | "blockSkip" | "emStrongLDelim" | "emStrongRDelimAst" | "emStrongRDelimUnd" | "punctuation" | "reflinkSearch", RegExp>;
319
- breaks: Record<"link" | "code" | "url" | "br" | "del" | "text" | "escape" | "tag" | "reflink" | "autolink" | "nolink" | "_backpedal" | "anyPunctuation" | "blockSkip" | "emStrongLDelim" | "emStrongRDelimAst" | "emStrongRDelimUnd" | "punctuation" | "reflinkSearch", RegExp>;
320
- pedantic: Record<"link" | "code" | "url" | "br" | "del" | "text" | "escape" | "tag" | "reflink" | "autolink" | "nolink" | "_backpedal" | "anyPunctuation" | "blockSkip" | "emStrongLDelim" | "emStrongRDelimAst" | "emStrongRDelimUnd" | "punctuation" | "reflinkSearch", RegExp>;
321
- };
322
274
  export interface Rules {
323
275
  block: Record<BlockKeys, RegExp>;
324
276
  inline: Record<InlineKeys, RegExp>;
@@ -358,6 +310,7 @@ declare class _Tokenizer {
358
310
  }
359
311
  declare class _Hooks {
360
312
  options: MarkedOptions;
313
+ block: boolean | undefined;
361
314
  constructor(options?: MarkedOptions);
362
315
  static passThroughHooks: Set<string>;
363
316
  /**
@@ -372,6 +325,14 @@ declare class _Hooks {
372
325
  * Process all tokens before walk tokens
373
326
  */
374
327
  processAllTokens(tokens: Token[] | TokensList): Token[] | TokensList;
328
+ /**
329
+ * Provide function to tokenize markdown
330
+ */
331
+ provideLexer(): typeof _Lexer.lexInline;
332
+ /**
333
+ * Provide function to parse tokens
334
+ */
335
+ provideParser(): typeof _Parser.parse;
375
336
  }
376
337
  export interface TokenizerThis {
377
338
  lexer: _Lexer;
@@ -394,7 +355,7 @@ export interface RendererExtension {
394
355
  renderer: RendererExtensionFunction;
395
356
  }
396
357
  export type TokenizerAndRendererExtension = TokenizerExtension | RendererExtension | (TokenizerExtension & RendererExtension);
397
- export type HooksApi = Omit<_Hooks, "constructor" | "options">;
358
+ export type HooksApi = Omit<_Hooks, "constructor" | "options" | "block">;
398
359
  export type HooksObject = {
399
360
  [K in keyof HooksApi]?: (this: _Hooks, ...args: Parameters<HooksApi[K]>) => ReturnType<HooksApi[K]> | Promise<ReturnType<HooksApi[K]>>;
400
361
  };
@@ -428,6 +389,8 @@ export interface MarkedExtension {
428
389
  * preprocess is called to process markdown before sending it to marked.
429
390
  * processAllTokens is called with the TokensList before walkTokens.
430
391
  * postprocess is called to process html after marked has finished parsing.
392
+ * provideLexer is called to provide a function to tokenize markdown.
393
+ * provideParser is called to provide a function to parse tokens.
431
394
  */
432
395
  hooks?: HooksObject | undefined | null;
433
396
  /**
package/lib/marked.d.ts CHANGED
@@ -271,54 +271,6 @@ declare const inlineNormal: {
271
271
  url: RegExp;
272
272
  };
273
273
  export type InlineKeys = keyof typeof inlineNormal;
274
- /**
275
- * exports
276
- */
277
- export declare const block: {
278
- normal: {
279
- blockquote: RegExp;
280
- code: RegExp;
281
- def: RegExp;
282
- fences: RegExp;
283
- heading: RegExp;
284
- hr: RegExp;
285
- html: RegExp;
286
- lheading: RegExp;
287
- list: RegExp;
288
- newline: RegExp;
289
- paragraph: RegExp;
290
- table: RegExp;
291
- text: RegExp;
292
- };
293
- gfm: Record<"code" | "blockquote" | "hr" | "html" | "table" | "text" | "heading" | "list" | "paragraph" | "def" | "fences" | "lheading" | "newline", RegExp>;
294
- pedantic: Record<"code" | "blockquote" | "hr" | "html" | "table" | "text" | "heading" | "list" | "paragraph" | "def" | "fences" | "lheading" | "newline", RegExp>;
295
- };
296
- export declare const inline: {
297
- normal: {
298
- _backpedal: RegExp;
299
- anyPunctuation: RegExp;
300
- autolink: RegExp;
301
- blockSkip: RegExp;
302
- br: RegExp;
303
- code: RegExp;
304
- del: RegExp;
305
- emStrongLDelim: RegExp;
306
- emStrongRDelimAst: RegExp;
307
- emStrongRDelimUnd: RegExp;
308
- escape: RegExp;
309
- link: RegExp;
310
- nolink: RegExp;
311
- punctuation: RegExp;
312
- reflink: RegExp;
313
- reflinkSearch: RegExp;
314
- tag: RegExp;
315
- text: RegExp;
316
- url: RegExp;
317
- };
318
- gfm: Record<"link" | "code" | "url" | "br" | "del" | "text" | "escape" | "tag" | "reflink" | "autolink" | "nolink" | "_backpedal" | "anyPunctuation" | "blockSkip" | "emStrongLDelim" | "emStrongRDelimAst" | "emStrongRDelimUnd" | "punctuation" | "reflinkSearch", RegExp>;
319
- breaks: Record<"link" | "code" | "url" | "br" | "del" | "text" | "escape" | "tag" | "reflink" | "autolink" | "nolink" | "_backpedal" | "anyPunctuation" | "blockSkip" | "emStrongLDelim" | "emStrongRDelimAst" | "emStrongRDelimUnd" | "punctuation" | "reflinkSearch", RegExp>;
320
- pedantic: Record<"link" | "code" | "url" | "br" | "del" | "text" | "escape" | "tag" | "reflink" | "autolink" | "nolink" | "_backpedal" | "anyPunctuation" | "blockSkip" | "emStrongLDelim" | "emStrongRDelimAst" | "emStrongRDelimUnd" | "punctuation" | "reflinkSearch", RegExp>;
321
- };
322
274
  export interface Rules {
323
275
  block: Record<BlockKeys, RegExp>;
324
276
  inline: Record<InlineKeys, RegExp>;
@@ -358,6 +310,7 @@ declare class _Tokenizer {
358
310
  }
359
311
  declare class _Hooks {
360
312
  options: MarkedOptions;
313
+ block: boolean | undefined;
361
314
  constructor(options?: MarkedOptions);
362
315
  static passThroughHooks: Set<string>;
363
316
  /**
@@ -372,6 +325,14 @@ declare class _Hooks {
372
325
  * Process all tokens before walk tokens
373
326
  */
374
327
  processAllTokens(tokens: Token[] | TokensList): Token[] | TokensList;
328
+ /**
329
+ * Provide function to tokenize markdown
330
+ */
331
+ provideLexer(): typeof _Lexer.lexInline;
332
+ /**
333
+ * Provide function to parse tokens
334
+ */
335
+ provideParser(): typeof _Parser.parse;
375
336
  }
376
337
  export interface TokenizerThis {
377
338
  lexer: _Lexer;
@@ -394,7 +355,7 @@ export interface RendererExtension {
394
355
  renderer: RendererExtensionFunction;
395
356
  }
396
357
  export type TokenizerAndRendererExtension = TokenizerExtension | RendererExtension | (TokenizerExtension & RendererExtension);
397
- export type HooksApi = Omit<_Hooks, "constructor" | "options">;
358
+ export type HooksApi = Omit<_Hooks, "constructor" | "options" | "block">;
398
359
  export type HooksObject = {
399
360
  [K in keyof HooksApi]?: (this: _Hooks, ...args: Parameters<HooksApi[K]>) => ReturnType<HooksApi[K]> | Promise<ReturnType<HooksApi[K]>>;
400
361
  };
@@ -428,6 +389,8 @@ export interface MarkedExtension {
428
389
  * preprocess is called to process markdown before sending it to marked.
429
390
  * processAllTokens is called with the TokensList before walkTokens.
430
391
  * postprocess is called to process html after marked has finished parsing.
392
+ * provideLexer is called to provide a function to tokenize markdown.
393
+ * provideParser is called to provide a function to parse tokens.
431
394
  */
432
395
  hooks?: HooksObject | undefined | null;
433
396
  /**
package/lib/marked.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * marked v14.0.0 - a markdown parser
2
+ * marked v14.1.1 - a markdown parser
3
3
  * Copyright (c) 2011-2024, Christopher Jeffrey. (MIT Licensed)
4
4
  * https://github.com/markedjs/marked
5
5
  */
@@ -247,7 +247,7 @@ class _Tokenizer {
247
247
  code(src) {
248
248
  const cap = this.rules.block.code.exec(src);
249
249
  if (cap) {
250
- const text = cap[0].replace(/^ {1,4}/gm, '');
250
+ const text = cap[0].replace(/^(?: {1,4}| {0,3}\t)/gm, '');
251
251
  return {
252
252
  type: 'code',
253
253
  raw: cap[0],
@@ -431,7 +431,7 @@ class _Tokenizer {
431
431
  itemContents = line.slice(indent);
432
432
  indent += cap[1].length;
433
433
  }
434
- if (blankLine && /^ *$/.test(nextLine)) { // Items begin with at most one blank line
434
+ if (blankLine && /^[ \t]*$/.test(nextLine)) { // Items begin with at most one blank line
435
435
  raw += nextLine + '\n';
436
436
  src = src.substring(nextLine.length + 1);
437
437
  endEarly = true;
@@ -444,10 +444,15 @@ class _Tokenizer {
444
444
  // Check if following lines should be included in List Item
445
445
  while (src) {
446
446
  const rawLine = src.split('\n', 1)[0];
447
+ let nextLineWithoutTabs;
447
448
  nextLine = rawLine;
448
449
  // Re-align to follow commonmark nesting rules
449
450
  if (this.options.pedantic) {
450
451
  nextLine = nextLine.replace(/^ {1,4}(?=( {4})*[^ ])/g, ' ');
452
+ nextLineWithoutTabs = nextLine;
453
+ }
454
+ else {
455
+ nextLineWithoutTabs = nextLine.replace(/\t/g, ' ');
451
456
  }
452
457
  // End list item if found code fences
453
458
  if (fencesBeginRegex.test(nextLine)) {
@@ -462,11 +467,11 @@ class _Tokenizer {
462
467
  break;
463
468
  }
464
469
  // Horizontal rule found
465
- if (hrRegex.test(src)) {
470
+ if (hrRegex.test(nextLine)) {
466
471
  break;
467
472
  }
468
- if (nextLine.search(/[^ ]/) >= indent || !nextLine.trim()) { // Dedent if possible
469
- itemContents += '\n' + nextLine.slice(indent);
473
+ if (nextLineWithoutTabs.search(/[^ ]/) >= indent || !nextLine.trim()) { // Dedent if possible
474
+ itemContents += '\n' + nextLineWithoutTabs.slice(indent);
470
475
  }
471
476
  else {
472
477
  // not enough indentation
@@ -474,7 +479,7 @@ class _Tokenizer {
474
479
  break;
475
480
  }
476
481
  // paragraph continuation unless last line was a different block level element
477
- if (line.search(/[^ ]/) >= 4) { // indented code block
482
+ if (line.replace(/\t/g, ' ').search(/[^ ]/) >= 4) { // indented code block
478
483
  break;
479
484
  }
480
485
  if (fencesBeginRegex.test(line)) {
@@ -493,7 +498,7 @@ class _Tokenizer {
493
498
  }
494
499
  raw += rawLine + '\n';
495
500
  src = src.substring(rawLine.length + 1);
496
- line = nextLine.slice(indent);
501
+ line = nextLineWithoutTabs.slice(indent);
497
502
  }
498
503
  }
499
504
  if (!list.loose) {
@@ -501,7 +506,7 @@ class _Tokenizer {
501
506
  if (endsWithBlankLine) {
502
507
  list.loose = true;
503
508
  }
504
- else if (/\n *\n *$/.test(raw)) {
509
+ else if (/\n[ \t]*\n[ \t]*$/.test(raw)) {
505
510
  endsWithBlankLine = true;
506
511
  }
507
512
  }
@@ -963,15 +968,15 @@ class _Tokenizer {
963
968
  /**
964
969
  * Block-Level Grammar
965
970
  */
966
- const newline = /^(?: *(?:\n|$))+/;
967
- const blockCode = /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/;
971
+ const newline = /^(?:[ \t]*(?:\n|$))+/;
972
+ const blockCode = /^((?: {4}| {0,3}\t)[^\n]+(?:\n(?:[ \t]*(?:\n|$))*)?)+/;
968
973
  const fences = /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/;
969
974
  const hr = /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/;
970
975
  const heading = /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/;
971
976
  const bullet = /(?:[*+-]|\d{1,9}[.)])/;
972
977
  const lheading = edit(/^(?!bull |blockCode|fences|blockquote|heading|html)((?:.|\n(?!\s*?\n|bull |blockCode|fences|blockquote|heading|html))+?)\n {0,3}(=+|-+) *(?:\n+|$)/)
973
978
  .replace(/bull/g, bullet) // lists can interrupt
974
- .replace(/blockCode/g, / {4}/) // indented code blocks can interrupt
979
+ .replace(/blockCode/g, /(?: {4}| {0,3}\t)/) // indented code blocks can interrupt
975
980
  .replace(/fences/g, / {0,3}(?:`{3,}|~{3,})/) // fenced code blocks can interrupt
976
981
  .replace(/blockquote/g, / {0,3}>/) // blockquote can interrupt
977
982
  .replace(/heading/g, / {0,3}#{1,6}/) // ATX heading can interrupt
@@ -980,7 +985,7 @@ const lheading = edit(/^(?!bull |blockCode|fences|blockquote|heading|html)((?:.|
980
985
  const _paragraph = /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/;
981
986
  const blockText = /^[^\n]+/;
982
987
  const _blockLabel = /(?!\s*\])(?:\\.|[^\[\]\\])+/;
983
- const def = edit(/^ {0,3}\[(label)\]: *(?:\n *)?([^<\s][^\s]*|<.*?>)(?:(?: +(?:\n *)?| *\n *)(title))? *(?:\n+|$)/)
988
+ const def = edit(/^ {0,3}\[(label)\]: *(?:\n[ \t]*)?([^<\s][^\s]*|<.*?>)(?:(?: +(?:\n[ \t]*)?| *\n[ \t]*)(title))? *(?:\n+|$)/)
984
989
  .replace('label', _blockLabel)
985
990
  .replace('title', /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/)
986
991
  .getRegex();
@@ -1000,9 +1005,9 @@ const html = edit('^ {0,3}(?:' // optional indentation
1000
1005
  + '|<\\?[\\s\\S]*?(?:\\?>\\n*|$)' // (3)
1001
1006
  + '|<![A-Z][\\s\\S]*?(?:>\\n*|$)' // (4)
1002
1007
  + '|<!\\[CDATA\\[[\\s\\S]*?(?:\\]\\]>\\n*|$)' // (5)
1003
- + '|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (6)
1004
- + '|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (7) open tag
1005
- + '|</(?!script|pre|style|textarea)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (7) closing tag
1008
+ + '|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (6)
1009
+ + '|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (7) open tag
1010
+ + '|</(?!script|pre|style|textarea)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (7) closing tag
1006
1011
  + ')', 'i')
1007
1012
  .replace('comment', _comment)
1008
1013
  .replace('tag', _tag)
@@ -1049,7 +1054,7 @@ const gfmTable = edit('^ *([^\\n ].*)\\n' // Header
1049
1054
  .replace('hr', hr)
1050
1055
  .replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
1051
1056
  .replace('blockquote', ' {0,3}>')
1052
- .replace('code', ' {4}[^\\n]')
1057
+ .replace('code', '(?: {4}| {0,3}\t)[^\\n]')
1053
1058
  .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
1054
1059
  .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
1055
1060
  .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
@@ -1329,11 +1334,6 @@ class _Lexer {
1329
1334
  if (this.options.pedantic) {
1330
1335
  src = src.replace(/\t/g, ' ').replace(/^ +$/gm, '');
1331
1336
  }
1332
- else {
1333
- src = src.replace(/^( *)(\t+)/gm, (_, leading, tabs) => {
1334
- return leading + ' '.repeat(tabs.length);
1335
- });
1336
- }
1337
1337
  let token;
1338
1338
  let lastToken;
1339
1339
  let cutSrc;
@@ -2076,6 +2076,7 @@ class _Parser {
2076
2076
 
2077
2077
  class _Hooks {
2078
2078
  options;
2079
+ block;
2079
2080
  constructor(options) {
2080
2081
  this.options = options || _defaults;
2081
2082
  }
@@ -2102,13 +2103,25 @@ class _Hooks {
2102
2103
  processAllTokens(tokens) {
2103
2104
  return tokens;
2104
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
+ }
2105
2118
  }
2106
2119
 
2107
2120
  class Marked {
2108
2121
  defaults = _getDefaults();
2109
2122
  options = this.setOptions;
2110
- parse = this.parseMarkdown(_Lexer.lex, _Parser.parse);
2111
- parseInline = this.parseMarkdown(_Lexer.lexInline, _Parser.parseInline);
2123
+ parse = this.parseMarkdown(true);
2124
+ parseInline = this.parseMarkdown(false);
2112
2125
  Parser = _Parser;
2113
2126
  Renderer = _Renderer;
2114
2127
  TextRenderer = _TextRenderer;
@@ -2281,8 +2294,8 @@ class Marked {
2281
2294
  if (!(prop in hooks)) {
2282
2295
  throw new Error(`hook '${prop}' does not exist`);
2283
2296
  }
2284
- if (prop === 'options') {
2285
- // ignore options property
2297
+ if (['options', 'block'].includes(prop)) {
2298
+ // ignore options and block properties
2286
2299
  continue;
2287
2300
  }
2288
2301
  const hooksProp = prop;
@@ -2340,7 +2353,7 @@ class Marked {
2340
2353
  parser(tokens, options) {
2341
2354
  return _Parser.parse(tokens, options ?? this.defaults);
2342
2355
  }
2343
- parseMarkdown(lexer, parser) {
2356
+ parseMarkdown(blockType) {
2344
2357
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
2345
2358
  const parse = (src, options) => {
2346
2359
  const origOpt = { ...options };
@@ -2360,7 +2373,10 @@ class Marked {
2360
2373
  }
2361
2374
  if (opt.hooks) {
2362
2375
  opt.hooks.options = opt;
2376
+ opt.hooks.block = blockType;
2363
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);
2364
2380
  if (opt.async) {
2365
2381
  return Promise.resolve(opt.hooks ? opt.hooks.preprocess(src) : src)
2366
2382
  .then(src => lexer(src, opt))