marked 4.0.19 → 4.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/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
 
@@ -355,10 +353,10 @@ export class Tokenizer {
355
353
  text: cap[0]
356
354
  };
357
355
  if (this.options.sanitize) {
356
+ const text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
358
357
  token.type = 'paragraph';
359
- token.text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
360
- token.tokens = [];
361
- this.lexer.inline(token.text, token.tokens);
358
+ token.text = text;
359
+ token.tokens = this.lexer.inline(text);
362
360
  }
363
361
  return token;
364
362
  }
@@ -416,8 +414,7 @@ export class Tokenizer {
416
414
  // header child tokens
417
415
  l = item.header.length;
418
416
  for (j = 0; j < l; j++) {
419
- item.header[j].tokens = [];
420
- this.lexer.inline(item.header[j].text, item.header[j].tokens);
417
+ item.header[j].tokens = this.lexer.inline(item.header[j].text);
421
418
  }
422
419
 
423
420
  // cell child tokens
@@ -425,8 +422,7 @@ export class Tokenizer {
425
422
  for (j = 0; j < l; j++) {
426
423
  row = item.rows[j];
427
424
  for (k = 0; k < row.length; k++) {
428
- row[k].tokens = [];
429
- this.lexer.inline(row[k].text, row[k].tokens);
425
+ row[k].tokens = this.lexer.inline(row[k].text);
430
426
  }
431
427
  }
432
428
 
@@ -438,45 +434,40 @@ export class Tokenizer {
438
434
  lheading(src) {
439
435
  const cap = this.rules.block.lheading.exec(src);
440
436
  if (cap) {
441
- const token = {
437
+ return {
442
438
  type: 'heading',
443
439
  raw: cap[0],
444
440
  depth: cap[2].charAt(0) === '=' ? 1 : 2,
445
441
  text: cap[1],
446
- tokens: []
442
+ tokens: this.lexer.inline(cap[1])
447
443
  };
448
- this.lexer.inline(token.text, token.tokens);
449
- return token;
450
444
  }
451
445
  }
452
446
 
453
447
  paragraph(src) {
454
448
  const cap = this.rules.block.paragraph.exec(src);
455
449
  if (cap) {
456
- const token = {
450
+ const text = cap[1].charAt(cap[1].length - 1) === '\n'
451
+ ? cap[1].slice(0, -1)
452
+ : cap[1];
453
+ return {
457
454
  type: 'paragraph',
458
455
  raw: cap[0],
459
- text: cap[1].charAt(cap[1].length - 1) === '\n'
460
- ? cap[1].slice(0, -1)
461
- : cap[1],
462
- tokens: []
456
+ text,
457
+ tokens: this.lexer.inline(text)
463
458
  };
464
- this.lexer.inline(token.text, token.tokens);
465
- return token;
466
459
  }
467
460
  }
468
461
 
469
462
  text(src) {
470
463
  const cap = this.rules.block.text.exec(src);
471
464
  if (cap) {
472
- const token = {
465
+ return {
473
466
  type: 'text',
474
467
  raw: cap[0],
475
468
  text: cap[0],
476
- tokens: []
469
+ tokens: this.lexer.inline(cap[0])
477
470
  };
478
- this.lexer.inline(token.text, token.tokens);
479
- return token;
480
471
  }
481
472
  }
482
473
 
@@ -645,7 +636,7 @@ export class Tokenizer {
645
636
  type: 'em',
646
637
  raw: src.slice(0, lLength + match.index + rLength + 1),
647
638
  text,
648
- tokens: this.lexer.inlineTokens(text, [])
639
+ tokens: this.lexer.inlineTokens(text)
649
640
  };
650
641
  }
651
642
 
@@ -655,7 +646,7 @@ export class Tokenizer {
655
646
  type: 'strong',
656
647
  raw: src.slice(0, lLength + match.index + rLength + 1),
657
648
  text,
658
- tokens: this.lexer.inlineTokens(text, [])
649
+ tokens: this.lexer.inlineTokens(text)
659
650
  };
660
651
  }
661
652
  }
@@ -696,7 +687,7 @@ export class Tokenizer {
696
687
  type: 'del',
697
688
  raw: cap[0],
698
689
  text: cap[2],
699
- tokens: this.lexer.inlineTokens(cap[2], [])
690
+ tokens: this.lexer.inlineTokens(cap[2])
700
691
  };
701
692
  }
702
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,
@@ -14,7 +15,6 @@ export function getDefaults() {
14
15
  sanitize: false,
15
16
  sanitizer: null,
16
17
  silent: false,
17
- smartLists: false,
18
18
  smartypants: false,
19
19
  tokenizer: null,
20
20
  walkTokens: 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
  /**