marked 4.0.17 → 4.1.0

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/src/Tokenizer.js CHANGED
@@ -19,7 +19,7 @@ function outputLink(cap, link, raw, lexer) {
19
19
  href,
20
20
  title,
21
21
  text,
22
- tokens: lexer.inlineTokens(text, [])
22
+ tokens: lexer.inlineTokens(text)
23
23
  };
24
24
  lexer.state.inLink = false;
25
25
  return token;
@@ -125,15 +125,13 @@ export class Tokenizer {
125
125
  }
126
126
  }
127
127
 
128
- const token = {
128
+ return {
129
129
  type: 'heading',
130
130
  raw: cap[0],
131
131
  depth: cap[1].length,
132
132
  text,
133
- tokens: []
133
+ tokens: this.lexer.inline(text)
134
134
  };
135
- this.lexer.inline(token.text, token.tokens);
136
- return token;
137
135
  }
138
136
  }
139
137
 
@@ -226,7 +224,8 @@ export class Tokenizer {
226
224
  if (!endEarly) {
227
225
  const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])((?: [^\\n]*)?(?:\\n|$))`);
228
226
  const hrRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`);
229
- const fencesBeginRegex = new RegExp(`^( {0,${Math.min(3, indent - 1)}})(\`\`\`|~~~)`);
227
+ const fencesBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:\`\`\`|~~~)`);
228
+ const headingBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}#`);
230
229
 
231
230
  // Check if following lines should be included in List Item
232
231
  while (src) {
@@ -244,7 +243,7 @@ export class Tokenizer {
244
243
  }
245
244
 
246
245
  // End list item if found start of new heading
247
- if (this.rules.block.heading.test(line)) {
246
+ if (headingBeginRegex.test(line)) {
248
247
  break;
249
248
  }
250
249
 
@@ -354,10 +353,10 @@ export class Tokenizer {
354
353
  text: cap[0]
355
354
  };
356
355
  if (this.options.sanitize) {
356
+ const text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
357
357
  token.type = 'paragraph';
358
- token.text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
359
- token.tokens = [];
360
- this.lexer.inline(token.text, token.tokens);
358
+ token.text = text;
359
+ token.tokens = this.lexer.inline(text);
361
360
  }
362
361
  return token;
363
362
  }
@@ -415,8 +414,7 @@ export class Tokenizer {
415
414
  // header child tokens
416
415
  l = item.header.length;
417
416
  for (j = 0; j < l; j++) {
418
- item.header[j].tokens = [];
419
- this.lexer.inline(item.header[j].text, item.header[j].tokens);
417
+ item.header[j].tokens = this.lexer.inline(item.header[j].text);
420
418
  }
421
419
 
422
420
  // cell child tokens
@@ -424,8 +422,7 @@ export class Tokenizer {
424
422
  for (j = 0; j < l; j++) {
425
423
  row = item.rows[j];
426
424
  for (k = 0; k < row.length; k++) {
427
- row[k].tokens = [];
428
- this.lexer.inline(row[k].text, row[k].tokens);
425
+ row[k].tokens = this.lexer.inline(row[k].text);
429
426
  }
430
427
  }
431
428
 
@@ -437,45 +434,40 @@ export class Tokenizer {
437
434
  lheading(src) {
438
435
  const cap = this.rules.block.lheading.exec(src);
439
436
  if (cap) {
440
- const token = {
437
+ return {
441
438
  type: 'heading',
442
439
  raw: cap[0],
443
440
  depth: cap[2].charAt(0) === '=' ? 1 : 2,
444
441
  text: cap[1],
445
- tokens: []
442
+ tokens: this.lexer.inline(cap[1])
446
443
  };
447
- this.lexer.inline(token.text, token.tokens);
448
- return token;
449
444
  }
450
445
  }
451
446
 
452
447
  paragraph(src) {
453
448
  const cap = this.rules.block.paragraph.exec(src);
454
449
  if (cap) {
455
- const token = {
450
+ const text = cap[1].charAt(cap[1].length - 1) === '\n'
451
+ ? cap[1].slice(0, -1)
452
+ : cap[1];
453
+ return {
456
454
  type: 'paragraph',
457
455
  raw: cap[0],
458
- text: cap[1].charAt(cap[1].length - 1) === '\n'
459
- ? cap[1].slice(0, -1)
460
- : cap[1],
461
- tokens: []
456
+ text,
457
+ tokens: this.lexer.inline(text)
462
458
  };
463
- this.lexer.inline(token.text, token.tokens);
464
- return token;
465
459
  }
466
460
  }
467
461
 
468
462
  text(src) {
469
463
  const cap = this.rules.block.text.exec(src);
470
464
  if (cap) {
471
- const token = {
465
+ return {
472
466
  type: 'text',
473
467
  raw: cap[0],
474
468
  text: cap[0],
475
- tokens: []
469
+ tokens: this.lexer.inline(cap[0])
476
470
  };
477
- this.lexer.inline(token.text, token.tokens);
478
- return token;
479
471
  }
480
472
  }
481
473
 
@@ -644,7 +636,7 @@ export class Tokenizer {
644
636
  type: 'em',
645
637
  raw: src.slice(0, lLength + match.index + rLength + 1),
646
638
  text,
647
- tokens: this.lexer.inlineTokens(text, [])
639
+ tokens: this.lexer.inlineTokens(text)
648
640
  };
649
641
  }
650
642
 
@@ -654,7 +646,7 @@ export class Tokenizer {
654
646
  type: 'strong',
655
647
  raw: src.slice(0, lLength + match.index + rLength + 1),
656
648
  text,
657
- tokens: this.lexer.inlineTokens(text, [])
649
+ tokens: this.lexer.inlineTokens(text)
658
650
  };
659
651
  }
660
652
  }
@@ -695,7 +687,7 @@ export class Tokenizer {
695
687
  type: 'del',
696
688
  raw: cap[0],
697
689
  text: cap[2],
698
- tokens: this.lexer.inlineTokens(cap[2], [])
690
+ tokens: this.lexer.inlineTokens(cap[2])
699
691
  };
700
692
  }
701
693
  }
package/src/defaults.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export function getDefaults() {
2
2
  return {
3
+ async: false,
3
4
  baseUrl: null,
4
5
  breaks: false,
5
6
  extensions: null,
package/src/marked.js CHANGED
@@ -105,13 +105,7 @@ export function marked(src, opt, callback) {
105
105
  return;
106
106
  }
107
107
 
108
- try {
109
- const tokens = Lexer.lex(src, opt);
110
- if (opt.walkTokens) {
111
- marked.walkTokens(tokens, opt.walkTokens);
112
- }
113
- return Parser.parse(tokens, opt);
114
- } catch (e) {
108
+ function onError(e) {
115
109
  e.message += '\nPlease report this to https://github.com/markedjs/marked.';
116
110
  if (opt.silent) {
117
111
  return '<p>An error occurred:</p><pre>'
@@ -120,6 +114,23 @@ export function marked(src, opt, callback) {
120
114
  }
121
115
  throw e;
122
116
  }
117
+
118
+ try {
119
+ const tokens = Lexer.lex(src, opt);
120
+ if (opt.walkTokens) {
121
+ if (opt.async) {
122
+ return Promise.all(marked.walkTokens(tokens, opt.walkTokens))
123
+ .then(() => {
124
+ return Parser.parse(tokens, opt);
125
+ })
126
+ .catch(onError);
127
+ }
128
+ marked.walkTokens(tokens, opt.walkTokens);
129
+ }
130
+ return Parser.parse(tokens, opt);
131
+ } catch (e) {
132
+ onError(e);
133
+ }
123
134
  }
124
135
 
125
136
  /**
@@ -236,10 +247,12 @@ marked.use = function(...args) {
236
247
  if (pack.walkTokens) {
237
248
  const walkTokens = marked.defaults.walkTokens;
238
249
  opts.walkTokens = function(token) {
239
- pack.walkTokens.call(this, token);
250
+ let values = [];
251
+ values.push(pack.walkTokens.call(this, token));
240
252
  if (walkTokens) {
241
- walkTokens.call(this, token);
253
+ values = values.concat(walkTokens.call(this, token));
242
254
  }
255
+ return values;
243
256
  };
244
257
  }
245
258
 
@@ -256,35 +269,37 @@ marked.use = function(...args) {
256
269
  */
257
270
 
258
271
  marked.walkTokens = function(tokens, callback) {
272
+ let values = [];
259
273
  for (const token of tokens) {
260
- callback.call(marked, token);
274
+ values = values.concat(callback.call(marked, token));
261
275
  switch (token.type) {
262
276
  case 'table': {
263
277
  for (const cell of token.header) {
264
- marked.walkTokens(cell.tokens, callback);
278
+ values = values.concat(marked.walkTokens(cell.tokens, callback));
265
279
  }
266
280
  for (const row of token.rows) {
267
281
  for (const cell of row) {
268
- marked.walkTokens(cell.tokens, callback);
282
+ values = values.concat(marked.walkTokens(cell.tokens, callback));
269
283
  }
270
284
  }
271
285
  break;
272
286
  }
273
287
  case 'list': {
274
- marked.walkTokens(token.items, callback);
288
+ values = values.concat(marked.walkTokens(token.items, callback));
275
289
  break;
276
290
  }
277
291
  default: {
278
292
  if (marked.defaults.extensions && marked.defaults.extensions.childTokens && marked.defaults.extensions.childTokens[token.type]) { // Walk any extensions
279
293
  marked.defaults.extensions.childTokens[token.type].forEach(function(childTokens) {
280
- marked.walkTokens(token[childTokens], callback);
294
+ values = values.concat(marked.walkTokens(token[childTokens], callback));
281
295
  });
282
296
  } else if (token.tokens) {
283
- marked.walkTokens(token.tokens, callback);
297
+ values = values.concat(marked.walkTokens(token.tokens, callback));
284
298
  }
285
299
  }
286
300
  }
287
301
  }
302
+ return values;
288
303
  };
289
304
 
290
305
  /**