plugin-build-guide-block 1.0.11 → 1.1.2

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