marked 4.0.12 → 4.0.15

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
@@ -70,6 +70,9 @@ function escape(html, encode) {
70
70
 
71
71
  const unescapeTest = /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig;
72
72
 
73
+ /**
74
+ * @param {string} html
75
+ */
73
76
  function unescape(html) {
74
77
  // explicitly match decimal, hex, and named HTML entities
75
78
  return html.replace(unescapeTest, (_, n) => {
@@ -85,8 +88,13 @@ function unescape(html) {
85
88
  }
86
89
 
87
90
  const caret = /(^|[^\[])\^/g;
91
+
92
+ /**
93
+ * @param {string | RegExp} regex
94
+ * @param {string} opt
95
+ */
88
96
  function edit(regex, opt) {
89
- regex = regex.source || regex;
97
+ regex = typeof regex === 'string' ? regex : regex.source;
90
98
  opt = opt || '';
91
99
  const obj = {
92
100
  replace: (name, val) => {
@@ -104,6 +112,12 @@ function edit(regex, opt) {
104
112
 
105
113
  const nonWordAndColonTest = /[^\w:]/g;
106
114
  const originIndependentUrl = /^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;
115
+
116
+ /**
117
+ * @param {boolean} sanitize
118
+ * @param {string} base
119
+ * @param {string} href
120
+ */
107
121
  function cleanUrl(sanitize, base, href) {
108
122
  if (sanitize) {
109
123
  let prot;
@@ -134,6 +148,10 @@ const justDomain = /^[^:]+:\/*[^/]*$/;
134
148
  const protocol = /^([^:]+:)[\s\S]*$/;
135
149
  const domain = /^([^:]+:\/*[^/]*)[\s\S]*$/;
136
150
 
151
+ /**
152
+ * @param {string} base
153
+ * @param {string} href
154
+ */
137
155
  function resolveUrl(base, href) {
138
156
  if (!baseUrls[' ' + base]) {
139
157
  // we can ignore everything in base after the last slash of its path component,
@@ -218,9 +236,14 @@ function splitCells(tableRow, count) {
218
236
  return cells;
219
237
  }
220
238
 
221
- // Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
222
- // /c*$/ is vulnerable to REDOS.
223
- // invert: Remove suffix of non-c chars instead. Default falsey.
239
+ /**
240
+ * Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
241
+ * /c*$/ is vulnerable to REDOS.
242
+ *
243
+ * @param {string} str
244
+ * @param {string} c
245
+ * @param {boolean} invert Remove suffix of non-c chars instead. Default falsey.
246
+ */
224
247
  function rtrim(str, c, invert) {
225
248
  const l = str.length;
226
249
  if (l === 0) {
@@ -242,7 +265,7 @@ function rtrim(str, c, invert) {
242
265
  }
243
266
  }
244
267
 
245
- return str.substr(0, l - suffLen);
268
+ return str.slice(0, l - suffLen);
246
269
  }
247
270
 
248
271
  function findClosingBracket(str, b) {
@@ -274,6 +297,10 @@ function checkSanitizeDeprecation(opt) {
274
297
  }
275
298
 
276
299
  // copied from https://stackoverflow.com/a/5450113/806777
300
+ /**
301
+ * @param {string} pattern
302
+ * @param {number} count
303
+ */
277
304
  function repeatString(pattern, count) {
278
305
  if (count < 1) {
279
306
  return '';
@@ -306,15 +333,14 @@ function outputLink(cap, link, raw, lexer) {
306
333
  };
307
334
  lexer.state.inLink = false;
308
335
  return token;
309
- } else {
310
- return {
311
- type: 'image',
312
- raw,
313
- href,
314
- title,
315
- text: escape(text)
316
- };
317
336
  }
337
+ return {
338
+ type: 'image',
339
+ raw,
340
+ href,
341
+ title,
342
+ text: escape(text)
343
+ };
318
344
  }
319
345
 
320
346
  function indentCodeCompensation(raw, text) {
@@ -413,7 +439,7 @@ class Tokenizer {
413
439
  type: 'heading',
414
440
  raw: cap[0],
415
441
  depth: cap[1].length,
416
- text: text,
442
+ text,
417
443
  tokens: []
418
444
  };
419
445
  this.lexer.inline(token.text, token.tokens);
@@ -434,7 +460,7 @@ class Tokenizer {
434
460
  blockquote(src) {
435
461
  const cap = this.rules.block.blockquote.exec(src);
436
462
  if (cap) {
437
- const text = cap[0].replace(/^ *> ?/gm, '');
463
+ const text = cap[0].replace(/^ *>[ \t]?/gm, '');
438
464
 
439
465
  return {
440
466
  type: 'blockquote',
@@ -470,7 +496,7 @@ class Tokenizer {
470
496
  }
471
497
 
472
498
  // Get next list item
473
- const itemRegex = new RegExp(`^( {0,3}${bull})((?: [^\\n]*)?(?:\\n|$))`);
499
+ const itemRegex = new RegExp(`^( {0,3}${bull})((?:[\t ][^\\n]*)?(?:\\n|$))`);
474
500
 
475
501
  // Check if current bullet point can start a new List Item
476
502
  while (src) {
@@ -508,7 +534,8 @@ class Tokenizer {
508
534
  }
509
535
 
510
536
  if (!endEarly) {
511
- const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])`);
537
+ const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])((?: [^\\n]*)?(?:\\n|$))`);
538
+ const hrRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`);
512
539
 
513
540
  // Check if following lines should be included in List Item
514
541
  while (src) {
@@ -525,6 +552,11 @@ class Tokenizer {
525
552
  break;
526
553
  }
527
554
 
555
+ // Horizontal rule found
556
+ if (hrRegex.test(src)) {
557
+ break;
558
+ }
559
+
528
560
  if (line.search(/[^ ]/) >= indent || !line.trim()) { // Dedent if possible
529
561
  itemContents += '\n' + line.slice(indent);
530
562
  } else if (!blankLine) { // Until blank line, item doesn't need indentation
@@ -562,7 +594,7 @@ class Tokenizer {
562
594
 
563
595
  list.items.push({
564
596
  type: 'list_item',
565
- raw: raw,
597
+ raw,
566
598
  task: !!istask,
567
599
  checked: ischecked,
568
600
  loose: false,
@@ -1057,10 +1089,10 @@ const block = {
1057
1089
  newline: /^(?: *(?:\n|$))+/,
1058
1090
  code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
1059
1091
  fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
1060
- hr: /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,
1092
+ hr: /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/,
1061
1093
  heading: /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,
1062
1094
  blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
1063
- list: /^( {0,3}bull)( [^\n]+?)?(?:\n|$)/,
1095
+ list: /^( {0,3}bull)([ \t][^\n]+?)?(?:\n|$)/,
1064
1096
  html: '^ {0,3}(?:' // optional indentation
1065
1097
  + '<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)' // (1)
1066
1098
  + '|comment[^\\n]*(\\n+|$)' // (2)
@@ -1214,9 +1246,9 @@ const inline = {
1214
1246
  emStrong: {
1215
1247
  lDelim: /^(?:\*+(?:([punct_])|[^\s*]))|^_+(?:([punct*])|([^\s_]))/,
1216
1248
  // (1) and (2) can only be a Right Delimiter. (3) and (4) can only be Left. (5) and (6) can be either Left or Right.
1217
- // () Skip orphan delim inside strong (1) #*** (2) a***#, a*** (3) #***a, ***a (4) ***# (5) #***# (6) a***a
1218
- rDelimAst: /^[^_*]*?\_\_[^_*]*?\*[^_*]*?(?=\_\_)|[punct_](\*+)(?=[\s]|$)|[^punct*_\s](\*+)(?=[punct_\s]|$)|[punct_\s](\*+)(?=[^punct*_\s])|[\s](\*+)(?=[punct_])|[punct_](\*+)(?=[punct_])|[^punct*_\s](\*+)(?=[^punct*_\s])/,
1219
- rDelimUnd: /^[^_*]*?\*\*[^_*]*?\_[^_*]*?(?=\*\*)|[punct*](\_+)(?=[\s]|$)|[^punct*_\s](\_+)(?=[punct*\s]|$)|[punct*\s](\_+)(?=[^punct*_\s])|[\s](\_+)(?=[punct*])|[punct*](\_+)(?=[punct*])/ // ^- Not allowed for _
1249
+ // () Skip orphan inside strong () Consume to delim (1) #*** (2) a***#, a*** (3) #***a, ***a (4) ***# (5) #***# (6) a***a
1250
+ rDelimAst: /^[^_*]*?\_\_[^_*]*?\*[^_*]*?(?=\_\_)|[^*]+(?=[^*])|[punct_](\*+)(?=[\s]|$)|[^punct*_\s](\*+)(?=[punct_\s]|$)|[punct_\s](\*+)(?=[^punct*_\s])|[\s](\*+)(?=[punct_])|[punct_](\*+)(?=[punct_])|[^punct*_\s](\*+)(?=[^punct*_\s])/,
1251
+ rDelimUnd: /^[^_*]*?\*\*[^_*]*?\_[^_*]*?(?=\*\*)|[^_]+(?=[^_])|[punct*](\_+)(?=[\s]|$)|[^punct*_\s](\_+)(?=[punct*\s]|$)|[punct*\s](\_+)(?=[^punct*_\s])|[\s](\_+)(?=[punct*])|[punct*](\_+)(?=[punct*])/ // ^- Not allowed for _
1220
1252
  },
1221
1253
  code: /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,
1222
1254
  br: /^( {2,}|\\)\n(?!\s*$)/,
@@ -1349,6 +1381,7 @@ inline.breaks = merge({}, inline.gfm, {
1349
1381
 
1350
1382
  /**
1351
1383
  * smartypants text replacement
1384
+ * @param {string} text
1352
1385
  */
1353
1386
  function smartypants(text) {
1354
1387
  return text
@@ -1370,6 +1403,7 @@ function smartypants(text) {
1370
1403
 
1371
1404
  /**
1372
1405
  * mangle email addresses
1406
+ * @param {string} text
1373
1407
  */
1374
1408
  function mangle(text) {
1375
1409
  let out = '',
@@ -1457,8 +1491,7 @@ class Lexer {
1457
1491
  */
1458
1492
  lex(src) {
1459
1493
  src = src
1460
- .replace(/\r\n|\r/g, '\n')
1461
- .replace(/\t/g, ' ');
1494
+ .replace(/\r\n|\r/g, '\n');
1462
1495
 
1463
1496
  this.blockTokens(src, this.tokens);
1464
1497
 
@@ -1475,8 +1508,13 @@ class Lexer {
1475
1508
  */
1476
1509
  blockTokens(src, tokens = []) {
1477
1510
  if (this.options.pedantic) {
1478
- src = src.replace(/^ +$/gm, '');
1511
+ src = src.replace(/\t/g, ' ').replace(/^ +$/gm, '');
1512
+ } else {
1513
+ src = src.replace(/^( *)(\t+)/gm, (_, leading, tabs) => {
1514
+ return leading + ' '.repeat(tabs.length);
1515
+ });
1479
1516
  }
1517
+
1480
1518
  let token, lastToken, cutSrc, lastParagraphClipped;
1481
1519
 
1482
1520
  while (src) {
@@ -1872,29 +1910,31 @@ class Renderer {
1872
1910
  + '</code></pre>\n';
1873
1911
  }
1874
1912
 
1913
+ /**
1914
+ * @param {string} quote
1915
+ */
1875
1916
  blockquote(quote) {
1876
- return '<blockquote>\n' + quote + '</blockquote>\n';
1917
+ return `<blockquote>\n${quote}</blockquote>\n`;
1877
1918
  }
1878
1919
 
1879
1920
  html(html) {
1880
1921
  return html;
1881
1922
  }
1882
1923
 
1924
+ /**
1925
+ * @param {string} text
1926
+ * @param {string} level
1927
+ * @param {string} raw
1928
+ * @param {any} slugger
1929
+ */
1883
1930
  heading(text, level, raw, slugger) {
1884
1931
  if (this.options.headerIds) {
1885
- return '<h'
1886
- + level
1887
- + ' id="'
1888
- + this.options.headerPrefix
1889
- + slugger.slug(raw)
1890
- + '">'
1891
- + text
1892
- + '</h'
1893
- + level
1894
- + '>\n';
1932
+ const id = this.options.headerPrefix + slugger.slug(raw);
1933
+ return `<h${level} id="${id}">${text}</h${level}>\n`;
1895
1934
  }
1935
+
1896
1936
  // ignore IDs
1897
- return '<h' + level + '>' + text + '</h' + level + '>\n';
1937
+ return `<h${level}>${text}</h${level}>\n`;
1898
1938
  }
1899
1939
 
1900
1940
  hr() {
@@ -1907,8 +1947,11 @@ class Renderer {
1907
1947
  return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
1908
1948
  }
1909
1949
 
1950
+ /**
1951
+ * @param {string} text
1952
+ */
1910
1953
  listitem(text) {
1911
- return '<li>' + text + '</li>\n';
1954
+ return `<li>${text}</li>\n`;
1912
1955
  }
1913
1956
 
1914
1957
  checkbox(checked) {
@@ -1919,12 +1962,19 @@ class Renderer {
1919
1962
  + '> ';
1920
1963
  }
1921
1964
 
1965
+ /**
1966
+ * @param {string} text
1967
+ */
1922
1968
  paragraph(text) {
1923
- return '<p>' + text + '</p>\n';
1969
+ return `<p>${text}</p>\n`;
1924
1970
  }
1925
1971
 
1972
+ /**
1973
+ * @param {string} header
1974
+ * @param {string} body
1975
+ */
1926
1976
  table(header, body) {
1927
- if (body) body = '<tbody>' + body + '</tbody>';
1977
+ if (body) body = `<tbody>${body}</tbody>`;
1928
1978
 
1929
1979
  return '<table>\n'
1930
1980
  + '<thead>\n'
@@ -1934,39 +1984,59 @@ class Renderer {
1934
1984
  + '</table>\n';
1935
1985
  }
1936
1986
 
1987
+ /**
1988
+ * @param {string} content
1989
+ */
1937
1990
  tablerow(content) {
1938
- return '<tr>\n' + content + '</tr>\n';
1991
+ return `<tr>\n${content}</tr>\n`;
1939
1992
  }
1940
1993
 
1941
1994
  tablecell(content, flags) {
1942
1995
  const type = flags.header ? 'th' : 'td';
1943
1996
  const tag = flags.align
1944
- ? '<' + type + ' align="' + flags.align + '">'
1945
- : '<' + type + '>';
1946
- return tag + content + '</' + type + '>\n';
1997
+ ? `<${type} align="${flags.align}">`
1998
+ : `<${type}>`;
1999
+ return tag + content + `</${type}>\n`;
1947
2000
  }
1948
2001
 
1949
- // span level renderer
2002
+ /**
2003
+ * span level renderer
2004
+ * @param {string} text
2005
+ */
1950
2006
  strong(text) {
1951
- return '<strong>' + text + '</strong>';
2007
+ return `<strong>${text}</strong>`;
1952
2008
  }
1953
2009
 
2010
+ /**
2011
+ * @param {string} text
2012
+ */
1954
2013
  em(text) {
1955
- return '<em>' + text + '</em>';
2014
+ return `<em>${text}</em>`;
1956
2015
  }
1957
2016
 
2017
+ /**
2018
+ * @param {string} text
2019
+ */
1958
2020
  codespan(text) {
1959
- return '<code>' + text + '</code>';
2021
+ return `<code>${text}</code>`;
1960
2022
  }
1961
2023
 
1962
2024
  br() {
1963
2025
  return this.options.xhtml ? '<br/>' : '<br>';
1964
2026
  }
1965
2027
 
2028
+ /**
2029
+ * @param {string} text
2030
+ */
1966
2031
  del(text) {
1967
- return '<del>' + text + '</del>';
2032
+ return `<del>${text}</del>`;
1968
2033
  }
1969
2034
 
2035
+ /**
2036
+ * @param {string} href
2037
+ * @param {string} title
2038
+ * @param {string} text
2039
+ */
1970
2040
  link(href, title, text) {
1971
2041
  href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
1972
2042
  if (href === null) {
@@ -1980,15 +2050,20 @@ class Renderer {
1980
2050
  return out;
1981
2051
  }
1982
2052
 
2053
+ /**
2054
+ * @param {string} href
2055
+ * @param {string} title
2056
+ * @param {string} text
2057
+ */
1983
2058
  image(href, title, text) {
1984
2059
  href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
1985
2060
  if (href === null) {
1986
2061
  return text;
1987
2062
  }
1988
2063
 
1989
- let out = '<img src="' + href + '" alt="' + text + '"';
2064
+ let out = `<img src="${href}" alt="${text}"`;
1990
2065
  if (title) {
1991
- out += ' title="' + title + '"';
2066
+ out += ` title="${title}"`;
1992
2067
  }
1993
2068
  out += this.options.xhtml ? '/>' : '>';
1994
2069
  return out;
@@ -2050,6 +2125,9 @@ class Slugger {
2050
2125
  this.seen = {};
2051
2126
  }
2052
2127
 
2128
+ /**
2129
+ * @param {string} value
2130
+ */
2053
2131
  serialize(value) {
2054
2132
  return value
2055
2133
  .toLowerCase()
@@ -2063,6 +2141,8 @@ class Slugger {
2063
2141
 
2064
2142
  /**
2065
2143
  * Finds the next safe (unique) slug to use
2144
+ * @param {string} originalSlug
2145
+ * @param {boolean} isDryRun
2066
2146
  */
2067
2147
  getNextSafeSlug(originalSlug, isDryRun) {
2068
2148
  let slug = originalSlug;
@@ -2083,8 +2163,9 @@ class Slugger {
2083
2163
 
2084
2164
  /**
2085
2165
  * Convert string to unique id
2086
- * @param {object} options
2087
- * @param {boolean} options.dryrun Generates the next unique slug without updating the internal accumulator.
2166
+ * @param {object} [options]
2167
+ * @param {boolean} [options.dryrun] Generates the next unique slug without
2168
+ * updating the internal accumulator.
2088
2169
  */
2089
2170
  slug(value, options = {}) {
2090
2171
  const slug = this.serialize(value);
@@ -2645,6 +2726,7 @@ marked.walkTokens = function(tokens, callback) {
2645
2726
 
2646
2727
  /**
2647
2728
  * Parse Inline
2729
+ * @param {string} src
2648
2730
  */
2649
2731
  marked.parseInline = function(src, opt) {
2650
2732
  // throw error in case of non string input