marked-cs 0.0.1-security → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of marked-cs might be problematic. Click here for more details.

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