@subiz/widget-v4 4.0.53 → 4.0.55

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.
Files changed (86) hide show
  1. package/compiled_src/notif/notif.js +6 -2
  2. package/compiled_src/util/campaign.js +6 -1
  3. package/dist/17d7ef81b7a.178.js +1 -0
  4. package/dist/17d7ef81b7a.213.js +1 -0
  5. package/dist/17d7ef81b7a.362.js +1 -0
  6. package/dist/17d7ef81b7a.441.js +1 -0
  7. package/dist/17d7ef81b7a.605.js +1 -0
  8. package/dist/17d7ef81b7a.619.js +1 -0
  9. package/dist/17d7ef81b7a.678.js +1 -0
  10. package/dist/17d7ef81b7a.74.js +1 -0
  11. package/dist/17d7ef81b7a.782.js +1 -0
  12. package/dist/17d7ef81b7a.app.js +1 -0
  13. package/dist/17d7ef81b7a.index.html +21 -0
  14. package/dist/17d7ef81b7a.library.html +21 -0
  15. package/dist/17d7ef81b7a.library.js +1 -0
  16. package/dist/17d7ef81b7a.preview.html +759 -0
  17. package/dist/17d7ef81b7a.preview.js +1 -0
  18. package/dist/17d7ef81b7a.screenshoot.js +1 -0
  19. package/dist/17d7ef84382.178.js +406 -0
  20. package/dist/17d7ef84382.213.js +3223 -0
  21. package/dist/17d7ef84382.362.js +107 -0
  22. package/dist/17d7ef84382.441.js +2706 -0
  23. package/dist/17d7ef84382.605.js +391 -0
  24. package/dist/17d7ef84382.619.js +777 -0
  25. package/dist/17d7ef84382.678.js +107 -0
  26. package/dist/17d7ef84382.74.js +4732 -0
  27. package/dist/17d7ef84382.782.js +107 -0
  28. package/dist/17d7ef84382.screenshoot.js +5719 -0
  29. package/dist/HASH +1 -1
  30. package/dist/assets/img/Facebook.64607ec.png +0 -0
  31. package/dist/assets/img/Zalo.6845707.png +0 -0
  32. package/dist/assets/img/award1a.1a58e10.png +0 -0
  33. package/dist/assets/img/award3a.3b74423.png +0 -0
  34. package/dist/assets/img/award4a.ed6ccd4.png +0 -0
  35. package/dist/assets/img/award5.39eb7ae.png +0 -0
  36. package/dist/assets/img/bubble_default.7d5e4ab.svg +20 -0
  37. package/dist/assets/img/convos_empty.2353007.svg +72 -0
  38. package/dist/assets/img/default.06e6ee0.jpg +0 -0
  39. package/dist/assets/img/default_avatar.5b74dc1.png +0 -0
  40. package/dist/assets/img/done_gray.d7e8203.svg +4 -0
  41. package/dist/assets/img/emoji/messenger/sub_23.529_58.824.d5faf14.png +0 -0
  42. package/dist/assets/img/emoji/messenger/sub_23.529_60.784.1420241.png +0 -0
  43. package/dist/assets/img/emoji/messenger/sub_25.490_0.000.1e6b2c0.png +0 -0
  44. package/dist/assets/img/emoji/messenger/sub_25.490_45.098.3dfbc08.png +0 -0
  45. package/dist/assets/img/emoji/messenger/sub_25.490_5.882.4b49e94.png +0 -0
  46. package/dist/assets/img/emoji/messenger/sub_27.451_72.549.7114f58.png +0 -0
  47. package/dist/assets/img/emoji/messenger/sub_27.451_84.314.231e2b7.png +0 -0
  48. package/dist/assets/img/emoji/messenger/sub_27.451_96.078.ddbb008.png +0 -0
  49. package/dist/assets/img/emoji/messenger/sub_29.412_5.882.61f3359.png +0 -0
  50. package/dist/assets/img/emoji/messenger/sub_58.824_100.000.6df72bc.png +0 -0
  51. package/dist/assets/img/emoji/messenger/sub_58.824_45.098.3d7a7ad.png +0 -0
  52. package/dist/assets/img/emoji/messenger/sub_58.824_47.059.0f4d6e2.png +0 -0
  53. package/dist/assets/img/emoji/messenger/sub_58.824_54.902.65724c1.png +0 -0
  54. package/dist/assets/img/emoji/messenger/sub_58.824_64.706.9da3cba.png +0 -0
  55. package/dist/assets/img/emoji/messenger/sub_58.824_72.549.edca5cf.png +0 -0
  56. package/dist/assets/img/emoji/messenger/sub_58.824_78.431.f910f2a.png +0 -0
  57. package/dist/assets/img/emoji/messenger/sub_58.824_88.235.def4e71.png +0 -0
  58. package/dist/assets/img/emoji/messenger/sub_60.784_11.765.49c03d0.png +0 -0
  59. package/dist/assets/img/emoji/messenger/sub_60.784_27.451.46b54e5.png +0 -0
  60. package/dist/assets/img/emoji/messenger/sub_60.784_29.412.e36d284.png +0 -0
  61. package/dist/assets/img/emoji/messenger/sub_60.784_3.922.bb191b0.png +0 -0
  62. package/dist/assets/img/emoji/messenger/sub_60.784_35.294.af7b927.png +0 -0
  63. package/dist/assets/img/emoji/messenger/sub_60.784_7.843.98f2a99.png +0 -0
  64. package/dist/assets/img/empty.29bed2d.svg +23 -0
  65. package/dist/assets/img/examplte_logo.f86535f.svg +1 -0
  66. package/dist/assets/img/external-link.90577cc.svg +1 -0
  67. package/dist/assets/img/fb_inverse_circle.36c2cfc.png +0 -0
  68. package/dist/assets/img/image_default.be0698b.png +0 -0
  69. package/dist/assets/img/loading_static.8f403ae.svg +10 -0
  70. package/dist/assets/img/main-service1.d536cc2.png +0 -0
  71. package/dist/assets/img/main-service2.9b99549.png +0 -0
  72. package/dist/assets/img/rating--average.1ea492c.svg +12 -0
  73. package/dist/assets/img/rating--great.e3d712e.svg +16 -0
  74. package/dist/assets/img/rating--poor.2938a25.svg +12 -0
  75. package/dist/assets/img/send.6db52a9.svg +3 -0
  76. package/dist/assets/img/subiz-mono.877e9f5.svg +3 -0
  77. package/dist/screenshoot.html +7 -0
  78. package/package.json +8 -7
  79. package/screenshoot.html +15 -0
  80. package/screenshoot.js +28 -0
  81. package/src/notif/notif.js +36 -27
  82. package/src/util/campaign.js +14 -11
  83. package/take.js +36 -0
  84. package/webpack.dev.js +7 -0
  85. package/webpack.prod.js +12 -2
  86. package/webpack.screenshoot.js +28 -0
@@ -0,0 +1,2706 @@
1
+ "use strict";
2
+ (self["webpackChunk_subiz_widget_v4"] = self["webpackChunk_subiz_widget_v4"] || []).push([[441],{
3
+
4
+ /***/ 7441:
5
+ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => {
6
+
7
+ __webpack_require__.r(__webpack_exports__);
8
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
9
+ /* harmony export */ "Lexer": () => (/* binding */ Lexer),
10
+ /* harmony export */ "Parser": () => (/* binding */ Parser),
11
+ /* harmony export */ "Renderer": () => (/* binding */ Renderer),
12
+ /* harmony export */ "Slugger": () => (/* binding */ Slugger),
13
+ /* harmony export */ "TextRenderer": () => (/* binding */ TextRenderer),
14
+ /* harmony export */ "Tokenizer": () => (/* binding */ Tokenizer),
15
+ /* harmony export */ "defaults": () => (/* binding */ defaults),
16
+ /* harmony export */ "getDefaults": () => (/* binding */ getDefaults),
17
+ /* harmony export */ "lexer": () => (/* binding */ lexer),
18
+ /* harmony export */ "marked": () => (/* binding */ marked),
19
+ /* harmony export */ "options": () => (/* binding */ options),
20
+ /* harmony export */ "parse": () => (/* binding */ parse),
21
+ /* harmony export */ "parseInline": () => (/* binding */ parseInline),
22
+ /* harmony export */ "parser": () => (/* binding */ parser),
23
+ /* harmony export */ "setOptions": () => (/* binding */ setOptions),
24
+ /* harmony export */ "use": () => (/* binding */ use),
25
+ /* harmony export */ "walkTokens": () => (/* binding */ walkTokens)
26
+ /* harmony export */ });
27
+ /**
28
+ * marked - a markdown parser
29
+ * Copyright (c) 2011-2021, Christopher Jeffrey. (MIT Licensed)
30
+ * https://github.com/markedjs/marked
31
+ */
32
+
33
+ /**
34
+ * DO NOT EDIT THIS FILE
35
+ * The code in this file is generated from files in ./src/
36
+ */
37
+
38
+ function getDefaults() {
39
+ return {
40
+ baseUrl: null,
41
+ breaks: false,
42
+ extensions: null,
43
+ gfm: true,
44
+ headerIds: true,
45
+ headerPrefix: '',
46
+ highlight: null,
47
+ langPrefix: 'language-',
48
+ mangle: true,
49
+ pedantic: false,
50
+ renderer: null,
51
+ sanitize: false,
52
+ sanitizer: null,
53
+ silent: false,
54
+ smartLists: false,
55
+ smartypants: false,
56
+ tokenizer: null,
57
+ walkTokens: null,
58
+ xhtml: false
59
+ };
60
+ }
61
+
62
+ let defaults = getDefaults();
63
+
64
+ function changeDefaults(newDefaults) {
65
+ defaults = newDefaults;
66
+ }
67
+
68
+ /**
69
+ * Helpers
70
+ */
71
+ const escapeTest = /[&<>"']/;
72
+ const escapeReplace = /[&<>"']/g;
73
+ const escapeTestNoEncode = /[<>"']|&(?!#?\w+;)/;
74
+ const escapeReplaceNoEncode = /[<>"']|&(?!#?\w+;)/g;
75
+ const escapeReplacements = {
76
+ '&': '&amp;',
77
+ '<': '&lt;',
78
+ '>': '&gt;',
79
+ '"': '&quot;',
80
+ "'": '&#39;'
81
+ };
82
+ const getEscapeReplacement = (ch) => escapeReplacements[ch];
83
+ function escape(html, encode) {
84
+ if (encode) {
85
+ if (escapeTest.test(html)) {
86
+ return html.replace(escapeReplace, getEscapeReplacement);
87
+ }
88
+ } else {
89
+ if (escapeTestNoEncode.test(html)) {
90
+ return html.replace(escapeReplaceNoEncode, getEscapeReplacement);
91
+ }
92
+ }
93
+
94
+ return html;
95
+ }
96
+
97
+ const unescapeTest = /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig;
98
+
99
+ function unescape(html) {
100
+ // explicitly match decimal, hex, and named HTML entities
101
+ return html.replace(unescapeTest, (_, n) => {
102
+ n = n.toLowerCase();
103
+ if (n === 'colon') return ':';
104
+ if (n.charAt(0) === '#') {
105
+ return n.charAt(1) === 'x'
106
+ ? String.fromCharCode(parseInt(n.substring(2), 16))
107
+ : String.fromCharCode(+n.substring(1));
108
+ }
109
+ return '';
110
+ });
111
+ }
112
+
113
+ const caret = /(^|[^\[])\^/g;
114
+ function edit(regex, opt) {
115
+ regex = regex.source || regex;
116
+ opt = opt || '';
117
+ const obj = {
118
+ replace: (name, val) => {
119
+ val = val.source || val;
120
+ val = val.replace(caret, '$1');
121
+ regex = regex.replace(name, val);
122
+ return obj;
123
+ },
124
+ getRegex: () => {
125
+ return new RegExp(regex, opt);
126
+ }
127
+ };
128
+ return obj;
129
+ }
130
+
131
+ const nonWordAndColonTest = /[^\w:]/g;
132
+ const originIndependentUrl = /^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;
133
+ function cleanUrl(sanitize, base, href) {
134
+ if (sanitize) {
135
+ let prot;
136
+ try {
137
+ prot = decodeURIComponent(unescape(href))
138
+ .replace(nonWordAndColonTest, '')
139
+ .toLowerCase();
140
+ } catch (e) {
141
+ return null;
142
+ }
143
+ if (prot.indexOf('javascript:') === 0 || prot.indexOf('vbscript:') === 0 || prot.indexOf('data:') === 0) {
144
+ return null;
145
+ }
146
+ }
147
+ if (base && !originIndependentUrl.test(href)) {
148
+ href = resolveUrl(base, href);
149
+ }
150
+ try {
151
+ href = encodeURI(href).replace(/%25/g, '%');
152
+ } catch (e) {
153
+ return null;
154
+ }
155
+ return href;
156
+ }
157
+
158
+ const baseUrls = {};
159
+ const justDomain = /^[^:]+:\/*[^/]*$/;
160
+ const protocol = /^([^:]+:)[\s\S]*$/;
161
+ const domain = /^([^:]+:\/*[^/]*)[\s\S]*$/;
162
+
163
+ function resolveUrl(base, href) {
164
+ if (!baseUrls[' ' + base]) {
165
+ // we can ignore everything in base after the last slash of its path component,
166
+ // but we might need to add _that_
167
+ // https://tools.ietf.org/html/rfc3986#section-3
168
+ if (justDomain.test(base)) {
169
+ baseUrls[' ' + base] = base + '/';
170
+ } else {
171
+ baseUrls[' ' + base] = rtrim(base, '/', true);
172
+ }
173
+ }
174
+ base = baseUrls[' ' + base];
175
+ const relativeBase = base.indexOf(':') === -1;
176
+
177
+ if (href.substring(0, 2) === '//') {
178
+ if (relativeBase) {
179
+ return href;
180
+ }
181
+ return base.replace(protocol, '$1') + href;
182
+ } else if (href.charAt(0) === '/') {
183
+ if (relativeBase) {
184
+ return href;
185
+ }
186
+ return base.replace(domain, '$1') + href;
187
+ } else {
188
+ return base + href;
189
+ }
190
+ }
191
+
192
+ const noopTest = { exec: function noopTest() {} };
193
+
194
+ function merge(obj) {
195
+ let i = 1,
196
+ target,
197
+ key;
198
+
199
+ for (; i < arguments.length; i++) {
200
+ target = arguments[i];
201
+ for (key in target) {
202
+ if (Object.prototype.hasOwnProperty.call(target, key)) {
203
+ obj[key] = target[key];
204
+ }
205
+ }
206
+ }
207
+
208
+ return obj;
209
+ }
210
+
211
+ function splitCells(tableRow, count) {
212
+ // ensure that every cell-delimiting pipe has a space
213
+ // before it to distinguish it from an escaped pipe
214
+ const row = tableRow.replace(/\|/g, (match, offset, str) => {
215
+ let escaped = false,
216
+ curr = offset;
217
+ while (--curr >= 0 && str[curr] === '\\') escaped = !escaped;
218
+ if (escaped) {
219
+ // odd number of slashes means | is escaped
220
+ // so we leave it alone
221
+ return '|';
222
+ } else {
223
+ // add space before unescaped |
224
+ return ' |';
225
+ }
226
+ }),
227
+ cells = row.split(/ \|/);
228
+ let i = 0;
229
+
230
+ // First/last cell in a row cannot be empty if it has no leading/trailing pipe
231
+ if (!cells[0].trim()) { cells.shift(); }
232
+ if (!cells[cells.length - 1].trim()) { cells.pop(); }
233
+
234
+ if (cells.length > count) {
235
+ cells.splice(count);
236
+ } else {
237
+ while (cells.length < count) cells.push('');
238
+ }
239
+
240
+ for (; i < cells.length; i++) {
241
+ // leading or trailing whitespace is ignored per the gfm spec
242
+ cells[i] = cells[i].trim().replace(/\\\|/g, '|');
243
+ }
244
+ return cells;
245
+ }
246
+
247
+ // Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
248
+ // /c*$/ is vulnerable to REDOS.
249
+ // invert: Remove suffix of non-c chars instead. Default falsey.
250
+ function rtrim(str, c, invert) {
251
+ const l = str.length;
252
+ if (l === 0) {
253
+ return '';
254
+ }
255
+
256
+ // Length of suffix matching the invert condition.
257
+ let suffLen = 0;
258
+
259
+ // Step left until we fail to match the invert condition.
260
+ while (suffLen < l) {
261
+ const currChar = str.charAt(l - suffLen - 1);
262
+ if (currChar === c && !invert) {
263
+ suffLen++;
264
+ } else if (currChar !== c && invert) {
265
+ suffLen++;
266
+ } else {
267
+ break;
268
+ }
269
+ }
270
+
271
+ return str.substr(0, l - suffLen);
272
+ }
273
+
274
+ function findClosingBracket(str, b) {
275
+ if (str.indexOf(b[1]) === -1) {
276
+ return -1;
277
+ }
278
+ const l = str.length;
279
+ let level = 0,
280
+ i = 0;
281
+ for (; i < l; i++) {
282
+ if (str[i] === '\\') {
283
+ i++;
284
+ } else if (str[i] === b[0]) {
285
+ level++;
286
+ } else if (str[i] === b[1]) {
287
+ level--;
288
+ if (level < 0) {
289
+ return i;
290
+ }
291
+ }
292
+ }
293
+ return -1;
294
+ }
295
+
296
+ function checkSanitizeDeprecation(opt) {
297
+ if (opt && opt.sanitize && !opt.silent) {
298
+ console.warn('marked(): sanitize and sanitizer parameters are deprecated since version 0.7.0, should not be used and will be removed in the future. Read more here: https://marked.js.org/#/USING_ADVANCED.md#options');
299
+ }
300
+ }
301
+
302
+ // copied from https://stackoverflow.com/a/5450113/806777
303
+ function repeatString(pattern, count) {
304
+ if (count < 1) {
305
+ return '';
306
+ }
307
+ let result = '';
308
+ while (count > 1) {
309
+ if (count & 1) {
310
+ result += pattern;
311
+ }
312
+ count >>= 1;
313
+ pattern += pattern;
314
+ }
315
+ return result + pattern;
316
+ }
317
+
318
+ function outputLink(cap, link, raw, lexer) {
319
+ const href = link.href;
320
+ const title = link.title ? escape(link.title) : null;
321
+ const text = cap[1].replace(/\\([\[\]])/g, '$1');
322
+
323
+ if (cap[0].charAt(0) !== '!') {
324
+ lexer.state.inLink = true;
325
+ const token = {
326
+ type: 'link',
327
+ raw,
328
+ href,
329
+ title,
330
+ text,
331
+ tokens: lexer.inlineTokens(text, [])
332
+ };
333
+ lexer.state.inLink = false;
334
+ return token;
335
+ } else {
336
+ return {
337
+ type: 'image',
338
+ raw,
339
+ href,
340
+ title,
341
+ text: escape(text)
342
+ };
343
+ }
344
+ }
345
+
346
+ function indentCodeCompensation(raw, text) {
347
+ const matchIndentToCode = raw.match(/^(\s+)(?:```)/);
348
+
349
+ if (matchIndentToCode === null) {
350
+ return text;
351
+ }
352
+
353
+ const indentToCode = matchIndentToCode[1];
354
+
355
+ return text
356
+ .split('\n')
357
+ .map(node => {
358
+ const matchIndentInNode = node.match(/^\s+/);
359
+ if (matchIndentInNode === null) {
360
+ return node;
361
+ }
362
+
363
+ const [indentInNode] = matchIndentInNode;
364
+
365
+ if (indentInNode.length >= indentToCode.length) {
366
+ return node.slice(indentToCode.length);
367
+ }
368
+
369
+ return node;
370
+ })
371
+ .join('\n');
372
+ }
373
+
374
+ /**
375
+ * Tokenizer
376
+ */
377
+ class Tokenizer {
378
+ constructor(options) {
379
+ this.options = options || defaults;
380
+ }
381
+
382
+ space(src) {
383
+ const cap = this.rules.block.newline.exec(src);
384
+ if (cap) {
385
+ if (cap[0].length > 1) {
386
+ return {
387
+ type: 'space',
388
+ raw: cap[0]
389
+ };
390
+ }
391
+ return { raw: '\n' };
392
+ }
393
+ }
394
+
395
+ code(src) {
396
+ const cap = this.rules.block.code.exec(src);
397
+ if (cap) {
398
+ const text = cap[0].replace(/^ {1,4}/gm, '');
399
+ return {
400
+ type: 'code',
401
+ raw: cap[0],
402
+ codeBlockStyle: 'indented',
403
+ text: !this.options.pedantic
404
+ ? rtrim(text, '\n')
405
+ : text
406
+ };
407
+ }
408
+ }
409
+
410
+ fences(src) {
411
+ const cap = this.rules.block.fences.exec(src);
412
+ if (cap) {
413
+ const raw = cap[0];
414
+ const text = indentCodeCompensation(raw, cap[3] || '');
415
+
416
+ return {
417
+ type: 'code',
418
+ raw,
419
+ lang: cap[2] ? cap[2].trim() : cap[2],
420
+ text
421
+ };
422
+ }
423
+ }
424
+
425
+ heading(src) {
426
+ const cap = this.rules.block.heading.exec(src);
427
+ if (cap) {
428
+ let text = cap[2].trim();
429
+
430
+ // remove trailing #s
431
+ if (/#$/.test(text)) {
432
+ const trimmed = rtrim(text, '#');
433
+ if (this.options.pedantic) {
434
+ text = trimmed.trim();
435
+ } else if (!trimmed || / $/.test(trimmed)) {
436
+ // CommonMark requires space before trailing #s
437
+ text = trimmed.trim();
438
+ }
439
+ }
440
+
441
+ const token = {
442
+ type: 'heading',
443
+ raw: cap[0],
444
+ depth: cap[1].length,
445
+ text: text,
446
+ tokens: []
447
+ };
448
+ this.lexer.inline(token.text, token.tokens);
449
+ return token;
450
+ }
451
+ }
452
+
453
+ hr(src) {
454
+ const cap = this.rules.block.hr.exec(src);
455
+ if (cap) {
456
+ return {
457
+ type: 'hr',
458
+ raw: cap[0]
459
+ };
460
+ }
461
+ }
462
+
463
+ blockquote(src) {
464
+ const cap = this.rules.block.blockquote.exec(src);
465
+ if (cap) {
466
+ const text = cap[0].replace(/^ *> ?/gm, '');
467
+
468
+ return {
469
+ type: 'blockquote',
470
+ raw: cap[0],
471
+ tokens: this.lexer.blockTokens(text, []),
472
+ text
473
+ };
474
+ }
475
+ }
476
+
477
+ list(src) {
478
+ let cap = this.rules.block.list.exec(src);
479
+ if (cap) {
480
+ let raw, istask, ischecked, indent, i, blankLine, endsWithBlankLine,
481
+ line, nextLine, rawLine, itemContents;
482
+
483
+ let bull = cap[1].trim();
484
+ const isordered = bull.length > 1;
485
+
486
+ const list = {
487
+ type: 'list',
488
+ raw: '',
489
+ ordered: isordered,
490
+ start: isordered ? +bull.slice(0, -1) : '',
491
+ loose: false,
492
+ items: []
493
+ };
494
+
495
+ bull = isordered ? `\\d{1,9}\\${bull.slice(-1)}` : `\\${bull}`;
496
+
497
+ if (this.options.pedantic) {
498
+ bull = isordered ? bull : '[*+-]';
499
+ }
500
+
501
+ // Get next list item
502
+ const itemRegex = new RegExp(`^( {0,3}${bull})((?: [^\\n]*)?(?:\\n|$))`);
503
+
504
+ // Check if current bullet point can start a new List Item
505
+ while (src) {
506
+ if (!(cap = itemRegex.exec(src))) {
507
+ break;
508
+ }
509
+
510
+ if (this.rules.block.hr.test(src)) { // End list if bullet was actually HR (possibly move into itemRegex?)
511
+ break;
512
+ }
513
+
514
+ raw = cap[0];
515
+ src = src.substring(raw.length);
516
+
517
+ line = cap[2].split('\n', 1)[0];
518
+ nextLine = src.split('\n', 1)[0];
519
+
520
+ if (this.options.pedantic) {
521
+ indent = 2;
522
+ itemContents = line.trimLeft();
523
+ } else {
524
+ indent = cap[2].search(/[^ ]/); // Find first non-space char
525
+ indent = indent > 4 ? 1 : indent; // Treat indented code blocks (> 4 spaces) as having only 1 indent
526
+ itemContents = line.slice(indent);
527
+ indent += cap[1].length;
528
+ }
529
+
530
+ blankLine = false;
531
+
532
+ if (!line && /^ *$/.test(nextLine)) { // Items begin with at most one blank line
533
+ raw += nextLine + '\n';
534
+ src = src.substring(nextLine.length + 1);
535
+ list.loose = true;
536
+ }
537
+
538
+ const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])`);
539
+
540
+ // Check if following lines should be included in List Item
541
+ while (src && !list.loose) {
542
+ rawLine = src.split('\n', 1)[0];
543
+ line = rawLine;
544
+
545
+ // Re-align to follow commonmark nesting rules
546
+ if (this.options.pedantic) {
547
+ line = line.replace(/^ {1,4}(?=( {4})*[^ ])/g, ' ');
548
+ }
549
+
550
+ // End list item if found start of new bullet
551
+ if (nextBulletRegex.test(line)) {
552
+ break;
553
+ }
554
+
555
+ if (line.search(/[^ ]/) >= indent || !line.trim()) { // Dedent if possible
556
+ itemContents += '\n' + line.slice(indent);
557
+ } else if (!blankLine) { // Until blank line, item doesn't need indentation
558
+ itemContents += '\n' + line;
559
+ } else { // Otherwise, improper indentation ends this item
560
+ break;
561
+ }
562
+
563
+ if (!blankLine && !line.trim()) { // Check if current line is blank
564
+ blankLine = true;
565
+ }
566
+
567
+ raw += rawLine + '\n';
568
+ src = src.substring(rawLine.length + 1);
569
+ }
570
+
571
+ if (!list.loose) {
572
+ // If the previous item ended with a blank line, the list is loose
573
+ if (endsWithBlankLine) {
574
+ list.loose = true;
575
+ } else if (/\n *\n *$/.test(raw)) {
576
+ endsWithBlankLine = true;
577
+ }
578
+ }
579
+
580
+ // Check for task list items
581
+ if (this.options.gfm) {
582
+ istask = /^\[[ xX]\] /.exec(itemContents);
583
+ if (istask) {
584
+ ischecked = istask[0] !== '[ ] ';
585
+ itemContents = itemContents.replace(/^\[[ xX]\] +/, '');
586
+ }
587
+ }
588
+
589
+ list.items.push({
590
+ type: 'list_item',
591
+ raw: raw,
592
+ task: !!istask,
593
+ checked: ischecked,
594
+ loose: false,
595
+ text: itemContents
596
+ });
597
+
598
+ list.raw += raw;
599
+ }
600
+
601
+ // Do not consume newlines at end of final item. Alternatively, make itemRegex *start* with any newlines to simplify/speed up endsWithBlankLine logic
602
+ list.items[list.items.length - 1].raw = raw.trimRight();
603
+ list.items[list.items.length - 1].text = itemContents.trimRight();
604
+ list.raw = list.raw.trimRight();
605
+
606
+ const l = list.items.length;
607
+
608
+ // Item child tokens handled here at end because we needed to have the final item to trim it first
609
+ for (i = 0; i < l; i++) {
610
+ this.lexer.state.top = false;
611
+ list.items[i].tokens = this.lexer.blockTokens(list.items[i].text, []);
612
+ if (!list.loose && list.items[i].tokens.some(t => t.type === 'space')) {
613
+ list.loose = true;
614
+ list.items[i].loose = true;
615
+ }
616
+ }
617
+
618
+ return list;
619
+ }
620
+ }
621
+
622
+ html(src) {
623
+ const cap = this.rules.block.html.exec(src);
624
+ if (cap) {
625
+ const token = {
626
+ type: 'html',
627
+ raw: cap[0],
628
+ pre: !this.options.sanitizer
629
+ && (cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style'),
630
+ text: cap[0]
631
+ };
632
+ if (this.options.sanitize) {
633
+ token.type = 'paragraph';
634
+ token.text = this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0]);
635
+ token.tokens = [];
636
+ this.lexer.inline(token.text, token.tokens);
637
+ }
638
+ return token;
639
+ }
640
+ }
641
+
642
+ def(src) {
643
+ const cap = this.rules.block.def.exec(src);
644
+ if (cap) {
645
+ if (cap[3]) cap[3] = cap[3].substring(1, cap[3].length - 1);
646
+ const tag = cap[1].toLowerCase().replace(/\s+/g, ' ');
647
+ return {
648
+ type: 'def',
649
+ tag,
650
+ raw: cap[0],
651
+ href: cap[2],
652
+ title: cap[3]
653
+ };
654
+ }
655
+ }
656
+
657
+ table(src) {
658
+ const cap = this.rules.block.table.exec(src);
659
+ if (cap) {
660
+ const item = {
661
+ type: 'table',
662
+ header: splitCells(cap[1]).map(c => { return { text: c }; }),
663
+ align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
664
+ rows: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : []
665
+ };
666
+
667
+ if (item.header.length === item.align.length) {
668
+ item.raw = cap[0];
669
+
670
+ let l = item.align.length;
671
+ let i, j, k, row;
672
+ for (i = 0; i < l; i++) {
673
+ if (/^ *-+: *$/.test(item.align[i])) {
674
+ item.align[i] = 'right';
675
+ } else if (/^ *:-+: *$/.test(item.align[i])) {
676
+ item.align[i] = 'center';
677
+ } else if (/^ *:-+ *$/.test(item.align[i])) {
678
+ item.align[i] = 'left';
679
+ } else {
680
+ item.align[i] = null;
681
+ }
682
+ }
683
+
684
+ l = item.rows.length;
685
+ for (i = 0; i < l; i++) {
686
+ item.rows[i] = splitCells(item.rows[i], item.header.length).map(c => { return { text: c }; });
687
+ }
688
+
689
+ // parse child tokens inside headers and cells
690
+
691
+ // header child tokens
692
+ l = item.header.length;
693
+ for (j = 0; j < l; j++) {
694
+ item.header[j].tokens = [];
695
+ this.lexer.inlineTokens(item.header[j].text, item.header[j].tokens);
696
+ }
697
+
698
+ // cell child tokens
699
+ l = item.rows.length;
700
+ for (j = 0; j < l; j++) {
701
+ row = item.rows[j];
702
+ for (k = 0; k < row.length; k++) {
703
+ row[k].tokens = [];
704
+ this.lexer.inlineTokens(row[k].text, row[k].tokens);
705
+ }
706
+ }
707
+
708
+ return item;
709
+ }
710
+ }
711
+ }
712
+
713
+ lheading(src) {
714
+ const cap = this.rules.block.lheading.exec(src);
715
+ if (cap) {
716
+ const token = {
717
+ type: 'heading',
718
+ raw: cap[0],
719
+ depth: cap[2].charAt(0) === '=' ? 1 : 2,
720
+ text: cap[1],
721
+ tokens: []
722
+ };
723
+ this.lexer.inline(token.text, token.tokens);
724
+ return token;
725
+ }
726
+ }
727
+
728
+ paragraph(src) {
729
+ const cap = this.rules.block.paragraph.exec(src);
730
+ if (cap) {
731
+ const token = {
732
+ type: 'paragraph',
733
+ raw: cap[0],
734
+ text: cap[1].charAt(cap[1].length - 1) === '\n'
735
+ ? cap[1].slice(0, -1)
736
+ : cap[1],
737
+ tokens: []
738
+ };
739
+ this.lexer.inline(token.text, token.tokens);
740
+ return token;
741
+ }
742
+ }
743
+
744
+ text(src) {
745
+ const cap = this.rules.block.text.exec(src);
746
+ if (cap) {
747
+ const token = {
748
+ type: 'text',
749
+ raw: cap[0],
750
+ text: cap[0],
751
+ tokens: []
752
+ };
753
+ this.lexer.inline(token.text, token.tokens);
754
+ return token;
755
+ }
756
+ }
757
+
758
+ escape(src) {
759
+ const cap = this.rules.inline.escape.exec(src);
760
+ if (cap) {
761
+ return {
762
+ type: 'escape',
763
+ raw: cap[0],
764
+ text: escape(cap[1])
765
+ };
766
+ }
767
+ }
768
+
769
+ tag(src) {
770
+ const cap = this.rules.inline.tag.exec(src);
771
+ if (cap) {
772
+ if (!this.lexer.state.inLink && /^<a /i.test(cap[0])) {
773
+ this.lexer.state.inLink = true;
774
+ } else if (this.lexer.state.inLink && /^<\/a>/i.test(cap[0])) {
775
+ this.lexer.state.inLink = false;
776
+ }
777
+ if (!this.lexer.state.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
778
+ this.lexer.state.inRawBlock = true;
779
+ } else if (this.lexer.state.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
780
+ this.lexer.state.inRawBlock = false;
781
+ }
782
+
783
+ return {
784
+ type: this.options.sanitize
785
+ ? 'text'
786
+ : 'html',
787
+ raw: cap[0],
788
+ inLink: this.lexer.state.inLink,
789
+ inRawBlock: this.lexer.state.inRawBlock,
790
+ text: this.options.sanitize
791
+ ? (this.options.sanitizer
792
+ ? this.options.sanitizer(cap[0])
793
+ : escape(cap[0]))
794
+ : cap[0]
795
+ };
796
+ }
797
+ }
798
+
799
+ link(src) {
800
+ const cap = this.rules.inline.link.exec(src);
801
+ if (cap) {
802
+ const trimmedUrl = cap[2].trim();
803
+ if (!this.options.pedantic && /^</.test(trimmedUrl)) {
804
+ // commonmark requires matching angle brackets
805
+ if (!(/>$/.test(trimmedUrl))) {
806
+ return;
807
+ }
808
+
809
+ // ending angle bracket cannot be escaped
810
+ const rtrimSlash = rtrim(trimmedUrl.slice(0, -1), '\\');
811
+ if ((trimmedUrl.length - rtrimSlash.length) % 2 === 0) {
812
+ return;
813
+ }
814
+ } else {
815
+ // find closing parenthesis
816
+ const lastParenIndex = findClosingBracket(cap[2], '()');
817
+ if (lastParenIndex > -1) {
818
+ const start = cap[0].indexOf('!') === 0 ? 5 : 4;
819
+ const linkLen = start + cap[1].length + lastParenIndex;
820
+ cap[2] = cap[2].substring(0, lastParenIndex);
821
+ cap[0] = cap[0].substring(0, linkLen).trim();
822
+ cap[3] = '';
823
+ }
824
+ }
825
+ let href = cap[2];
826
+ let title = '';
827
+ if (this.options.pedantic) {
828
+ // split pedantic href and title
829
+ const link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href);
830
+
831
+ if (link) {
832
+ href = link[1];
833
+ title = link[3];
834
+ }
835
+ } else {
836
+ title = cap[3] ? cap[3].slice(1, -1) : '';
837
+ }
838
+
839
+ href = href.trim();
840
+ if (/^</.test(href)) {
841
+ if (this.options.pedantic && !(/>$/.test(trimmedUrl))) {
842
+ // pedantic allows starting angle bracket without ending angle bracket
843
+ href = href.slice(1);
844
+ } else {
845
+ href = href.slice(1, -1);
846
+ }
847
+ }
848
+ return outputLink(cap, {
849
+ href: href ? href.replace(this.rules.inline._escapes, '$1') : href,
850
+ title: title ? title.replace(this.rules.inline._escapes, '$1') : title
851
+ }, cap[0], this.lexer);
852
+ }
853
+ }
854
+
855
+ reflink(src, links) {
856
+ let cap;
857
+ if ((cap = this.rules.inline.reflink.exec(src))
858
+ || (cap = this.rules.inline.nolink.exec(src))) {
859
+ let link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
860
+ link = links[link.toLowerCase()];
861
+ if (!link || !link.href) {
862
+ const text = cap[0].charAt(0);
863
+ return {
864
+ type: 'text',
865
+ raw: text,
866
+ text
867
+ };
868
+ }
869
+ return outputLink(cap, link, cap[0], this.lexer);
870
+ }
871
+ }
872
+
873
+ emStrong(src, maskedSrc, prevChar = '') {
874
+ let match = this.rules.inline.emStrong.lDelim.exec(src);
875
+ if (!match) return;
876
+
877
+ // _ can't be between two alphanumerics. \p{L}\p{N} includes non-english alphabet/numbers as well
878
+ if (match[3] && prevChar.match(/[\p{L}\p{N}]/u)) return;
879
+
880
+ const nextChar = match[1] || match[2] || '';
881
+
882
+ if (!nextChar || (nextChar && (prevChar === '' || this.rules.inline.punctuation.exec(prevChar)))) {
883
+ const lLength = match[0].length - 1;
884
+ let rDelim, rLength, delimTotal = lLength, midDelimTotal = 0;
885
+
886
+ const endReg = match[0][0] === '*' ? this.rules.inline.emStrong.rDelimAst : this.rules.inline.emStrong.rDelimUnd;
887
+ endReg.lastIndex = 0;
888
+
889
+ // Clip maskedSrc to same section of string as src (move to lexer?)
890
+ maskedSrc = maskedSrc.slice(-1 * src.length + lLength);
891
+
892
+ while ((match = endReg.exec(maskedSrc)) != null) {
893
+ rDelim = match[1] || match[2] || match[3] || match[4] || match[5] || match[6];
894
+
895
+ if (!rDelim) continue; // skip single * in __abc*abc__
896
+
897
+ rLength = rDelim.length;
898
+
899
+ if (match[3] || match[4]) { // found another Left Delim
900
+ delimTotal += rLength;
901
+ continue;
902
+ } else if (match[5] || match[6]) { // either Left or Right Delim
903
+ if (lLength % 3 && !((lLength + rLength) % 3)) {
904
+ midDelimTotal += rLength;
905
+ continue; // CommonMark Emphasis Rules 9-10
906
+ }
907
+ }
908
+
909
+ delimTotal -= rLength;
910
+
911
+ if (delimTotal > 0) continue; // Haven't found enough closing delimiters
912
+
913
+ // Remove extra characters. *a*** -> *a*
914
+ rLength = Math.min(rLength, rLength + delimTotal + midDelimTotal);
915
+
916
+ // Create `em` if smallest delimiter has odd char count. *a***
917
+ if (Math.min(lLength, rLength) % 2) {
918
+ const text = src.slice(1, lLength + match.index + rLength);
919
+ return {
920
+ type: 'em',
921
+ raw: src.slice(0, lLength + match.index + rLength + 1),
922
+ text,
923
+ tokens: this.lexer.inlineTokens(text, [])
924
+ };
925
+ }
926
+
927
+ // Create 'strong' if smallest delimiter has even char count. **a***
928
+ const text = src.slice(2, lLength + match.index + rLength - 1);
929
+ return {
930
+ type: 'strong',
931
+ raw: src.slice(0, lLength + match.index + rLength + 1),
932
+ text,
933
+ tokens: this.lexer.inlineTokens(text, [])
934
+ };
935
+ }
936
+ }
937
+ }
938
+
939
+ codespan(src) {
940
+ const cap = this.rules.inline.code.exec(src);
941
+ if (cap) {
942
+ let text = cap[2].replace(/\n/g, ' ');
943
+ const hasNonSpaceChars = /[^ ]/.test(text);
944
+ const hasSpaceCharsOnBothEnds = /^ /.test(text) && / $/.test(text);
945
+ if (hasNonSpaceChars && hasSpaceCharsOnBothEnds) {
946
+ text = text.substring(1, text.length - 1);
947
+ }
948
+ text = escape(text, true);
949
+ return {
950
+ type: 'codespan',
951
+ raw: cap[0],
952
+ text
953
+ };
954
+ }
955
+ }
956
+
957
+ br(src) {
958
+ const cap = this.rules.inline.br.exec(src);
959
+ if (cap) {
960
+ return {
961
+ type: 'br',
962
+ raw: cap[0]
963
+ };
964
+ }
965
+ }
966
+
967
+ del(src) {
968
+ const cap = this.rules.inline.del.exec(src);
969
+ if (cap) {
970
+ return {
971
+ type: 'del',
972
+ raw: cap[0],
973
+ text: cap[2],
974
+ tokens: this.lexer.inlineTokens(cap[2], [])
975
+ };
976
+ }
977
+ }
978
+
979
+ autolink(src, mangle) {
980
+ const cap = this.rules.inline.autolink.exec(src);
981
+ if (cap) {
982
+ let text, href;
983
+ if (cap[2] === '@') {
984
+ text = escape(this.options.mangle ? mangle(cap[1]) : cap[1]);
985
+ href = 'mailto:' + text;
986
+ } else {
987
+ text = escape(cap[1]);
988
+ href = text;
989
+ }
990
+
991
+ return {
992
+ type: 'link',
993
+ raw: cap[0],
994
+ text,
995
+ href,
996
+ tokens: [
997
+ {
998
+ type: 'text',
999
+ raw: text,
1000
+ text
1001
+ }
1002
+ ]
1003
+ };
1004
+ }
1005
+ }
1006
+
1007
+ url(src, mangle) {
1008
+ let cap;
1009
+ if (cap = this.rules.inline.url.exec(src)) {
1010
+ let text, href;
1011
+ if (cap[2] === '@') {
1012
+ text = escape(this.options.mangle ? mangle(cap[0]) : cap[0]);
1013
+ href = 'mailto:' + text;
1014
+ } else {
1015
+ // do extended autolink path validation
1016
+ let prevCapZero;
1017
+ do {
1018
+ prevCapZero = cap[0];
1019
+ cap[0] = this.rules.inline._backpedal.exec(cap[0])[0];
1020
+ } while (prevCapZero !== cap[0]);
1021
+ text = escape(cap[0]);
1022
+ if (cap[1] === 'www.') {
1023
+ href = 'http://' + text;
1024
+ } else {
1025
+ href = text;
1026
+ }
1027
+ }
1028
+ return {
1029
+ type: 'link',
1030
+ raw: cap[0],
1031
+ text,
1032
+ href,
1033
+ tokens: [
1034
+ {
1035
+ type: 'text',
1036
+ raw: text,
1037
+ text
1038
+ }
1039
+ ]
1040
+ };
1041
+ }
1042
+ }
1043
+
1044
+ inlineText(src, smartypants) {
1045
+ const cap = this.rules.inline.text.exec(src);
1046
+ if (cap) {
1047
+ let text;
1048
+ if (this.lexer.state.inRawBlock) {
1049
+ text = this.options.sanitize ? (this.options.sanitizer ? this.options.sanitizer(cap[0]) : escape(cap[0])) : cap[0];
1050
+ } else {
1051
+ text = escape(this.options.smartypants ? smartypants(cap[0]) : cap[0]);
1052
+ }
1053
+ return {
1054
+ type: 'text',
1055
+ raw: cap[0],
1056
+ text
1057
+ };
1058
+ }
1059
+ }
1060
+ }
1061
+
1062
+ /**
1063
+ * Block-Level Grammar
1064
+ */
1065
+ const block = {
1066
+ newline: /^(?: *(?:\n|$))+/,
1067
+ code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
1068
+ fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
1069
+ hr: /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,
1070
+ heading: /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,
1071
+ blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
1072
+ list: /^( {0,3}bull)( [^\n]+?)?(?:\n|$)/,
1073
+ html: '^ {0,3}(?:' // optional indentation
1074
+ + '<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)' // (1)
1075
+ + '|comment[^\\n]*(\\n+|$)' // (2)
1076
+ + '|<\\?[\\s\\S]*?(?:\\?>\\n*|$)' // (3)
1077
+ + '|<![A-Z][\\s\\S]*?(?:>\\n*|$)' // (4)
1078
+ + '|<!\\[CDATA\\[[\\s\\S]*?(?:\\]\\]>\\n*|$)' // (5)
1079
+ + '|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (6)
1080
+ + '|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (7) open tag
1081
+ + '|</(?!script|pre|style|textarea)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (7) closing tag
1082
+ + ')',
1083
+ def: /^ {0,3}\[(label)\]: *\n? *<?([^\s>]+)>?(?:(?: +\n? *| *\n *)(title))? *(?:\n+|$)/,
1084
+ table: noopTest,
1085
+ lheading: /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/,
1086
+ // regex template, placeholders will be replaced according to different paragraph
1087
+ // interruption rules of commonmark and the original markdown spec:
1088
+ _paragraph: /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/,
1089
+ text: /^[^\n]+/
1090
+ };
1091
+
1092
+ block._label = /(?!\s*\])(?:\\[\[\]]|[^\[\]])+/;
1093
+ block._title = /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/;
1094
+ block.def = edit(block.def)
1095
+ .replace('label', block._label)
1096
+ .replace('title', block._title)
1097
+ .getRegex();
1098
+
1099
+ block.bullet = /(?:[*+-]|\d{1,9}[.)])/;
1100
+ block.listItemStart = edit(/^( *)(bull) */)
1101
+ .replace('bull', block.bullet)
1102
+ .getRegex();
1103
+
1104
+ block.list = edit(block.list)
1105
+ .replace(/bull/g, block.bullet)
1106
+ .replace('hr', '\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))')
1107
+ .replace('def', '\\n+(?=' + block.def.source + ')')
1108
+ .getRegex();
1109
+
1110
+ block._tag = 'address|article|aside|base|basefont|blockquote|body|caption'
1111
+ + '|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption'
1112
+ + '|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe'
1113
+ + '|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option'
1114
+ + '|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr'
1115
+ + '|track|ul';
1116
+ block._comment = /<!--(?!-?>)[\s\S]*?(?:-->|$)/;
1117
+ block.html = edit(block.html, 'i')
1118
+ .replace('comment', block._comment)
1119
+ .replace('tag', block._tag)
1120
+ .replace('attribute', / +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/)
1121
+ .getRegex();
1122
+
1123
+ block.paragraph = edit(block._paragraph)
1124
+ .replace('hr', block.hr)
1125
+ .replace('heading', ' {0,3}#{1,6} ')
1126
+ .replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
1127
+ .replace('|table', '')
1128
+ .replace('blockquote', ' {0,3}>')
1129
+ .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
1130
+ .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
1131
+ .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
1132
+ .replace('tag', block._tag) // pars can be interrupted by type (6) html blocks
1133
+ .getRegex();
1134
+
1135
+ block.blockquote = edit(block.blockquote)
1136
+ .replace('paragraph', block.paragraph)
1137
+ .getRegex();
1138
+
1139
+ /**
1140
+ * Normal Block Grammar
1141
+ */
1142
+
1143
+ block.normal = merge({}, block);
1144
+
1145
+ /**
1146
+ * GFM Block Grammar
1147
+ */
1148
+
1149
+ block.gfm = merge({}, block.normal, {
1150
+ table: '^ *([^\\n ].*\\|.*)\\n' // Header
1151
+ + ' {0,3}(?:\\| *)?(:?-+:? *(?:\\| *:?-+:? *)*)(?:\\| *)?' // Align
1152
+ + '(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)' // Cells
1153
+ });
1154
+
1155
+ block.gfm.table = edit(block.gfm.table)
1156
+ .replace('hr', block.hr)
1157
+ .replace('heading', ' {0,3}#{1,6} ')
1158
+ .replace('blockquote', ' {0,3}>')
1159
+ .replace('code', ' {4}[^\\n]')
1160
+ .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
1161
+ .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
1162
+ .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
1163
+ .replace('tag', block._tag) // tables can be interrupted by type (6) html blocks
1164
+ .getRegex();
1165
+
1166
+ block.gfm.paragraph = edit(block._paragraph)
1167
+ .replace('hr', block.hr)
1168
+ .replace('heading', ' {0,3}#{1,6} ')
1169
+ .replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
1170
+ .replace('table', block.gfm.table) // interrupt paragraphs with table
1171
+ .replace('blockquote', ' {0,3}>')
1172
+ .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
1173
+ .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
1174
+ .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
1175
+ .replace('tag', block._tag) // pars can be interrupted by type (6) html blocks
1176
+ .getRegex();
1177
+ /**
1178
+ * Pedantic grammar (original John Gruber's loose markdown specification)
1179
+ */
1180
+
1181
+ block.pedantic = merge({}, block.normal, {
1182
+ html: edit(
1183
+ '^ *(?:comment *(?:\\n|\\s*$)'
1184
+ + '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
1185
+ + '|<tag(?:"[^"]*"|\'[^\']*\'|\\s[^\'"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))')
1186
+ .replace('comment', block._comment)
1187
+ .replace(/tag/g, '(?!(?:'
1188
+ + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub'
1189
+ + '|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)'
1190
+ + '\\b)\\w+(?!:|[^\\w\\s@]*@)\\b')
1191
+ .getRegex(),
1192
+ def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
1193
+ heading: /^(#{1,6})(.*)(?:\n+|$)/,
1194
+ fences: noopTest, // fences not supported
1195
+ paragraph: edit(block.normal._paragraph)
1196
+ .replace('hr', block.hr)
1197
+ .replace('heading', ' *#{1,6} *[^\n]')
1198
+ .replace('lheading', block.lheading)
1199
+ .replace('blockquote', ' {0,3}>')
1200
+ .replace('|fences', '')
1201
+ .replace('|list', '')
1202
+ .replace('|html', '')
1203
+ .getRegex()
1204
+ });
1205
+
1206
+ /**
1207
+ * Inline-Level Grammar
1208
+ */
1209
+ const inline = {
1210
+ escape: /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/,
1211
+ autolink: /^<(scheme:[^\s\x00-\x1f<>]*|email)>/,
1212
+ url: noopTest,
1213
+ tag: '^comment'
1214
+ + '|^</[a-zA-Z][\\w:-]*\\s*>' // self-closing tag
1215
+ + '|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>' // open tag
1216
+ + '|^<\\?[\\s\\S]*?\\?>' // processing instruction, e.g. <?php ?>
1217
+ + '|^<![a-zA-Z]+\\s[\\s\\S]*?>' // declaration, e.g. <!DOCTYPE html>
1218
+ + '|^<!\\[CDATA\\[[\\s\\S]*?\\]\\]>', // CDATA section
1219
+ link: /^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/,
1220
+ reflink: /^!?\[(label)\]\[(?!\s*\])((?:\\[\[\]]?|[^\[\]\\])+)\]/,
1221
+ nolink: /^!?\[(?!\s*\])((?:\[[^\[\]]*\]|\\[\[\]]|[^\[\]])*)\](?:\[\])?/,
1222
+ reflinkSearch: 'reflink|nolink(?!\\()',
1223
+ emStrong: {
1224
+ lDelim: /^(?:\*+(?:([punct_])|[^\s*]))|^_+(?:([punct*])|([^\s_]))/,
1225
+ // (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.
1226
+ // () Skip orphan delim inside strong (1) #*** (2) a***#, a*** (3) #***a, ***a (4) ***# (5) #***# (6) a***a
1227
+ rDelimAst: /^[^_*]*?\_\_[^_*]*?\*[^_*]*?(?=\_\_)|[punct_](\*+)(?=[\s]|$)|[^punct*_\s](\*+)(?=[punct_\s]|$)|[punct_\s](\*+)(?=[^punct*_\s])|[\s](\*+)(?=[punct_])|[punct_](\*+)(?=[punct_])|[^punct*_\s](\*+)(?=[^punct*_\s])/,
1228
+ rDelimUnd: /^[^_*]*?\*\*[^_*]*?\_[^_*]*?(?=\*\*)|[punct*](\_+)(?=[\s]|$)|[^punct*_\s](\_+)(?=[punct*\s]|$)|[punct*\s](\_+)(?=[^punct*_\s])|[\s](\_+)(?=[punct*])|[punct*](\_+)(?=[punct*])/ // ^- Not allowed for _
1229
+ },
1230
+ code: /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,
1231
+ br: /^( {2,}|\\)\n(?!\s*$)/,
1232
+ del: noopTest,
1233
+ text: /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<!\[`*_]|\b_|$)|[^ ](?= {2,}\n)))/,
1234
+ punctuation: /^([\spunctuation])/
1235
+ };
1236
+
1237
+ // list of punctuation marks from CommonMark spec
1238
+ // without * and _ to handle the different emphasis markers * and _
1239
+ inline._punctuation = '!"#$%&\'()+\\-.,/:;<=>?@\\[\\]`^{|}~';
1240
+ inline.punctuation = edit(inline.punctuation).replace(/punctuation/g, inline._punctuation).getRegex();
1241
+
1242
+ // sequences em should skip over [title](link), `code`, <html>
1243
+ inline.blockSkip = /\[[^\]]*?\]\([^\)]*?\)|`[^`]*?`|<[^>]*?>/g;
1244
+ inline.escapedEmSt = /\\\*|\\_/g;
1245
+
1246
+ inline._comment = edit(block._comment).replace('(?:-->|$)', '-->').getRegex();
1247
+
1248
+ inline.emStrong.lDelim = edit(inline.emStrong.lDelim)
1249
+ .replace(/punct/g, inline._punctuation)
1250
+ .getRegex();
1251
+
1252
+ inline.emStrong.rDelimAst = edit(inline.emStrong.rDelimAst, 'g')
1253
+ .replace(/punct/g, inline._punctuation)
1254
+ .getRegex();
1255
+
1256
+ inline.emStrong.rDelimUnd = edit(inline.emStrong.rDelimUnd, 'g')
1257
+ .replace(/punct/g, inline._punctuation)
1258
+ .getRegex();
1259
+
1260
+ inline._escapes = /\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/g;
1261
+
1262
+ inline._scheme = /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/;
1263
+ inline._email = /[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_])/;
1264
+ inline.autolink = edit(inline.autolink)
1265
+ .replace('scheme', inline._scheme)
1266
+ .replace('email', inline._email)
1267
+ .getRegex();
1268
+
1269
+ inline._attribute = /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/;
1270
+
1271
+ inline.tag = edit(inline.tag)
1272
+ .replace('comment', inline._comment)
1273
+ .replace('attribute', inline._attribute)
1274
+ .getRegex();
1275
+
1276
+ inline._label = /(?:\[(?:\\.|[^\[\]\\])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
1277
+ inline._href = /<(?:\\.|[^\n<>\\])+>|[^\s\x00-\x1f]*/;
1278
+ inline._title = /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/;
1279
+
1280
+ inline.link = edit(inline.link)
1281
+ .replace('label', inline._label)
1282
+ .replace('href', inline._href)
1283
+ .replace('title', inline._title)
1284
+ .getRegex();
1285
+
1286
+ inline.reflink = edit(inline.reflink)
1287
+ .replace('label', inline._label)
1288
+ .getRegex();
1289
+
1290
+ inline.reflinkSearch = edit(inline.reflinkSearch, 'g')
1291
+ .replace('reflink', inline.reflink)
1292
+ .replace('nolink', inline.nolink)
1293
+ .getRegex();
1294
+
1295
+ /**
1296
+ * Normal Inline Grammar
1297
+ */
1298
+
1299
+ inline.normal = merge({}, inline);
1300
+
1301
+ /**
1302
+ * Pedantic Inline Grammar
1303
+ */
1304
+
1305
+ inline.pedantic = merge({}, inline.normal, {
1306
+ strong: {
1307
+ start: /^__|\*\*/,
1308
+ middle: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
1309
+ endAst: /\*\*(?!\*)/g,
1310
+ endUnd: /__(?!_)/g
1311
+ },
1312
+ em: {
1313
+ start: /^_|\*/,
1314
+ middle: /^()\*(?=\S)([\s\S]*?\S)\*(?!\*)|^_(?=\S)([\s\S]*?\S)_(?!_)/,
1315
+ endAst: /\*(?!\*)/g,
1316
+ endUnd: /_(?!_)/g
1317
+ },
1318
+ link: edit(/^!?\[(label)\]\((.*?)\)/)
1319
+ .replace('label', inline._label)
1320
+ .getRegex(),
1321
+ reflink: edit(/^!?\[(label)\]\s*\[([^\]]*)\]/)
1322
+ .replace('label', inline._label)
1323
+ .getRegex()
1324
+ });
1325
+
1326
+ /**
1327
+ * GFM Inline Grammar
1328
+ */
1329
+
1330
+ inline.gfm = merge({}, inline.normal, {
1331
+ escape: edit(inline.escape).replace('])', '~|])').getRegex(),
1332
+ _extended_email: /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,
1333
+ url: /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,
1334
+ _backpedal: /(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/,
1335
+ del: /^(~~?)(?=[^\s~])([\s\S]*?[^\s~])\1(?=[^~]|$)/,
1336
+ text: /^([`~]+|[^`~])(?:(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\<!\[`*~_]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)))/
1337
+ });
1338
+
1339
+ inline.gfm.url = edit(inline.gfm.url, 'i')
1340
+ .replace('email', inline.gfm._extended_email)
1341
+ .getRegex();
1342
+ /**
1343
+ * GFM + Line Breaks Inline Grammar
1344
+ */
1345
+
1346
+ inline.breaks = merge({}, inline.gfm, {
1347
+ br: edit(inline.br).replace('{2,}', '*').getRegex(),
1348
+ text: edit(inline.gfm.text)
1349
+ .replace('\\b_', '\\b_| {2,}\\n')
1350
+ .replace(/\{2,\}/g, '*')
1351
+ .getRegex()
1352
+ });
1353
+
1354
+ /**
1355
+ * smartypants text replacement
1356
+ */
1357
+ function smartypants(text) {
1358
+ return text
1359
+ // em-dashes
1360
+ .replace(/---/g, '\u2014')
1361
+ // en-dashes
1362
+ .replace(/--/g, '\u2013')
1363
+ // opening singles
1364
+ .replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018')
1365
+ // closing singles & apostrophes
1366
+ .replace(/'/g, '\u2019')
1367
+ // opening doubles
1368
+ .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c')
1369
+ // closing doubles
1370
+ .replace(/"/g, '\u201d')
1371
+ // ellipses
1372
+ .replace(/\.{3}/g, '\u2026');
1373
+ }
1374
+
1375
+ /**
1376
+ * mangle email addresses
1377
+ */
1378
+ function mangle(text) {
1379
+ let out = '',
1380
+ i,
1381
+ ch;
1382
+
1383
+ const l = text.length;
1384
+ for (i = 0; i < l; i++) {
1385
+ ch = text.charCodeAt(i);
1386
+ if (Math.random() > 0.5) {
1387
+ ch = 'x' + ch.toString(16);
1388
+ }
1389
+ out += '&#' + ch + ';';
1390
+ }
1391
+
1392
+ return out;
1393
+ }
1394
+
1395
+ /**
1396
+ * Block Lexer
1397
+ */
1398
+ class Lexer {
1399
+ constructor(options) {
1400
+ this.tokens = [];
1401
+ this.tokens.links = Object.create(null);
1402
+ this.options = options || defaults;
1403
+ this.options.tokenizer = this.options.tokenizer || new Tokenizer();
1404
+ this.tokenizer = this.options.tokenizer;
1405
+ this.tokenizer.options = this.options;
1406
+ this.tokenizer.lexer = this;
1407
+ this.inlineQueue = [];
1408
+ this.state = {
1409
+ inLink: false,
1410
+ inRawBlock: false,
1411
+ top: true
1412
+ };
1413
+
1414
+ const rules = {
1415
+ block: block.normal,
1416
+ inline: inline.normal
1417
+ };
1418
+
1419
+ if (this.options.pedantic) {
1420
+ rules.block = block.pedantic;
1421
+ rules.inline = inline.pedantic;
1422
+ } else if (this.options.gfm) {
1423
+ rules.block = block.gfm;
1424
+ if (this.options.breaks) {
1425
+ rules.inline = inline.breaks;
1426
+ } else {
1427
+ rules.inline = inline.gfm;
1428
+ }
1429
+ }
1430
+ this.tokenizer.rules = rules;
1431
+ }
1432
+
1433
+ /**
1434
+ * Expose Rules
1435
+ */
1436
+ static get rules() {
1437
+ return {
1438
+ block,
1439
+ inline
1440
+ };
1441
+ }
1442
+
1443
+ /**
1444
+ * Static Lex Method
1445
+ */
1446
+ static lex(src, options) {
1447
+ const lexer = new Lexer(options);
1448
+ return lexer.lex(src);
1449
+ }
1450
+
1451
+ /**
1452
+ * Static Lex Inline Method
1453
+ */
1454
+ static lexInline(src, options) {
1455
+ const lexer = new Lexer(options);
1456
+ return lexer.inlineTokens(src);
1457
+ }
1458
+
1459
+ /**
1460
+ * Preprocessing
1461
+ */
1462
+ lex(src) {
1463
+ src = src
1464
+ .replace(/\r\n|\r/g, '\n')
1465
+ .replace(/\t/g, ' ');
1466
+
1467
+ this.blockTokens(src, this.tokens);
1468
+
1469
+ let next;
1470
+ while (next = this.inlineQueue.shift()) {
1471
+ this.inlineTokens(next.src, next.tokens);
1472
+ }
1473
+
1474
+ return this.tokens;
1475
+ }
1476
+
1477
+ /**
1478
+ * Lexing
1479
+ */
1480
+ blockTokens(src, tokens = []) {
1481
+ if (this.options.pedantic) {
1482
+ src = src.replace(/^ +$/gm, '');
1483
+ }
1484
+ let token, lastToken, cutSrc, lastParagraphClipped;
1485
+
1486
+ while (src) {
1487
+ if (this.options.extensions
1488
+ && this.options.extensions.block
1489
+ && this.options.extensions.block.some((extTokenizer) => {
1490
+ if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
1491
+ src = src.substring(token.raw.length);
1492
+ tokens.push(token);
1493
+ return true;
1494
+ }
1495
+ return false;
1496
+ })) {
1497
+ continue;
1498
+ }
1499
+
1500
+ // newline
1501
+ if (token = this.tokenizer.space(src)) {
1502
+ src = src.substring(token.raw.length);
1503
+ if (token.type) {
1504
+ tokens.push(token);
1505
+ }
1506
+ continue;
1507
+ }
1508
+
1509
+ // code
1510
+ if (token = this.tokenizer.code(src)) {
1511
+ src = src.substring(token.raw.length);
1512
+ lastToken = tokens[tokens.length - 1];
1513
+ // An indented code block cannot interrupt a paragraph.
1514
+ if (lastToken && (lastToken.type === 'paragraph' || lastToken.type === 'text')) {
1515
+ lastToken.raw += '\n' + token.raw;
1516
+ lastToken.text += '\n' + token.text;
1517
+ this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
1518
+ } else {
1519
+ tokens.push(token);
1520
+ }
1521
+ continue;
1522
+ }
1523
+
1524
+ // fences
1525
+ if (token = this.tokenizer.fences(src)) {
1526
+ src = src.substring(token.raw.length);
1527
+ tokens.push(token);
1528
+ continue;
1529
+ }
1530
+
1531
+ // heading
1532
+ if (token = this.tokenizer.heading(src)) {
1533
+ src = src.substring(token.raw.length);
1534
+ tokens.push(token);
1535
+ continue;
1536
+ }
1537
+
1538
+ // hr
1539
+ if (token = this.tokenizer.hr(src)) {
1540
+ src = src.substring(token.raw.length);
1541
+ tokens.push(token);
1542
+ continue;
1543
+ }
1544
+
1545
+ // blockquote
1546
+ if (token = this.tokenizer.blockquote(src)) {
1547
+ src = src.substring(token.raw.length);
1548
+ tokens.push(token);
1549
+ continue;
1550
+ }
1551
+
1552
+ // list
1553
+ if (token = this.tokenizer.list(src)) {
1554
+ src = src.substring(token.raw.length);
1555
+ tokens.push(token);
1556
+ continue;
1557
+ }
1558
+
1559
+ // html
1560
+ if (token = this.tokenizer.html(src)) {
1561
+ src = src.substring(token.raw.length);
1562
+ tokens.push(token);
1563
+ continue;
1564
+ }
1565
+
1566
+ // def
1567
+ if (token = this.tokenizer.def(src)) {
1568
+ src = src.substring(token.raw.length);
1569
+ lastToken = tokens[tokens.length - 1];
1570
+ if (lastToken && (lastToken.type === 'paragraph' || lastToken.type === 'text')) {
1571
+ lastToken.raw += '\n' + token.raw;
1572
+ lastToken.text += '\n' + token.raw;
1573
+ this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
1574
+ } else if (!this.tokens.links[token.tag]) {
1575
+ this.tokens.links[token.tag] = {
1576
+ href: token.href,
1577
+ title: token.title
1578
+ };
1579
+ }
1580
+ continue;
1581
+ }
1582
+
1583
+ // table (gfm)
1584
+ if (token = this.tokenizer.table(src)) {
1585
+ src = src.substring(token.raw.length);
1586
+ tokens.push(token);
1587
+ continue;
1588
+ }
1589
+
1590
+ // lheading
1591
+ if (token = this.tokenizer.lheading(src)) {
1592
+ src = src.substring(token.raw.length);
1593
+ tokens.push(token);
1594
+ continue;
1595
+ }
1596
+
1597
+ // top-level paragraph
1598
+ // prevent paragraph consuming extensions by clipping 'src' to extension start
1599
+ cutSrc = src;
1600
+ if (this.options.extensions && this.options.extensions.startBlock) {
1601
+ let startIndex = Infinity;
1602
+ const tempSrc = src.slice(1);
1603
+ let tempStart;
1604
+ this.options.extensions.startBlock.forEach(function(getStartIndex) {
1605
+ tempStart = getStartIndex.call({ lexer: this }, tempSrc);
1606
+ if (typeof tempStart === 'number' && tempStart >= 0) { startIndex = Math.min(startIndex, tempStart); }
1607
+ });
1608
+ if (startIndex < Infinity && startIndex >= 0) {
1609
+ cutSrc = src.substring(0, startIndex + 1);
1610
+ }
1611
+ }
1612
+ if (this.state.top && (token = this.tokenizer.paragraph(cutSrc))) {
1613
+ lastToken = tokens[tokens.length - 1];
1614
+ if (lastParagraphClipped && lastToken.type === 'paragraph') {
1615
+ lastToken.raw += '\n' + token.raw;
1616
+ lastToken.text += '\n' + token.text;
1617
+ this.inlineQueue.pop();
1618
+ this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
1619
+ } else {
1620
+ tokens.push(token);
1621
+ }
1622
+ lastParagraphClipped = (cutSrc.length !== src.length);
1623
+ src = src.substring(token.raw.length);
1624
+ continue;
1625
+ }
1626
+
1627
+ // text
1628
+ if (token = this.tokenizer.text(src)) {
1629
+ src = src.substring(token.raw.length);
1630
+ lastToken = tokens[tokens.length - 1];
1631
+ if (lastToken && lastToken.type === 'text') {
1632
+ lastToken.raw += '\n' + token.raw;
1633
+ lastToken.text += '\n' + token.text;
1634
+ this.inlineQueue.pop();
1635
+ this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
1636
+ } else {
1637
+ tokens.push(token);
1638
+ }
1639
+ continue;
1640
+ }
1641
+
1642
+ if (src) {
1643
+ const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
1644
+ if (this.options.silent) {
1645
+ console.error(errMsg);
1646
+ break;
1647
+ } else {
1648
+ throw new Error(errMsg);
1649
+ }
1650
+ }
1651
+ }
1652
+
1653
+ this.state.top = true;
1654
+ return tokens;
1655
+ }
1656
+
1657
+ inline(src, tokens) {
1658
+ this.inlineQueue.push({ src, tokens });
1659
+ }
1660
+
1661
+ /**
1662
+ * Lexing/Compiling
1663
+ */
1664
+ inlineTokens(src, tokens = []) {
1665
+ let token, lastToken, cutSrc;
1666
+
1667
+ // String with links masked to avoid interference with em and strong
1668
+ let maskedSrc = src;
1669
+ let match;
1670
+ let keepPrevChar, prevChar;
1671
+
1672
+ // Mask out reflinks
1673
+ if (this.tokens.links) {
1674
+ const links = Object.keys(this.tokens.links);
1675
+ if (links.length > 0) {
1676
+ while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
1677
+ if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
1678
+ maskedSrc = maskedSrc.slice(0, match.index) + '[' + repeatString('a', match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
1679
+ }
1680
+ }
1681
+ }
1682
+ }
1683
+ // Mask out other blocks
1684
+ while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
1685
+ maskedSrc = maskedSrc.slice(0, match.index) + '[' + repeatString('a', match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
1686
+ }
1687
+
1688
+ // Mask out escaped em & strong delimiters
1689
+ while ((match = this.tokenizer.rules.inline.escapedEmSt.exec(maskedSrc)) != null) {
1690
+ maskedSrc = maskedSrc.slice(0, match.index) + '++' + maskedSrc.slice(this.tokenizer.rules.inline.escapedEmSt.lastIndex);
1691
+ }
1692
+
1693
+ while (src) {
1694
+ if (!keepPrevChar) {
1695
+ prevChar = '';
1696
+ }
1697
+ keepPrevChar = false;
1698
+
1699
+ // extensions
1700
+ if (this.options.extensions
1701
+ && this.options.extensions.inline
1702
+ && this.options.extensions.inline.some((extTokenizer) => {
1703
+ if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
1704
+ src = src.substring(token.raw.length);
1705
+ tokens.push(token);
1706
+ return true;
1707
+ }
1708
+ return false;
1709
+ })) {
1710
+ continue;
1711
+ }
1712
+
1713
+ // escape
1714
+ if (token = this.tokenizer.escape(src)) {
1715
+ src = src.substring(token.raw.length);
1716
+ tokens.push(token);
1717
+ continue;
1718
+ }
1719
+
1720
+ // tag
1721
+ if (token = this.tokenizer.tag(src)) {
1722
+ src = src.substring(token.raw.length);
1723
+ lastToken = tokens[tokens.length - 1];
1724
+ if (lastToken && token.type === 'text' && lastToken.type === 'text') {
1725
+ lastToken.raw += token.raw;
1726
+ lastToken.text += token.text;
1727
+ } else {
1728
+ tokens.push(token);
1729
+ }
1730
+ continue;
1731
+ }
1732
+
1733
+ // link
1734
+ if (token = this.tokenizer.link(src)) {
1735
+ src = src.substring(token.raw.length);
1736
+ tokens.push(token);
1737
+ continue;
1738
+ }
1739
+
1740
+ // reflink, nolink
1741
+ if (token = this.tokenizer.reflink(src, this.tokens.links)) {
1742
+ src = src.substring(token.raw.length);
1743
+ lastToken = tokens[tokens.length - 1];
1744
+ if (lastToken && token.type === 'text' && lastToken.type === 'text') {
1745
+ lastToken.raw += token.raw;
1746
+ lastToken.text += token.text;
1747
+ } else {
1748
+ tokens.push(token);
1749
+ }
1750
+ continue;
1751
+ }
1752
+
1753
+ // em & strong
1754
+ if (token = this.tokenizer.emStrong(src, maskedSrc, prevChar)) {
1755
+ src = src.substring(token.raw.length);
1756
+ tokens.push(token);
1757
+ continue;
1758
+ }
1759
+
1760
+ // code
1761
+ if (token = this.tokenizer.codespan(src)) {
1762
+ src = src.substring(token.raw.length);
1763
+ tokens.push(token);
1764
+ continue;
1765
+ }
1766
+
1767
+ // br
1768
+ if (token = this.tokenizer.br(src)) {
1769
+ src = src.substring(token.raw.length);
1770
+ tokens.push(token);
1771
+ continue;
1772
+ }
1773
+
1774
+ // del (gfm)
1775
+ if (token = this.tokenizer.del(src)) {
1776
+ src = src.substring(token.raw.length);
1777
+ tokens.push(token);
1778
+ continue;
1779
+ }
1780
+
1781
+ // autolink
1782
+ if (token = this.tokenizer.autolink(src, mangle)) {
1783
+ src = src.substring(token.raw.length);
1784
+ tokens.push(token);
1785
+ continue;
1786
+ }
1787
+
1788
+ // url (gfm)
1789
+ if (!this.state.inLink && (token = this.tokenizer.url(src, mangle))) {
1790
+ src = src.substring(token.raw.length);
1791
+ tokens.push(token);
1792
+ continue;
1793
+ }
1794
+
1795
+ // text
1796
+ // prevent inlineText consuming extensions by clipping 'src' to extension start
1797
+ cutSrc = src;
1798
+ if (this.options.extensions && this.options.extensions.startInline) {
1799
+ let startIndex = Infinity;
1800
+ const tempSrc = src.slice(1);
1801
+ let tempStart;
1802
+ this.options.extensions.startInline.forEach(function(getStartIndex) {
1803
+ tempStart = getStartIndex.call({ lexer: this }, tempSrc);
1804
+ if (typeof tempStart === 'number' && tempStart >= 0) { startIndex = Math.min(startIndex, tempStart); }
1805
+ });
1806
+ if (startIndex < Infinity && startIndex >= 0) {
1807
+ cutSrc = src.substring(0, startIndex + 1);
1808
+ }
1809
+ }
1810
+ if (token = this.tokenizer.inlineText(cutSrc, smartypants)) {
1811
+ src = src.substring(token.raw.length);
1812
+ if (token.raw.slice(-1) !== '_') { // Track prevChar before string of ____ started
1813
+ prevChar = token.raw.slice(-1);
1814
+ }
1815
+ keepPrevChar = true;
1816
+ lastToken = tokens[tokens.length - 1];
1817
+ if (lastToken && lastToken.type === 'text') {
1818
+ lastToken.raw += token.raw;
1819
+ lastToken.text += token.text;
1820
+ } else {
1821
+ tokens.push(token);
1822
+ }
1823
+ continue;
1824
+ }
1825
+
1826
+ if (src) {
1827
+ const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
1828
+ if (this.options.silent) {
1829
+ console.error(errMsg);
1830
+ break;
1831
+ } else {
1832
+ throw new Error(errMsg);
1833
+ }
1834
+ }
1835
+ }
1836
+
1837
+ return tokens;
1838
+ }
1839
+ }
1840
+
1841
+ /**
1842
+ * Renderer
1843
+ */
1844
+ class Renderer {
1845
+ constructor(options) {
1846
+ this.options = options || defaults;
1847
+ }
1848
+
1849
+ code(code, infostring, escaped) {
1850
+ const lang = (infostring || '').match(/\S*/)[0];
1851
+ if (this.options.highlight) {
1852
+ const out = this.options.highlight(code, lang);
1853
+ if (out != null && out !== code) {
1854
+ escaped = true;
1855
+ code = out;
1856
+ }
1857
+ }
1858
+
1859
+ code = code.replace(/\n$/, '') + '\n';
1860
+
1861
+ if (!lang) {
1862
+ return '<pre><code>'
1863
+ + (escaped ? code : escape(code, true))
1864
+ + '</code></pre>\n';
1865
+ }
1866
+
1867
+ return '<pre><code class="'
1868
+ + this.options.langPrefix
1869
+ + escape(lang, true)
1870
+ + '">'
1871
+ + (escaped ? code : escape(code, true))
1872
+ + '</code></pre>\n';
1873
+ }
1874
+
1875
+ blockquote(quote) {
1876
+ return '<blockquote>\n' + quote + '</blockquote>\n';
1877
+ }
1878
+
1879
+ html(html) {
1880
+ return html;
1881
+ }
1882
+
1883
+ heading(text, level, raw, slugger) {
1884
+ 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';
1895
+ }
1896
+ // ignore IDs
1897
+ return '<h' + level + '>' + text + '</h' + level + '>\n';
1898
+ }
1899
+
1900
+ hr() {
1901
+ return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
1902
+ }
1903
+
1904
+ list(body, ordered, start) {
1905
+ const type = ordered ? 'ol' : 'ul',
1906
+ startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
1907
+ return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
1908
+ }
1909
+
1910
+ listitem(text) {
1911
+ return '<li>' + text + '</li>\n';
1912
+ }
1913
+
1914
+ checkbox(checked) {
1915
+ return '<input '
1916
+ + (checked ? 'checked="" ' : '')
1917
+ + 'disabled="" type="checkbox"'
1918
+ + (this.options.xhtml ? ' /' : '')
1919
+ + '> ';
1920
+ }
1921
+
1922
+ paragraph(text) {
1923
+ return '<p>' + text + '</p>\n';
1924
+ }
1925
+
1926
+ table(header, body) {
1927
+ if (body) body = '<tbody>' + body + '</tbody>';
1928
+
1929
+ return '<table>\n'
1930
+ + '<thead>\n'
1931
+ + header
1932
+ + '</thead>\n'
1933
+ + body
1934
+ + '</table>\n';
1935
+ }
1936
+
1937
+ tablerow(content) {
1938
+ return '<tr>\n' + content + '</tr>\n';
1939
+ }
1940
+
1941
+ tablecell(content, flags) {
1942
+ const type = flags.header ? 'th' : 'td';
1943
+ const tag = flags.align
1944
+ ? '<' + type + ' align="' + flags.align + '">'
1945
+ : '<' + type + '>';
1946
+ return tag + content + '</' + type + '>\n';
1947
+ }
1948
+
1949
+ // span level renderer
1950
+ strong(text) {
1951
+ return '<strong>' + text + '</strong>';
1952
+ }
1953
+
1954
+ em(text) {
1955
+ return '<em>' + text + '</em>';
1956
+ }
1957
+
1958
+ codespan(text) {
1959
+ return '<code>' + text + '</code>';
1960
+ }
1961
+
1962
+ br() {
1963
+ return this.options.xhtml ? '<br/>' : '<br>';
1964
+ }
1965
+
1966
+ del(text) {
1967
+ return '<del>' + text + '</del>';
1968
+ }
1969
+
1970
+ link(href, title, text) {
1971
+ href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
1972
+ if (href === null) {
1973
+ return text;
1974
+ }
1975
+ let out = '<a href="' + escape(href) + '"';
1976
+ if (title) {
1977
+ out += ' title="' + title + '"';
1978
+ }
1979
+ out += '>' + text + '</a>';
1980
+ return out;
1981
+ }
1982
+
1983
+ image(href, title, text) {
1984
+ href = cleanUrl(this.options.sanitize, this.options.baseUrl, href);
1985
+ if (href === null) {
1986
+ return text;
1987
+ }
1988
+
1989
+ let out = '<img src="' + href + '" alt="' + text + '"';
1990
+ if (title) {
1991
+ out += ' title="' + title + '"';
1992
+ }
1993
+ out += this.options.xhtml ? '/>' : '>';
1994
+ return out;
1995
+ }
1996
+
1997
+ text(text) {
1998
+ return text;
1999
+ }
2000
+ }
2001
+
2002
+ /**
2003
+ * TextRenderer
2004
+ * returns only the textual part of the token
2005
+ */
2006
+ class TextRenderer {
2007
+ // no need for block level renderers
2008
+ strong(text) {
2009
+ return text;
2010
+ }
2011
+
2012
+ em(text) {
2013
+ return text;
2014
+ }
2015
+
2016
+ codespan(text) {
2017
+ return text;
2018
+ }
2019
+
2020
+ del(text) {
2021
+ return text;
2022
+ }
2023
+
2024
+ html(text) {
2025
+ return text;
2026
+ }
2027
+
2028
+ text(text) {
2029
+ return text;
2030
+ }
2031
+
2032
+ link(href, title, text) {
2033
+ return '' + text;
2034
+ }
2035
+
2036
+ image(href, title, text) {
2037
+ return '' + text;
2038
+ }
2039
+
2040
+ br() {
2041
+ return '';
2042
+ }
2043
+ }
2044
+
2045
+ /**
2046
+ * Slugger generates header id
2047
+ */
2048
+ class Slugger {
2049
+ constructor() {
2050
+ this.seen = {};
2051
+ }
2052
+
2053
+ serialize(value) {
2054
+ return value
2055
+ .toLowerCase()
2056
+ .trim()
2057
+ // remove html tags
2058
+ .replace(/<[!\/a-z].*?>/ig, '')
2059
+ // remove unwanted chars
2060
+ .replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '')
2061
+ .replace(/\s/g, '-');
2062
+ }
2063
+
2064
+ /**
2065
+ * Finds the next safe (unique) slug to use
2066
+ */
2067
+ getNextSafeSlug(originalSlug, isDryRun) {
2068
+ let slug = originalSlug;
2069
+ let occurenceAccumulator = 0;
2070
+ if (this.seen.hasOwnProperty(slug)) {
2071
+ occurenceAccumulator = this.seen[originalSlug];
2072
+ do {
2073
+ occurenceAccumulator++;
2074
+ slug = originalSlug + '-' + occurenceAccumulator;
2075
+ } while (this.seen.hasOwnProperty(slug));
2076
+ }
2077
+ if (!isDryRun) {
2078
+ this.seen[originalSlug] = occurenceAccumulator;
2079
+ this.seen[slug] = 0;
2080
+ }
2081
+ return slug;
2082
+ }
2083
+
2084
+ /**
2085
+ * Convert string to unique id
2086
+ * @param {object} options
2087
+ * @param {boolean} options.dryrun Generates the next unique slug without updating the internal accumulator.
2088
+ */
2089
+ slug(value, options = {}) {
2090
+ const slug = this.serialize(value);
2091
+ return this.getNextSafeSlug(slug, options.dryrun);
2092
+ }
2093
+ }
2094
+
2095
+ /**
2096
+ * Parsing & Compiling
2097
+ */
2098
+ class Parser {
2099
+ constructor(options) {
2100
+ this.options = options || defaults;
2101
+ this.options.renderer = this.options.renderer || new Renderer();
2102
+ this.renderer = this.options.renderer;
2103
+ this.renderer.options = this.options;
2104
+ this.textRenderer = new TextRenderer();
2105
+ this.slugger = new Slugger();
2106
+ }
2107
+
2108
+ /**
2109
+ * Static Parse Method
2110
+ */
2111
+ static parse(tokens, options) {
2112
+ const parser = new Parser(options);
2113
+ return parser.parse(tokens);
2114
+ }
2115
+
2116
+ /**
2117
+ * Static Parse Inline Method
2118
+ */
2119
+ static parseInline(tokens, options) {
2120
+ const parser = new Parser(options);
2121
+ return parser.parseInline(tokens);
2122
+ }
2123
+
2124
+ /**
2125
+ * Parse Loop
2126
+ */
2127
+ parse(tokens, top = true) {
2128
+ let out = '',
2129
+ i,
2130
+ j,
2131
+ k,
2132
+ l2,
2133
+ l3,
2134
+ row,
2135
+ cell,
2136
+ header,
2137
+ body,
2138
+ token,
2139
+ ordered,
2140
+ start,
2141
+ loose,
2142
+ itemBody,
2143
+ item,
2144
+ checked,
2145
+ task,
2146
+ checkbox,
2147
+ ret;
2148
+
2149
+ const l = tokens.length;
2150
+ for (i = 0; i < l; i++) {
2151
+ token = tokens[i];
2152
+
2153
+ // Run any renderer extensions
2154
+ if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[token.type]) {
2155
+ ret = this.options.extensions.renderers[token.type].call({ parser: this }, token);
2156
+ if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'html', 'paragraph', 'text'].includes(token.type)) {
2157
+ out += ret || '';
2158
+ continue;
2159
+ }
2160
+ }
2161
+
2162
+ switch (token.type) {
2163
+ case 'space': {
2164
+ continue;
2165
+ }
2166
+ case 'hr': {
2167
+ out += this.renderer.hr();
2168
+ continue;
2169
+ }
2170
+ case 'heading': {
2171
+ out += this.renderer.heading(
2172
+ this.parseInline(token.tokens),
2173
+ token.depth,
2174
+ unescape(this.parseInline(token.tokens, this.textRenderer)),
2175
+ this.slugger);
2176
+ continue;
2177
+ }
2178
+ case 'code': {
2179
+ out += this.renderer.code(token.text,
2180
+ token.lang,
2181
+ token.escaped);
2182
+ continue;
2183
+ }
2184
+ case 'table': {
2185
+ header = '';
2186
+
2187
+ // header
2188
+ cell = '';
2189
+ l2 = token.header.length;
2190
+ for (j = 0; j < l2; j++) {
2191
+ cell += this.renderer.tablecell(
2192
+ this.parseInline(token.header[j].tokens),
2193
+ { header: true, align: token.align[j] }
2194
+ );
2195
+ }
2196
+ header += this.renderer.tablerow(cell);
2197
+
2198
+ body = '';
2199
+ l2 = token.rows.length;
2200
+ for (j = 0; j < l2; j++) {
2201
+ row = token.rows[j];
2202
+
2203
+ cell = '';
2204
+ l3 = row.length;
2205
+ for (k = 0; k < l3; k++) {
2206
+ cell += this.renderer.tablecell(
2207
+ this.parseInline(row[k].tokens),
2208
+ { header: false, align: token.align[k] }
2209
+ );
2210
+ }
2211
+
2212
+ body += this.renderer.tablerow(cell);
2213
+ }
2214
+ out += this.renderer.table(header, body);
2215
+ continue;
2216
+ }
2217
+ case 'blockquote': {
2218
+ body = this.parse(token.tokens);
2219
+ out += this.renderer.blockquote(body);
2220
+ continue;
2221
+ }
2222
+ case 'list': {
2223
+ ordered = token.ordered;
2224
+ start = token.start;
2225
+ loose = token.loose;
2226
+ l2 = token.items.length;
2227
+
2228
+ body = '';
2229
+ for (j = 0; j < l2; j++) {
2230
+ item = token.items[j];
2231
+ checked = item.checked;
2232
+ task = item.task;
2233
+
2234
+ itemBody = '';
2235
+ if (item.task) {
2236
+ checkbox = this.renderer.checkbox(checked);
2237
+ if (loose) {
2238
+ if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
2239
+ item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
2240
+ if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
2241
+ item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
2242
+ }
2243
+ } else {
2244
+ item.tokens.unshift({
2245
+ type: 'text',
2246
+ text: checkbox
2247
+ });
2248
+ }
2249
+ } else {
2250
+ itemBody += checkbox;
2251
+ }
2252
+ }
2253
+
2254
+ itemBody += this.parse(item.tokens, loose);
2255
+ body += this.renderer.listitem(itemBody, task, checked);
2256
+ }
2257
+
2258
+ out += this.renderer.list(body, ordered, start);
2259
+ continue;
2260
+ }
2261
+ case 'html': {
2262
+ // TODO parse inline content if parameter markdown=1
2263
+ out += this.renderer.html(token.text);
2264
+ continue;
2265
+ }
2266
+ case 'paragraph': {
2267
+ out += this.renderer.paragraph(this.parseInline(token.tokens));
2268
+ continue;
2269
+ }
2270
+ case 'text': {
2271
+ body = token.tokens ? this.parseInline(token.tokens) : token.text;
2272
+ while (i + 1 < l && tokens[i + 1].type === 'text') {
2273
+ token = tokens[++i];
2274
+ body += '\n' + (token.tokens ? this.parseInline(token.tokens) : token.text);
2275
+ }
2276
+ out += top ? this.renderer.paragraph(body) : body;
2277
+ continue;
2278
+ }
2279
+
2280
+ default: {
2281
+ const errMsg = 'Token with "' + token.type + '" type was not found.';
2282
+ if (this.options.silent) {
2283
+ console.error(errMsg);
2284
+ return;
2285
+ } else {
2286
+ throw new Error(errMsg);
2287
+ }
2288
+ }
2289
+ }
2290
+ }
2291
+
2292
+ return out;
2293
+ }
2294
+
2295
+ /**
2296
+ * Parse Inline Tokens
2297
+ */
2298
+ parseInline(tokens, renderer) {
2299
+ renderer = renderer || this.renderer;
2300
+ let out = '',
2301
+ i,
2302
+ token,
2303
+ ret;
2304
+
2305
+ const l = tokens.length;
2306
+ for (i = 0; i < l; i++) {
2307
+ token = tokens[i];
2308
+
2309
+ // Run any renderer extensions
2310
+ if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[token.type]) {
2311
+ ret = this.options.extensions.renderers[token.type].call({ parser: this }, token);
2312
+ if (ret !== false || !['escape', 'html', 'link', 'image', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(token.type)) {
2313
+ out += ret || '';
2314
+ continue;
2315
+ }
2316
+ }
2317
+
2318
+ switch (token.type) {
2319
+ case 'escape': {
2320
+ out += renderer.text(token.text);
2321
+ break;
2322
+ }
2323
+ case 'html': {
2324
+ out += renderer.html(token.text);
2325
+ break;
2326
+ }
2327
+ case 'link': {
2328
+ out += renderer.link(token.href, token.title, this.parseInline(token.tokens, renderer));
2329
+ break;
2330
+ }
2331
+ case 'image': {
2332
+ out += renderer.image(token.href, token.title, token.text);
2333
+ break;
2334
+ }
2335
+ case 'strong': {
2336
+ out += renderer.strong(this.parseInline(token.tokens, renderer));
2337
+ break;
2338
+ }
2339
+ case 'em': {
2340
+ out += renderer.em(this.parseInline(token.tokens, renderer));
2341
+ break;
2342
+ }
2343
+ case 'codespan': {
2344
+ out += renderer.codespan(token.text);
2345
+ break;
2346
+ }
2347
+ case 'br': {
2348
+ out += renderer.br();
2349
+ break;
2350
+ }
2351
+ case 'del': {
2352
+ out += renderer.del(this.parseInline(token.tokens, renderer));
2353
+ break;
2354
+ }
2355
+ case 'text': {
2356
+ out += renderer.text(token.text);
2357
+ break;
2358
+ }
2359
+ default: {
2360
+ const errMsg = 'Token with "' + token.type + '" type was not found.';
2361
+ if (this.options.silent) {
2362
+ console.error(errMsg);
2363
+ return;
2364
+ } else {
2365
+ throw new Error(errMsg);
2366
+ }
2367
+ }
2368
+ }
2369
+ }
2370
+ return out;
2371
+ }
2372
+ }
2373
+
2374
+ /**
2375
+ * Marked
2376
+ */
2377
+ function marked(src, opt, callback) {
2378
+ // throw error in case of non string input
2379
+ if (typeof src === 'undefined' || src === null) {
2380
+ throw new Error('marked(): input parameter is undefined or null');
2381
+ }
2382
+ if (typeof src !== 'string') {
2383
+ throw new Error('marked(): input parameter is of type '
2384
+ + Object.prototype.toString.call(src) + ', string expected');
2385
+ }
2386
+
2387
+ if (typeof opt === 'function') {
2388
+ callback = opt;
2389
+ opt = null;
2390
+ }
2391
+
2392
+ opt = merge({}, marked.defaults, opt || {});
2393
+ checkSanitizeDeprecation(opt);
2394
+
2395
+ if (callback) {
2396
+ const highlight = opt.highlight;
2397
+ let tokens;
2398
+
2399
+ try {
2400
+ tokens = Lexer.lex(src, opt);
2401
+ } catch (e) {
2402
+ return callback(e);
2403
+ }
2404
+
2405
+ const done = function(err) {
2406
+ let out;
2407
+
2408
+ if (!err) {
2409
+ try {
2410
+ if (opt.walkTokens) {
2411
+ marked.walkTokens(tokens, opt.walkTokens);
2412
+ }
2413
+ out = Parser.parse(tokens, opt);
2414
+ } catch (e) {
2415
+ err = e;
2416
+ }
2417
+ }
2418
+
2419
+ opt.highlight = highlight;
2420
+
2421
+ return err
2422
+ ? callback(err)
2423
+ : callback(null, out);
2424
+ };
2425
+
2426
+ if (!highlight || highlight.length < 3) {
2427
+ return done();
2428
+ }
2429
+
2430
+ delete opt.highlight;
2431
+
2432
+ if (!tokens.length) return done();
2433
+
2434
+ let pending = 0;
2435
+ marked.walkTokens(tokens, function(token) {
2436
+ if (token.type === 'code') {
2437
+ pending++;
2438
+ setTimeout(() => {
2439
+ highlight(token.text, token.lang, function(err, code) {
2440
+ if (err) {
2441
+ return done(err);
2442
+ }
2443
+ if (code != null && code !== token.text) {
2444
+ token.text = code;
2445
+ token.escaped = true;
2446
+ }
2447
+
2448
+ pending--;
2449
+ if (pending === 0) {
2450
+ done();
2451
+ }
2452
+ });
2453
+ }, 0);
2454
+ }
2455
+ });
2456
+
2457
+ if (pending === 0) {
2458
+ done();
2459
+ }
2460
+
2461
+ return;
2462
+ }
2463
+
2464
+ try {
2465
+ const tokens = Lexer.lex(src, opt);
2466
+ if (opt.walkTokens) {
2467
+ marked.walkTokens(tokens, opt.walkTokens);
2468
+ }
2469
+ return Parser.parse(tokens, opt);
2470
+ } catch (e) {
2471
+ e.message += '\nPlease report this to https://github.com/markedjs/marked.';
2472
+ if (opt.silent) {
2473
+ return '<p>An error occurred:</p><pre>'
2474
+ + escape(e.message + '', true)
2475
+ + '</pre>';
2476
+ }
2477
+ throw e;
2478
+ }
2479
+ }
2480
+
2481
+ /**
2482
+ * Options
2483
+ */
2484
+
2485
+ marked.options =
2486
+ marked.setOptions = function(opt) {
2487
+ merge(marked.defaults, opt);
2488
+ changeDefaults(marked.defaults);
2489
+ return marked;
2490
+ };
2491
+
2492
+ marked.getDefaults = getDefaults;
2493
+
2494
+ marked.defaults = defaults;
2495
+
2496
+ /**
2497
+ * Use Extension
2498
+ */
2499
+
2500
+ marked.use = function(...args) {
2501
+ const opts = merge({}, ...args);
2502
+ const extensions = marked.defaults.extensions || { renderers: {}, childTokens: {} };
2503
+ let hasExtensions;
2504
+
2505
+ args.forEach((pack) => {
2506
+ // ==-- Parse "addon" extensions --== //
2507
+ if (pack.extensions) {
2508
+ hasExtensions = true;
2509
+ pack.extensions.forEach((ext) => {
2510
+ if (!ext.name) {
2511
+ throw new Error('extension name required');
2512
+ }
2513
+ if (ext.renderer) { // Renderer extensions
2514
+ const prevRenderer = extensions.renderers ? extensions.renderers[ext.name] : null;
2515
+ if (prevRenderer) {
2516
+ // Replace extension with func to run new extension but fall back if false
2517
+ extensions.renderers[ext.name] = function(...args) {
2518
+ let ret = ext.renderer.apply(this, args);
2519
+ if (ret === false) {
2520
+ ret = prevRenderer.apply(this, args);
2521
+ }
2522
+ return ret;
2523
+ };
2524
+ } else {
2525
+ extensions.renderers[ext.name] = ext.renderer;
2526
+ }
2527
+ }
2528
+ if (ext.tokenizer) { // Tokenizer Extensions
2529
+ if (!ext.level || (ext.level !== 'block' && ext.level !== 'inline')) {
2530
+ throw new Error("extension level must be 'block' or 'inline'");
2531
+ }
2532
+ if (extensions[ext.level]) {
2533
+ extensions[ext.level].unshift(ext.tokenizer);
2534
+ } else {
2535
+ extensions[ext.level] = [ext.tokenizer];
2536
+ }
2537
+ if (ext.start) { // Function to check for start of token
2538
+ if (ext.level === 'block') {
2539
+ if (extensions.startBlock) {
2540
+ extensions.startBlock.push(ext.start);
2541
+ } else {
2542
+ extensions.startBlock = [ext.start];
2543
+ }
2544
+ } else if (ext.level === 'inline') {
2545
+ if (extensions.startInline) {
2546
+ extensions.startInline.push(ext.start);
2547
+ } else {
2548
+ extensions.startInline = [ext.start];
2549
+ }
2550
+ }
2551
+ }
2552
+ }
2553
+ if (ext.childTokens) { // Child tokens to be visited by walkTokens
2554
+ extensions.childTokens[ext.name] = ext.childTokens;
2555
+ }
2556
+ });
2557
+ }
2558
+
2559
+ // ==-- Parse "overwrite" extensions --== //
2560
+ if (pack.renderer) {
2561
+ const renderer = marked.defaults.renderer || new Renderer();
2562
+ for (const prop in pack.renderer) {
2563
+ const prevRenderer = renderer[prop];
2564
+ // Replace renderer with func to run extension, but fall back if false
2565
+ renderer[prop] = (...args) => {
2566
+ let ret = pack.renderer[prop].apply(renderer, args);
2567
+ if (ret === false) {
2568
+ ret = prevRenderer.apply(renderer, args);
2569
+ }
2570
+ return ret;
2571
+ };
2572
+ }
2573
+ opts.renderer = renderer;
2574
+ }
2575
+ if (pack.tokenizer) {
2576
+ const tokenizer = marked.defaults.tokenizer || new Tokenizer();
2577
+ for (const prop in pack.tokenizer) {
2578
+ const prevTokenizer = tokenizer[prop];
2579
+ // Replace tokenizer with func to run extension, but fall back if false
2580
+ tokenizer[prop] = (...args) => {
2581
+ let ret = pack.tokenizer[prop].apply(tokenizer, args);
2582
+ if (ret === false) {
2583
+ ret = prevTokenizer.apply(tokenizer, args);
2584
+ }
2585
+ return ret;
2586
+ };
2587
+ }
2588
+ opts.tokenizer = tokenizer;
2589
+ }
2590
+
2591
+ // ==-- Parse WalkTokens extensions --== //
2592
+ if (pack.walkTokens) {
2593
+ const walkTokens = marked.defaults.walkTokens;
2594
+ opts.walkTokens = function(token) {
2595
+ pack.walkTokens.call(this, token);
2596
+ if (walkTokens) {
2597
+ walkTokens.call(this, token);
2598
+ }
2599
+ };
2600
+ }
2601
+
2602
+ if (hasExtensions) {
2603
+ opts.extensions = extensions;
2604
+ }
2605
+
2606
+ marked.setOptions(opts);
2607
+ });
2608
+ };
2609
+
2610
+ /**
2611
+ * Run callback for every token
2612
+ */
2613
+
2614
+ marked.walkTokens = function(tokens, callback) {
2615
+ for (const token of tokens) {
2616
+ callback.call(marked, token);
2617
+ switch (token.type) {
2618
+ case 'table': {
2619
+ for (const cell of token.header) {
2620
+ marked.walkTokens(cell.tokens, callback);
2621
+ }
2622
+ for (const row of token.rows) {
2623
+ for (const cell of row) {
2624
+ marked.walkTokens(cell.tokens, callback);
2625
+ }
2626
+ }
2627
+ break;
2628
+ }
2629
+ case 'list': {
2630
+ marked.walkTokens(token.items, callback);
2631
+ break;
2632
+ }
2633
+ default: {
2634
+ if (marked.defaults.extensions && marked.defaults.extensions.childTokens && marked.defaults.extensions.childTokens[token.type]) { // Walk any extensions
2635
+ marked.defaults.extensions.childTokens[token.type].forEach(function(childTokens) {
2636
+ marked.walkTokens(token[childTokens], callback);
2637
+ });
2638
+ } else if (token.tokens) {
2639
+ marked.walkTokens(token.tokens, callback);
2640
+ }
2641
+ }
2642
+ }
2643
+ }
2644
+ };
2645
+
2646
+ /**
2647
+ * Parse Inline
2648
+ */
2649
+ marked.parseInline = function(src, opt) {
2650
+ // throw error in case of non string input
2651
+ if (typeof src === 'undefined' || src === null) {
2652
+ throw new Error('marked.parseInline(): input parameter is undefined or null');
2653
+ }
2654
+ if (typeof src !== 'string') {
2655
+ throw new Error('marked.parseInline(): input parameter is of type '
2656
+ + Object.prototype.toString.call(src) + ', string expected');
2657
+ }
2658
+
2659
+ opt = merge({}, marked.defaults, opt || {});
2660
+ checkSanitizeDeprecation(opt);
2661
+
2662
+ try {
2663
+ const tokens = Lexer.lexInline(src, opt);
2664
+ if (opt.walkTokens) {
2665
+ marked.walkTokens(tokens, opt.walkTokens);
2666
+ }
2667
+ return Parser.parseInline(tokens, opt);
2668
+ } catch (e) {
2669
+ e.message += '\nPlease report this to https://github.com/markedjs/marked.';
2670
+ if (opt.silent) {
2671
+ return '<p>An error occurred:</p><pre>'
2672
+ + escape(e.message + '', true)
2673
+ + '</pre>';
2674
+ }
2675
+ throw e;
2676
+ }
2677
+ };
2678
+
2679
+ /**
2680
+ * Expose
2681
+ */
2682
+ marked.Parser = Parser;
2683
+ marked.parser = Parser.parse;
2684
+ marked.Renderer = Renderer;
2685
+ marked.TextRenderer = TextRenderer;
2686
+ marked.Lexer = Lexer;
2687
+ marked.lexer = Lexer.lex;
2688
+ marked.Tokenizer = Tokenizer;
2689
+ marked.Slugger = Slugger;
2690
+ marked.parse = marked;
2691
+
2692
+ const options = marked.options;
2693
+ const setOptions = marked.setOptions;
2694
+ const use = marked.use;
2695
+ const walkTokens = marked.walkTokens;
2696
+ const parseInline = marked.parseInline;
2697
+ const parse = marked;
2698
+ const parser = Parser.parse;
2699
+ const lexer = Lexer.lex;
2700
+
2701
+
2702
+
2703
+
2704
+ /***/ })
2705
+
2706
+ }]);