marked 1.1.0 → 1.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.esm.js CHANGED
@@ -306,6 +306,7 @@ const {
306
306
  function outputLink(cap, link, raw) {
307
307
  const href = link.href;
308
308
  const title = link.title ? escape$1(link.title) : null;
309
+ const text = cap[1].replace(/\\([\[\]])/g, '$1');
309
310
 
310
311
  if (cap[0].charAt(0) !== '!') {
311
312
  return {
@@ -313,15 +314,15 @@ function outputLink(cap, link, raw) {
313
314
  raw,
314
315
  href,
315
316
  title,
316
- text: cap[1]
317
+ text
317
318
  };
318
319
  } else {
319
320
  return {
320
321
  type: 'image',
321
322
  raw,
322
- text: escape$1(cap[1]),
323
323
  href,
324
- title
324
+ title,
325
+ text: escape$1(text)
325
326
  };
326
327
  }
327
328
  }
@@ -491,12 +492,13 @@ var Tokenizer_1 = class Tokenizer {
491
492
  let raw = cap[0];
492
493
  const bull = cap[2];
493
494
  const isordered = bull.length > 1;
495
+ const isparen = bull[bull.length - 1] === ')';
494
496
 
495
497
  const list = {
496
498
  type: 'list',
497
499
  raw,
498
500
  ordered: isordered,
499
- start: isordered ? +bull : '',
501
+ start: isordered ? +bull.slice(0, -1) : '',
500
502
  loose: false,
501
503
  items: []
502
504
  };
@@ -521,7 +523,7 @@ var Tokenizer_1 = class Tokenizer {
521
523
  // Remove the list item's bullet
522
524
  // so it is seen as the next token.
523
525
  space = item.length;
524
- item = item.replace(/^ *([*+-]|\d+\.) */, '');
526
+ item = item.replace(/^ *([*+-]|\d+[.)]) */, '');
525
527
 
526
528
  // Outdent whatever the
527
529
  // list item contains. Hacky.
@@ -536,7 +538,7 @@ var Tokenizer_1 = class Tokenizer {
536
538
  // Backpedal if it does not belong in this list.
537
539
  if (i !== l - 1) {
538
540
  b = this.rules.block.bullet.exec(itemMatch[i + 1])[0];
539
- if (bull.length > 1 ? b.length === 1
541
+ if (isordered ? b.length === 1 || (!isparen && b[b.length - 1] === ')')
540
542
  : (b.length > 1 || (this.options.smartLists && b !== bull))) {
541
543
  addBack = itemMatch.slice(i + 1).join('\n');
542
544
  list.raw = list.raw.substring(0, list.raw.length - addBack.length);
@@ -785,25 +787,49 @@ var Tokenizer_1 = class Tokenizer {
785
787
  }
786
788
  }
787
789
 
788
- strong(src) {
789
- const cap = this.rules.inline.strong.exec(src);
790
- if (cap) {
791
- return {
792
- type: 'strong',
793
- raw: cap[0],
794
- text: cap[4] || cap[3] || cap[2] || cap[1]
795
- };
790
+ strong(src, maskedSrc, prevChar = '') {
791
+ let match = this.rules.inline.strong.start.exec(src);
792
+
793
+ if (match && (!match[1] || (match[1] && (prevChar === '' || this.rules.inline.punctuation.exec(prevChar))))) {
794
+ maskedSrc = maskedSrc.slice(-1 * src.length);
795
+ const endReg = match[0] === '**' ? this.rules.inline.strong.endAst : this.rules.inline.strong.endUnd;
796
+
797
+ endReg.lastIndex = 0;
798
+
799
+ let cap;
800
+ while ((match = endReg.exec(maskedSrc)) != null) {
801
+ cap = this.rules.inline.strong.middle.exec(maskedSrc.slice(0, match.index + 3));
802
+ if (cap) {
803
+ return {
804
+ type: 'strong',
805
+ raw: src.slice(0, cap[0].length),
806
+ text: src.slice(2, cap[0].length - 2)
807
+ };
808
+ }
809
+ }
796
810
  }
797
811
  }
798
812
 
799
- em(src) {
800
- const cap = this.rules.inline.em.exec(src);
801
- if (cap) {
802
- return {
803
- type: 'em',
804
- raw: cap[0],
805
- text: cap[6] || cap[5] || cap[4] || cap[3] || cap[2] || cap[1]
806
- };
813
+ em(src, maskedSrc, prevChar = '') {
814
+ let match = this.rules.inline.em.start.exec(src);
815
+
816
+ if (match && (!match[1] || (match[1] && (prevChar === '' || this.rules.inline.punctuation.exec(prevChar))))) {
817
+ maskedSrc = maskedSrc.slice(-1 * src.length);
818
+ const endReg = match[0] === '*' ? this.rules.inline.em.endAst : this.rules.inline.em.endUnd;
819
+
820
+ endReg.lastIndex = 0;
821
+
822
+ let cap;
823
+ while ((match = endReg.exec(maskedSrc)) != null) {
824
+ cap = this.rules.inline.em.middle.exec(maskedSrc.slice(0, match.index + 2));
825
+ if (cap) {
826
+ return {
827
+ type: 'em',
828
+ raw: src.slice(0, cap[0].length),
829
+ text: src.slice(1, cap[0].length - 1)
830
+ };
831
+ }
832
+ }
807
833
  }
808
834
  }
809
835
 
@@ -973,7 +999,7 @@ block.def = edit$1(block.def)
973
999
  .replace('title', block._title)
974
1000
  .getRegex();
975
1001
 
976
- block.bullet = /(?:[*+-]|\d{1,9}\.)/;
1002
+ block.bullet = /(?:[*+-]|\d{1,9}[.)])/;
977
1003
  block.item = /^( *)(bull) ?[^\n]*(?:\n(?!\1bull ?)[^\n]*)*/;
978
1004
  block.item = edit$1(block.item, 'gm')
979
1005
  .replace(/bull/g, block.bullet)
@@ -1099,19 +1125,74 @@ const inline = {
1099
1125
  link: /^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/,
1100
1126
  reflink: /^!?\[(label)\]\[(?!\s*\])((?:\\[\[\]]?|[^\[\]\\])+)\]/,
1101
1127
  nolink: /^!?\[(?!\s*\])((?:\[[^\[\]]*\]|\\[\[\]]|[^\[\]])*)\](?:\[\])?/,
1102
- strong: /^__([^\s_])__(?!_)|^\*\*([^\s*])\*\*(?!\*)|^__([^\s][\s\S]*?[^\s])__(?!_)|^\*\*([^\s][\s\S]*?[^\s])\*\*(?!\*)/,
1103
- em: /^_([^\s_])_(?!_)|^_([^\s_<][\s\S]*?[^\s_])_(?!_|[^\s,punctuation])|^_([^\s_<][\s\S]*?[^\s])_(?!_|[^\s,punctuation])|^\*([^\s*<\[])\*(?!\*)|^\*([^\s<"][\s\S]*?[^\s\[\*])\*(?![\]`punctuation])|^\*([^\s*"<\[][\s\S]*[^\s])\*(?!\*)/,
1128
+ reflinkSearch: 'reflink|nolink(?!\\()',
1129
+ strong: {
1130
+ start: /^(?:(\*\*(?=[*punctuation]))|\*\*)(?![\s])|__/, // (1) returns if starts w/ punctuation
1131
+ middle: /^\*\*(?:(?:(?!overlapSkip)(?:[^*]|\\\*)|overlapSkip)|\*(?:(?!overlapSkip)(?:[^*]|\\\*)|overlapSkip)*?\*)+?\*\*$|^__(?![\s])((?:(?:(?!overlapSkip)(?:[^_]|\\_)|overlapSkip)|_(?:(?!overlapSkip)(?:[^_]|\\_)|overlapSkip)*?_)+?)__$/,
1132
+ endAst: /[^punctuation\s]\*\*(?!\*)|[punctuation]\*\*(?!\*)(?:(?=[punctuation\s]|$))/, // last char can't be punct, or final * must also be followed by punct (or endline)
1133
+ endUnd: /[^\s]__(?!_)(?:(?=[punctuation\s])|$)/ // last char can't be a space, and final _ must preceed punct or \s (or endline)
1134
+ },
1135
+ em: {
1136
+ start: /^(?:(\*(?=[punctuation]))|\*)(?![*\s])|_/, // (1) returns if starts w/ punctuation
1137
+ middle: /^\*(?:(?:(?!overlapSkip)(?:[^*]|\\\*)|overlapSkip)|\*(?:(?!overlapSkip)(?:[^*]|\\\*)|overlapSkip)*?\*)+?\*$|^_(?![_\s])(?:(?:(?!overlapSkip)(?:[^_]|\\_)|overlapSkip)|_(?:(?!overlapSkip)(?:[^_]|\\_)|overlapSkip)*?_)+?_$/,
1138
+ endAst: /[^punctuation\s]\*(?!\*)|[punctuation]\*(?!\*)(?:(?=[punctuation\s]|$))/, // last char can't be punct, or final * must also be followed by punct (or endline)
1139
+ endUnd: /[^\s]_(?!_)(?:(?=[punctuation\s])|$)/ // last char can't be a space, and final _ must preceed punct or \s (or endline)
1140
+ },
1104
1141
  code: /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,
1105
1142
  br: /^( {2,}|\\)\n(?!\s*$)/,
1106
1143
  del: noopTest$1,
1107
- text: /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*]|\b_|$)|[^ ](?= {2,}\n))|(?= {2,}\n))/
1144
+ text: /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*]|\b_|$)|[^ ](?= {2,}\n))|(?= {2,}\n))/,
1145
+ punctuation: /^([\s*punctuation])/
1108
1146
  };
1109
1147
 
1110
1148
  // list of punctuation marks from common mark spec
1111
- // without ` and ] to workaround Rule 17 (inline code blocks/links)
1112
- // without , to work around example 393
1113
- inline._punctuation = '!"#$%&\'()*+\\-./:;<=>?@\\[^_{|}~';
1114
- inline.em = edit$1(inline.em).replace(/punctuation/g, inline._punctuation).getRegex();
1149
+ // without * and _ to workaround cases with double emphasis
1150
+ inline._punctuation = '!"#$%&\'()+\\-.,/:;<=>?@\\[\\]`^{|}~';
1151
+ inline.punctuation = edit$1(inline.punctuation).replace(/punctuation/g, inline._punctuation).getRegex();
1152
+
1153
+ // sequences em should skip over [title](link), `code`, <html>
1154
+ inline._blockSkip = '\\[[^\\]]*?\\]\\([^\\)]*?\\)|`[^`]*?`|<[^>]*?>';
1155
+ inline._overlapSkip = '__[^_]*?__|\\*\\*\\[^\\*\\]*?\\*\\*';
1156
+
1157
+ inline.em.start = edit$1(inline.em.start)
1158
+ .replace(/punctuation/g, inline._punctuation)
1159
+ .getRegex();
1160
+
1161
+ inline.em.middle = edit$1(inline.em.middle)
1162
+ .replace(/punctuation/g, inline._punctuation)
1163
+ .replace(/overlapSkip/g, inline._overlapSkip)
1164
+ .getRegex();
1165
+
1166
+ inline.em.endAst = edit$1(inline.em.endAst, 'g')
1167
+ .replace(/punctuation/g, inline._punctuation)
1168
+ .getRegex();
1169
+
1170
+ inline.em.endUnd = edit$1(inline.em.endUnd, 'g')
1171
+ .replace(/punctuation/g, inline._punctuation)
1172
+ .getRegex();
1173
+
1174
+ inline.strong.start = edit$1(inline.strong.start)
1175
+ .replace(/punctuation/g, inline._punctuation)
1176
+ .getRegex();
1177
+
1178
+ inline.strong.middle = edit$1(inline.strong.middle)
1179
+ .replace(/punctuation/g, inline._punctuation)
1180
+ .replace(/blockSkip/g, inline._blockSkip)
1181
+ .getRegex();
1182
+
1183
+ inline.strong.endAst = edit$1(inline.strong.endAst, 'g')
1184
+ .replace(/punctuation/g, inline._punctuation)
1185
+ .getRegex();
1186
+
1187
+ inline.strong.endUnd = edit$1(inline.strong.endUnd, 'g')
1188
+ .replace(/punctuation/g, inline._punctuation)
1189
+ .getRegex();
1190
+
1191
+ inline.blockSkip = edit$1(inline._blockSkip, 'g')
1192
+ .getRegex();
1193
+
1194
+ inline.overlapSkip = edit$1(inline._overlapSkip, 'g')
1195
+ .getRegex();
1115
1196
 
1116
1197
  inline._escapes = /\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/g;
1117
1198
 
@@ -1129,7 +1210,7 @@ inline.tag = edit$1(inline.tag)
1129
1210
  .replace('attribute', inline._attribute)
1130
1211
  .getRegex();
1131
1212
 
1132
- inline._label = /(?:\[[^\[\]]*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
1213
+ inline._label = /(?:\[(?:\\.|[^\[\]\\])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
1133
1214
  inline._href = /<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*/;
1134
1215
  inline._title = /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/;
1135
1216
 
@@ -1143,6 +1224,11 @@ inline.reflink = edit$1(inline.reflink)
1143
1224
  .replace('label', inline._label)
1144
1225
  .getRegex();
1145
1226
 
1227
+ inline.reflinkSearch = edit$1(inline.reflinkSearch, 'g')
1228
+ .replace('reflink', inline.reflink)
1229
+ .replace('nolink', inline.nolink)
1230
+ .getRegex();
1231
+
1146
1232
  /**
1147
1233
  * Normal Inline Grammar
1148
1234
  */
@@ -1154,8 +1240,18 @@ inline.normal = merge$1({}, inline);
1154
1240
  */
1155
1241
 
1156
1242
  inline.pedantic = merge$1({}, inline.normal, {
1157
- strong: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
1158
- em: /^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/,
1243
+ strong: {
1244
+ start: /^__|\*\*/,
1245
+ middle: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
1246
+ endAst: /\*\*(?!\*)/g,
1247
+ endUnd: /__(?!_)/g
1248
+ },
1249
+ em: {
1250
+ start: /^_|\*/,
1251
+ middle: /^()\*(?=\S)([\s\S]*?\S)\*(?!\*)|^_(?=\S)([\s\S]*?\S)_(?!_)/,
1252
+ endAst: /\*(?!\*)/g,
1253
+ endUnd: /_(?!_)/g
1254
+ },
1159
1255
  link: edit$1(/^!?\[(label)\]\((.*?)\)/)
1160
1256
  .replace('label', inline._label)
1161
1257
  .getRegex(),
@@ -1514,9 +1610,29 @@ var Lexer_1 = class Lexer {
1514
1610
  /**
1515
1611
  * Lexing/Compiling
1516
1612
  */
1517
- inlineTokens(src, tokens = [], inLink = false, inRawBlock = false) {
1613
+ inlineTokens(src, tokens = [], inLink = false, inRawBlock = false, prevChar = '') {
1518
1614
  let token;
1519
1615
 
1616
+ // String with links masked to avoid interference with em and strong
1617
+ let maskedSrc = src;
1618
+ let match;
1619
+
1620
+ // Mask out reflinks
1621
+ if (this.tokens.links) {
1622
+ const links = Object.keys(this.tokens.links);
1623
+ if (links.length > 0) {
1624
+ while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
1625
+ if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
1626
+ maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
1627
+ }
1628
+ }
1629
+ }
1630
+ }
1631
+ // Mask out other blocks
1632
+ while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
1633
+ maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
1634
+ }
1635
+
1520
1636
  while (src) {
1521
1637
  // escape
1522
1638
  if (token = this.tokenizer.escape(src)) {
@@ -1555,7 +1671,7 @@ var Lexer_1 = class Lexer {
1555
1671
  }
1556
1672
 
1557
1673
  // strong
1558
- if (token = this.tokenizer.strong(src)) {
1674
+ if (token = this.tokenizer.strong(src, maskedSrc, prevChar)) {
1559
1675
  src = src.substring(token.raw.length);
1560
1676
  token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
1561
1677
  tokens.push(token);
@@ -1563,7 +1679,7 @@ var Lexer_1 = class Lexer {
1563
1679
  }
1564
1680
 
1565
1681
  // em
1566
- if (token = this.tokenizer.em(src)) {
1682
+ if (token = this.tokenizer.em(src, maskedSrc, prevChar)) {
1567
1683
  src = src.substring(token.raw.length);
1568
1684
  token.tokens = this.inlineTokens(token.text, [], inLink, inRawBlock);
1569
1685
  tokens.push(token);
@@ -1609,6 +1725,7 @@ var Lexer_1 = class Lexer {
1609
1725
  // text
1610
1726
  if (token = this.tokenizer.inlineText(src, inRawBlock, smartypants)) {
1611
1727
  src = src.substring(token.raw.length);
1728
+ prevChar = token.raw.slice(-1);
1612
1729
  tokens.push(token);
1613
1730
  continue;
1614
1731
  }
@@ -2195,20 +2312,22 @@ function marked(src, opt, callback) {
2195
2312
  marked.walkTokens(tokens, function(token) {
2196
2313
  if (token.type === 'code') {
2197
2314
  pending++;
2198
- highlight(token.text, token.lang, function(err, code) {
2199
- if (err) {
2200
- return done(err);
2201
- }
2202
- if (code != null && code !== token.text) {
2203
- token.text = code;
2204
- token.escaped = true;
2205
- }
2315
+ setTimeout(() => {
2316
+ highlight(token.text, token.lang, function(err, code) {
2317
+ if (err) {
2318
+ return done(err);
2319
+ }
2320
+ if (code != null && code !== token.text) {
2321
+ token.text = code;
2322
+ token.escaped = true;
2323
+ }
2206
2324
 
2207
- pending--;
2208
- if (pending === 0) {
2209
- done();
2210
- }
2211
- });
2325
+ pending--;
2326
+ if (pending === 0) {
2327
+ done();
2328
+ }
2329
+ });
2330
+ }, 0);
2212
2331
  }
2213
2332
  });
2214
2333