plugin-build-guide-block 1.0.10 → 1.0.12

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