@contentgrowth/content-widget 1.1.0 → 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.
@@ -0,0 +1,3805 @@
1
+ /**
2
+ * Content Growth Widget - Standalone Bundle
3
+ * Version: 1.1.2
4
+ * https://www.content-growth.com
5
+ */
6
+ (function(window) {
7
+ 'use strict';
8
+
9
+ // ===== Marked.js Library =====
10
+ /**
11
+ * marked v11.2.0 - a markdown parser
12
+ * Copyright (c) 2011-2024, Christopher Jeffrey. (MIT Licensed)
13
+ * https://github.com/markedjs/marked
14
+ */
15
+
16
+ /**
17
+ * DO NOT EDIT THIS FILE
18
+ * The code in this file is generated from files in ./src/
19
+ */
20
+
21
+ (function (global, factory) {
22
+ // Marked.js UMD Bundle
23
+
24
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
25
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
26
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.marked = {}));
27
+ })(this, (function (exports) { 'use strict';
28
+
29
+ /**
30
+ * Gets the original marked default options.
31
+ */
32
+ function _getDefaults() {
33
+ return {
34
+ async: false,
35
+ breaks: false,
36
+ extensions: null,
37
+ gfm: true,
38
+ hooks: null,
39
+ pedantic: false,
40
+ renderer: null,
41
+ silent: false,
42
+ tokenizer: null,
43
+ walkTokens: null
44
+ };
45
+ }
46
+ exports.defaults = _getDefaults();
47
+ function changeDefaults(newDefaults) {
48
+ exports.defaults = newDefaults;
49
+ }
50
+
51
+ /**
52
+ * Helpers
53
+ */
54
+ const escapeTest = /[&<>"']/;
55
+ const escapeReplace = new RegExp(escapeTest.source, 'g');
56
+ const escapeTestNoEncode = /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/;
57
+ const escapeReplaceNoEncode = new RegExp(escapeTestNoEncode.source, 'g');
58
+ const escapeReplacements = {
59
+ '&': '&amp;',
60
+ '<': '&lt;',
61
+ '>': '&gt;',
62
+ '"': '&quot;',
63
+ "'": '&#39;'
64
+ };
65
+ const getEscapeReplacement = (ch) => escapeReplacements[ch];
66
+ function escape$1(html, encode) {
67
+ if (encode) {
68
+ if (escapeTest.test(html)) {
69
+ return html.replace(escapeReplace, getEscapeReplacement);
70
+ }
71
+ }
72
+ else {
73
+ if (escapeTestNoEncode.test(html)) {
74
+ return html.replace(escapeReplaceNoEncode, getEscapeReplacement);
75
+ }
76
+ }
77
+ return html;
78
+ }
79
+ const unescapeTest = /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig;
80
+ function unescape(html) {
81
+ // explicitly match decimal, hex, and named HTML entities
82
+ return html.replace(unescapeTest, (_, n) => {
83
+ n = n.toLowerCase();
84
+ if (n === 'colon')
85
+ return ':';
86
+ if (n.charAt(0) === '#') {
87
+ return n.charAt(1) === 'x'
88
+ ? String.fromCharCode(parseInt(n.substring(2), 16))
89
+ : String.fromCharCode(+n.substring(1));
90
+ }
91
+ return '';
92
+ });
93
+ }
94
+ const caret = /(^|[^\[])\^/g;
95
+ function edit(regex, opt) {
96
+ let source = typeof regex === 'string' ? regex : regex.source;
97
+ opt = opt || '';
98
+ const obj = {
99
+ replace: (name, val) => {
100
+ let valSource = typeof val === 'string' ? val : val.source;
101
+ valSource = valSource.replace(caret, '$1');
102
+ source = source.replace(name, valSource);
103
+ return obj;
104
+ },
105
+ getRegex: () => {
106
+ return new RegExp(source, opt);
107
+ }
108
+ };
109
+ return obj;
110
+ }
111
+ function cleanUrl(href) {
112
+ try {
113
+ href = encodeURI(href).replace(/%25/g, '%');
114
+ }
115
+ catch (e) {
116
+ return null;
117
+ }
118
+ return href;
119
+ }
120
+ const noopTest = { exec: () => null };
121
+ function splitCells(tableRow, count) {
122
+ // ensure that every cell-delimiting pipe has a space
123
+ // before it to distinguish it from an escaped pipe
124
+ const row = tableRow.replace(/\|/g, (match, offset, str) => {
125
+ let escaped = false;
126
+ let curr = offset;
127
+ while (--curr >= 0 && str[curr] === '\\')
128
+ escaped = !escaped;
129
+ if (escaped) {
130
+ // odd number of slashes means | is escaped
131
+ // so we leave it alone
132
+ return '|';
133
+ }
134
+ else {
135
+ // add space before unescaped |
136
+ return ' |';
137
+ }
138
+ }), cells = row.split(/ \|/);
139
+ let i = 0;
140
+ // First/last cell in a row cannot be empty if it has no leading/trailing pipe
141
+ if (!cells[0].trim()) {
142
+ cells.shift();
143
+ }
144
+ if (cells.length > 0 && !cells[cells.length - 1].trim()) {
145
+ cells.pop();
146
+ }
147
+ if (count) {
148
+ if (cells.length > count) {
149
+ cells.splice(count);
150
+ }
151
+ else {
152
+ while (cells.length < count)
153
+ cells.push('');
154
+ }
155
+ }
156
+ for (; i < cells.length; i++) {
157
+ // leading or trailing whitespace is ignored per the gfm spec
158
+ cells[i] = cells[i].trim().replace(/\\\|/g, '|');
159
+ }
160
+ return cells;
161
+ }
162
+ /**
163
+ * Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
164
+ * /c*$/ is vulnerable to REDOS.
165
+ *
166
+ * @param str
167
+ * @param c
168
+ * @param invert Remove suffix of non-c chars instead. Default falsey.
169
+ */
170
+ function rtrim(str, c, invert) {
171
+ const l = str.length;
172
+ if (l === 0) {
173
+ return '';
174
+ }
175
+ // Length of suffix matching the invert condition.
176
+ let suffLen = 0;
177
+ // Step left until we fail to match the invert condition.
178
+ while (suffLen < l) {
179
+ const currChar = str.charAt(l - suffLen - 1);
180
+ if (currChar === c && !invert) {
181
+ suffLen++;
182
+ }
183
+ else if (currChar !== c && invert) {
184
+ suffLen++;
185
+ }
186
+ else {
187
+ break;
188
+ }
189
+ }
190
+ return str.slice(0, l - suffLen);
191
+ }
192
+ function findClosingBracket(str, b) {
193
+ if (str.indexOf(b[1]) === -1) {
194
+ return -1;
195
+ }
196
+ let level = 0;
197
+ for (let i = 0; i < str.length; i++) {
198
+ if (str[i] === '\\') {
199
+ i++;
200
+ }
201
+ else if (str[i] === b[0]) {
202
+ level++;
203
+ }
204
+ else if (str[i] === b[1]) {
205
+ level--;
206
+ if (level < 0) {
207
+ return i;
208
+ }
209
+ }
210
+ }
211
+ return -1;
212
+ }
213
+
214
+ function outputLink(cap, link, raw, lexer) {
215
+ const href = link.href;
216
+ const title = link.title ? escape$1(link.title) : null;
217
+ const text = cap[1].replace(/\\([\[\]])/g, '$1');
218
+ if (cap[0].charAt(0) !== '!') {
219
+ lexer.state.inLink = true;
220
+ const token = {
221
+ type: 'link',
222
+ raw,
223
+ href,
224
+ title,
225
+ text,
226
+ tokens: lexer.inlineTokens(text)
227
+ };
228
+ lexer.state.inLink = false;
229
+ return token;
230
+ }
231
+ return {
232
+ type: 'image',
233
+ raw,
234
+ href,
235
+ title,
236
+ text: escape$1(text)
237
+ };
238
+ }
239
+ function indentCodeCompensation(raw, text) {
240
+ const matchIndentToCode = raw.match(/^(\s+)(?:```)/);
241
+ if (matchIndentToCode === null) {
242
+ return text;
243
+ }
244
+ const indentToCode = matchIndentToCode[1];
245
+ return text
246
+ .split('\n')
247
+ .map(node => {
248
+ const matchIndentInNode = node.match(/^\s+/);
249
+ if (matchIndentInNode === null) {
250
+ return node;
251
+ }
252
+ const [indentInNode] = matchIndentInNode;
253
+ if (indentInNode.length >= indentToCode.length) {
254
+ return node.slice(indentToCode.length);
255
+ }
256
+ return node;
257
+ })
258
+ .join('\n');
259
+ }
260
+ /**
261
+ * Tokenizer
262
+ */
263
+ class _Tokenizer {
264
+ options;
265
+ rules; // set by the lexer
266
+ lexer; // set by the lexer
267
+ constructor(options) {
268
+ this.options = options || exports.defaults;
269
+ }
270
+ space(src) {
271
+ const cap = this.rules.block.newline.exec(src);
272
+ if (cap && cap[0].length > 0) {
273
+ return {
274
+ type: 'space',
275
+ raw: cap[0]
276
+ };
277
+ }
278
+ }
279
+ code(src) {
280
+ const cap = this.rules.block.code.exec(src);
281
+ if (cap) {
282
+ const text = cap[0].replace(/^ {1,4}/gm, '');
283
+ return {
284
+ type: 'code',
285
+ raw: cap[0],
286
+ codeBlockStyle: 'indented',
287
+ text: !this.options.pedantic
288
+ ? rtrim(text, '\n')
289
+ : text
290
+ };
291
+ }
292
+ }
293
+ fences(src) {
294
+ const cap = this.rules.block.fences.exec(src);
295
+ if (cap) {
296
+ const raw = cap[0];
297
+ const text = indentCodeCompensation(raw, cap[3] || '');
298
+ return {
299
+ type: 'code',
300
+ raw,
301
+ lang: cap[2] ? cap[2].trim().replace(this.rules.inline.anyPunctuation, '$1') : cap[2],
302
+ text
303
+ };
304
+ }
305
+ }
306
+ heading(src) {
307
+ const cap = this.rules.block.heading.exec(src);
308
+ if (cap) {
309
+ let text = cap[2].trim();
310
+ // remove trailing #s
311
+ if (/#$/.test(text)) {
312
+ const trimmed = rtrim(text, '#');
313
+ if (this.options.pedantic) {
314
+ text = trimmed.trim();
315
+ }
316
+ else if (!trimmed || / $/.test(trimmed)) {
317
+ // CommonMark requires space before trailing #s
318
+ text = trimmed.trim();
319
+ }
320
+ }
321
+ return {
322
+ type: 'heading',
323
+ raw: cap[0],
324
+ depth: cap[1].length,
325
+ text,
326
+ tokens: this.lexer.inline(text)
327
+ };
328
+ }
329
+ }
330
+ hr(src) {
331
+ const cap = this.rules.block.hr.exec(src);
332
+ if (cap) {
333
+ return {
334
+ type: 'hr',
335
+ raw: cap[0]
336
+ };
337
+ }
338
+ }
339
+ blockquote(src) {
340
+ const cap = this.rules.block.blockquote.exec(src);
341
+ if (cap) {
342
+ const text = rtrim(cap[0].replace(/^ *>[ \t]?/gm, ''), '\n');
343
+ const top = this.lexer.state.top;
344
+ this.lexer.state.top = true;
345
+ const tokens = this.lexer.blockTokens(text);
346
+ this.lexer.state.top = top;
347
+ return {
348
+ type: 'blockquote',
349
+ raw: cap[0],
350
+ tokens,
351
+ text
352
+ };
353
+ }
354
+ }
355
+ list(src) {
356
+ let cap = this.rules.block.list.exec(src);
357
+ if (cap) {
358
+ let bull = cap[1].trim();
359
+ const isordered = bull.length > 1;
360
+ const list = {
361
+ type: 'list',
362
+ raw: '',
363
+ ordered: isordered,
364
+ start: isordered ? +bull.slice(0, -1) : '',
365
+ loose: false,
366
+ items: []
367
+ };
368
+ bull = isordered ? `\\d{1,9}\\${bull.slice(-1)}` : `\\${bull}`;
369
+ if (this.options.pedantic) {
370
+ bull = isordered ? bull : '[*+-]';
371
+ }
372
+ // Get next list item
373
+ const itemRegex = new RegExp(`^( {0,3}${bull})((?:[\t ][^\\n]*)?(?:\\n|$))`);
374
+ let raw = '';
375
+ let itemContents = '';
376
+ let endsWithBlankLine = false;
377
+ // Check if current bullet point can start a new List Item
378
+ while (src) {
379
+ let endEarly = false;
380
+ if (!(cap = itemRegex.exec(src))) {
381
+ break;
382
+ }
383
+ if (this.rules.block.hr.test(src)) { // End list if bullet was actually HR (possibly move into itemRegex?)
384
+ break;
385
+ }
386
+ raw = cap[0];
387
+ src = src.substring(raw.length);
388
+ let line = cap[2].split('\n', 1)[0].replace(/^\t+/, (t) => ' '.repeat(3 * t.length));
389
+ let nextLine = src.split('\n', 1)[0];
390
+ let indent = 0;
391
+ if (this.options.pedantic) {
392
+ indent = 2;
393
+ itemContents = line.trimStart();
394
+ }
395
+ else {
396
+ indent = cap[2].search(/[^ ]/); // Find first non-space char
397
+ indent = indent > 4 ? 1 : indent; // Treat indented code blocks (> 4 spaces) as having only 1 indent
398
+ itemContents = line.slice(indent);
399
+ indent += cap[1].length;
400
+ }
401
+ let blankLine = false;
402
+ if (!line && /^ *$/.test(nextLine)) { // Items begin with at most one blank line
403
+ raw += nextLine + '\n';
404
+ src = src.substring(nextLine.length + 1);
405
+ endEarly = true;
406
+ }
407
+ if (!endEarly) {
408
+ const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])((?:[ \t][^\\n]*)?(?:\\n|$))`);
409
+ const hrRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`);
410
+ const fencesBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:\`\`\`|~~~)`);
411
+ const headingBeginRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}#`);
412
+ // Check if following lines should be included in List Item
413
+ while (src) {
414
+ const rawLine = src.split('\n', 1)[0];
415
+ nextLine = rawLine;
416
+ // Re-align to follow commonmark nesting rules
417
+ if (this.options.pedantic) {
418
+ nextLine = nextLine.replace(/^ {1,4}(?=( {4})*[^ ])/g, ' ');
419
+ }
420
+ // End list item if found code fences
421
+ if (fencesBeginRegex.test(nextLine)) {
422
+ break;
423
+ }
424
+ // End list item if found start of new heading
425
+ if (headingBeginRegex.test(nextLine)) {
426
+ break;
427
+ }
428
+ // End list item if found start of new bullet
429
+ if (nextBulletRegex.test(nextLine)) {
430
+ break;
431
+ }
432
+ // Horizontal rule found
433
+ if (hrRegex.test(src)) {
434
+ break;
435
+ }
436
+ if (nextLine.search(/[^ ]/) >= indent || !nextLine.trim()) { // Dedent if possible
437
+ itemContents += '\n' + nextLine.slice(indent);
438
+ }
439
+ else {
440
+ // not enough indentation
441
+ if (blankLine) {
442
+ break;
443
+ }
444
+ // paragraph continuation unless last line was a different block level element
445
+ if (line.search(/[^ ]/) >= 4) { // indented code block
446
+ break;
447
+ }
448
+ if (fencesBeginRegex.test(line)) {
449
+ break;
450
+ }
451
+ if (headingBeginRegex.test(line)) {
452
+ break;
453
+ }
454
+ if (hrRegex.test(line)) {
455
+ break;
456
+ }
457
+ itemContents += '\n' + nextLine;
458
+ }
459
+ if (!blankLine && !nextLine.trim()) { // Check if current line is blank
460
+ blankLine = true;
461
+ }
462
+ raw += rawLine + '\n';
463
+ src = src.substring(rawLine.length + 1);
464
+ line = nextLine.slice(indent);
465
+ }
466
+ }
467
+ if (!list.loose) {
468
+ // If the previous item ended with a blank line, the list is loose
469
+ if (endsWithBlankLine) {
470
+ list.loose = true;
471
+ }
472
+ else if (/\n *\n *$/.test(raw)) {
473
+ endsWithBlankLine = true;
474
+ }
475
+ }
476
+ let istask = null;
477
+ let ischecked;
478
+ // Check for task list items
479
+ if (this.options.gfm) {
480
+ istask = /^\[[ xX]\] /.exec(itemContents);
481
+ if (istask) {
482
+ ischecked = istask[0] !== '[ ] ';
483
+ itemContents = itemContents.replace(/^\[[ xX]\] +/, '');
484
+ }
485
+ }
486
+ list.items.push({
487
+ type: 'list_item',
488
+ raw,
489
+ task: !!istask,
490
+ checked: ischecked,
491
+ loose: false,
492
+ text: itemContents,
493
+ tokens: []
494
+ });
495
+ list.raw += raw;
496
+ }
497
+ // Do not consume newlines at end of final item. Alternatively, make itemRegex *start* with any newlines to simplify/speed up endsWithBlankLine logic
498
+ list.items[list.items.length - 1].raw = raw.trimEnd();
499
+ (list.items[list.items.length - 1]).text = itemContents.trimEnd();
500
+ list.raw = list.raw.trimEnd();
501
+ // Item child tokens handled here at end because we needed to have the final item to trim it first
502
+ for (let i = 0; i < list.items.length; i++) {
503
+ this.lexer.state.top = false;
504
+ list.items[i].tokens = this.lexer.blockTokens(list.items[i].text, []);
505
+ if (!list.loose) {
506
+ // Check if list should be loose
507
+ const spacers = list.items[i].tokens.filter(t => t.type === 'space');
508
+ const hasMultipleLineBreaks = spacers.length > 0 && spacers.some(t => /\n.*\n/.test(t.raw));
509
+ list.loose = hasMultipleLineBreaks;
510
+ }
511
+ }
512
+ // Set all items to loose if list is loose
513
+ if (list.loose) {
514
+ for (let i = 0; i < list.items.length; i++) {
515
+ list.items[i].loose = true;
516
+ }
517
+ }
518
+ return list;
519
+ }
520
+ }
521
+ html(src) {
522
+ const cap = this.rules.block.html.exec(src);
523
+ if (cap) {
524
+ const token = {
525
+ type: 'html',
526
+ block: true,
527
+ raw: cap[0],
528
+ pre: cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style',
529
+ text: cap[0]
530
+ };
531
+ return token;
532
+ }
533
+ }
534
+ def(src) {
535
+ const cap = this.rules.block.def.exec(src);
536
+ if (cap) {
537
+ const tag = cap[1].toLowerCase().replace(/\s+/g, ' ');
538
+ const href = cap[2] ? cap[2].replace(/^<(.*)>$/, '$1').replace(this.rules.inline.anyPunctuation, '$1') : '';
539
+ const title = cap[3] ? cap[3].substring(1, cap[3].length - 1).replace(this.rules.inline.anyPunctuation, '$1') : cap[3];
540
+ return {
541
+ type: 'def',
542
+ tag,
543
+ raw: cap[0],
544
+ href,
545
+ title
546
+ };
547
+ }
548
+ }
549
+ table(src) {
550
+ const cap = this.rules.block.table.exec(src);
551
+ if (!cap) {
552
+ return;
553
+ }
554
+ if (!/[:|]/.test(cap[2])) {
555
+ // delimiter row must have a pipe (|) or colon (:) otherwise it is a setext heading
556
+ return;
557
+ }
558
+ const headers = splitCells(cap[1]);
559
+ const aligns = cap[2].replace(/^\||\| *$/g, '').split('|');
560
+ const rows = cap[3] && cap[3].trim() ? cap[3].replace(/\n[ \t]*$/, '').split('\n') : [];
561
+ const item = {
562
+ type: 'table',
563
+ raw: cap[0],
564
+ header: [],
565
+ align: [],
566
+ rows: []
567
+ };
568
+ if (headers.length !== aligns.length) {
569
+ // header and align columns must be equal, rows can be different.
570
+ return;
571
+ }
572
+ for (const align of aligns) {
573
+ if (/^ *-+: *$/.test(align)) {
574
+ item.align.push('right');
575
+ }
576
+ else if (/^ *:-+: *$/.test(align)) {
577
+ item.align.push('center');
578
+ }
579
+ else if (/^ *:-+ *$/.test(align)) {
580
+ item.align.push('left');
581
+ }
582
+ else {
583
+ item.align.push(null);
584
+ }
585
+ }
586
+ for (const header of headers) {
587
+ item.header.push({
588
+ text: header,
589
+ tokens: this.lexer.inline(header)
590
+ });
591
+ }
592
+ for (const row of rows) {
593
+ item.rows.push(splitCells(row, item.header.length).map(cell => {
594
+ return {
595
+ text: cell,
596
+ tokens: this.lexer.inline(cell)
597
+ };
598
+ }));
599
+ }
600
+ return item;
601
+ }
602
+ lheading(src) {
603
+ const cap = this.rules.block.lheading.exec(src);
604
+ if (cap) {
605
+ return {
606
+ type: 'heading',
607
+ raw: cap[0],
608
+ depth: cap[2].charAt(0) === '=' ? 1 : 2,
609
+ text: cap[1],
610
+ tokens: this.lexer.inline(cap[1])
611
+ };
612
+ }
613
+ }
614
+ paragraph(src) {
615
+ const cap = this.rules.block.paragraph.exec(src);
616
+ if (cap) {
617
+ const text = cap[1].charAt(cap[1].length - 1) === '\n'
618
+ ? cap[1].slice(0, -1)
619
+ : cap[1];
620
+ return {
621
+ type: 'paragraph',
622
+ raw: cap[0],
623
+ text,
624
+ tokens: this.lexer.inline(text)
625
+ };
626
+ }
627
+ }
628
+ text(src) {
629
+ const cap = this.rules.block.text.exec(src);
630
+ if (cap) {
631
+ return {
632
+ type: 'text',
633
+ raw: cap[0],
634
+ text: cap[0],
635
+ tokens: this.lexer.inline(cap[0])
636
+ };
637
+ }
638
+ }
639
+ escape(src) {
640
+ const cap = this.rules.inline.escape.exec(src);
641
+ if (cap) {
642
+ return {
643
+ type: 'escape',
644
+ raw: cap[0],
645
+ text: escape$1(cap[1])
646
+ };
647
+ }
648
+ }
649
+ tag(src) {
650
+ const cap = this.rules.inline.tag.exec(src);
651
+ if (cap) {
652
+ if (!this.lexer.state.inLink && /^<a /i.test(cap[0])) {
653
+ this.lexer.state.inLink = true;
654
+ }
655
+ else if (this.lexer.state.inLink && /^<\/a>/i.test(cap[0])) {
656
+ this.lexer.state.inLink = false;
657
+ }
658
+ if (!this.lexer.state.inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
659
+ this.lexer.state.inRawBlock = true;
660
+ }
661
+ else if (this.lexer.state.inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(cap[0])) {
662
+ this.lexer.state.inRawBlock = false;
663
+ }
664
+ return {
665
+ type: 'html',
666
+ raw: cap[0],
667
+ inLink: this.lexer.state.inLink,
668
+ inRawBlock: this.lexer.state.inRawBlock,
669
+ block: false,
670
+ text: cap[0]
671
+ };
672
+ }
673
+ }
674
+ link(src) {
675
+ const cap = this.rules.inline.link.exec(src);
676
+ if (cap) {
677
+ const trimmedUrl = cap[2].trim();
678
+ if (!this.options.pedantic && /^</.test(trimmedUrl)) {
679
+ // commonmark requires matching angle brackets
680
+ if (!(/>$/.test(trimmedUrl))) {
681
+ return;
682
+ }
683
+ // ending angle bracket cannot be escaped
684
+ const rtrimSlash = rtrim(trimmedUrl.slice(0, -1), '\\');
685
+ if ((trimmedUrl.length - rtrimSlash.length) % 2 === 0) {
686
+ return;
687
+ }
688
+ }
689
+ else {
690
+ // find closing parenthesis
691
+ const lastParenIndex = findClosingBracket(cap[2], '()');
692
+ if (lastParenIndex > -1) {
693
+ const start = cap[0].indexOf('!') === 0 ? 5 : 4;
694
+ const linkLen = start + cap[1].length + lastParenIndex;
695
+ cap[2] = cap[2].substring(0, lastParenIndex);
696
+ cap[0] = cap[0].substring(0, linkLen).trim();
697
+ cap[3] = '';
698
+ }
699
+ }
700
+ let href = cap[2];
701
+ let title = '';
702
+ if (this.options.pedantic) {
703
+ // split pedantic href and title
704
+ const link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(href);
705
+ if (link) {
706
+ href = link[1];
707
+ title = link[3];
708
+ }
709
+ }
710
+ else {
711
+ title = cap[3] ? cap[3].slice(1, -1) : '';
712
+ }
713
+ href = href.trim();
714
+ if (/^</.test(href)) {
715
+ if (this.options.pedantic && !(/>$/.test(trimmedUrl))) {
716
+ // pedantic allows starting angle bracket without ending angle bracket
717
+ href = href.slice(1);
718
+ }
719
+ else {
720
+ href = href.slice(1, -1);
721
+ }
722
+ }
723
+ return outputLink(cap, {
724
+ href: href ? href.replace(this.rules.inline.anyPunctuation, '$1') : href,
725
+ title: title ? title.replace(this.rules.inline.anyPunctuation, '$1') : title
726
+ }, cap[0], this.lexer);
727
+ }
728
+ }
729
+ reflink(src, links) {
730
+ let cap;
731
+ if ((cap = this.rules.inline.reflink.exec(src))
732
+ || (cap = this.rules.inline.nolink.exec(src))) {
733
+ const linkString = (cap[2] || cap[1]).replace(/\s+/g, ' ');
734
+ const link = links[linkString.toLowerCase()];
735
+ if (!link) {
736
+ const text = cap[0].charAt(0);
737
+ return {
738
+ type: 'text',
739
+ raw: text,
740
+ text
741
+ };
742
+ }
743
+ return outputLink(cap, link, cap[0], this.lexer);
744
+ }
745
+ }
746
+ emStrong(src, maskedSrc, prevChar = '') {
747
+ let match = this.rules.inline.emStrongLDelim.exec(src);
748
+ if (!match)
749
+ return;
750
+ // _ can't be between two alphanumerics. \p{L}\p{N} includes non-english alphabet/numbers as well
751
+ if (match[3] && prevChar.match(/[\p{L}\p{N}]/u))
752
+ return;
753
+ const nextChar = match[1] || match[2] || '';
754
+ if (!nextChar || !prevChar || this.rules.inline.punctuation.exec(prevChar)) {
755
+ // unicode Regex counts emoji as 1 char; spread into array for proper count (used multiple times below)
756
+ const lLength = [...match[0]].length - 1;
757
+ let rDelim, rLength, delimTotal = lLength, midDelimTotal = 0;
758
+ const endReg = match[0][0] === '*' ? this.rules.inline.emStrongRDelimAst : this.rules.inline.emStrongRDelimUnd;
759
+ endReg.lastIndex = 0;
760
+ // Clip maskedSrc to same section of string as src (move to lexer?)
761
+ maskedSrc = maskedSrc.slice(-1 * src.length + lLength);
762
+ while ((match = endReg.exec(maskedSrc)) != null) {
763
+ rDelim = match[1] || match[2] || match[3] || match[4] || match[5] || match[6];
764
+ if (!rDelim)
765
+ continue; // skip single * in __abc*abc__
766
+ rLength = [...rDelim].length;
767
+ if (match[3] || match[4]) { // found another Left Delim
768
+ delimTotal += rLength;
769
+ continue;
770
+ }
771
+ else if (match[5] || match[6]) { // either Left or Right Delim
772
+ if (lLength % 3 && !((lLength + rLength) % 3)) {
773
+ midDelimTotal += rLength;
774
+ continue; // CommonMark Emphasis Rules 9-10
775
+ }
776
+ }
777
+ delimTotal -= rLength;
778
+ if (delimTotal > 0)
779
+ continue; // Haven't found enough closing delimiters
780
+ // Remove extra characters. *a*** -> *a*
781
+ rLength = Math.min(rLength, rLength + delimTotal + midDelimTotal);
782
+ // char length can be >1 for unicode characters;
783
+ const lastCharLength = [...match[0]][0].length;
784
+ const raw = src.slice(0, lLength + match.index + lastCharLength + rLength);
785
+ // Create `em` if smallest delimiter has odd char count. *a***
786
+ if (Math.min(lLength, rLength) % 2) {
787
+ const text = raw.slice(1, -1);
788
+ return {
789
+ type: 'em',
790
+ raw,
791
+ text,
792
+ tokens: this.lexer.inlineTokens(text)
793
+ };
794
+ }
795
+ // Create 'strong' if smallest delimiter has even char count. **a***
796
+ const text = raw.slice(2, -2);
797
+ return {
798
+ type: 'strong',
799
+ raw,
800
+ text,
801
+ tokens: this.lexer.inlineTokens(text)
802
+ };
803
+ }
804
+ }
805
+ }
806
+ codespan(src) {
807
+ const cap = this.rules.inline.code.exec(src);
808
+ if (cap) {
809
+ let text = cap[2].replace(/\n/g, ' ');
810
+ const hasNonSpaceChars = /[^ ]/.test(text);
811
+ const hasSpaceCharsOnBothEnds = /^ /.test(text) && / $/.test(text);
812
+ if (hasNonSpaceChars && hasSpaceCharsOnBothEnds) {
813
+ text = text.substring(1, text.length - 1);
814
+ }
815
+ text = escape$1(text, true);
816
+ return {
817
+ type: 'codespan',
818
+ raw: cap[0],
819
+ text
820
+ };
821
+ }
822
+ }
823
+ br(src) {
824
+ const cap = this.rules.inline.br.exec(src);
825
+ if (cap) {
826
+ return {
827
+ type: 'br',
828
+ raw: cap[0]
829
+ };
830
+ }
831
+ }
832
+ del(src) {
833
+ const cap = this.rules.inline.del.exec(src);
834
+ if (cap) {
835
+ return {
836
+ type: 'del',
837
+ raw: cap[0],
838
+ text: cap[2],
839
+ tokens: this.lexer.inlineTokens(cap[2])
840
+ };
841
+ }
842
+ }
843
+ autolink(src) {
844
+ const cap = this.rules.inline.autolink.exec(src);
845
+ if (cap) {
846
+ let text, href;
847
+ if (cap[2] === '@') {
848
+ text = escape$1(cap[1]);
849
+ href = 'mailto:' + text;
850
+ }
851
+ else {
852
+ text = escape$1(cap[1]);
853
+ href = text;
854
+ }
855
+ return {
856
+ type: 'link',
857
+ raw: cap[0],
858
+ text,
859
+ href,
860
+ tokens: [
861
+ {
862
+ type: 'text',
863
+ raw: text,
864
+ text
865
+ }
866
+ ]
867
+ };
868
+ }
869
+ }
870
+ url(src) {
871
+ let cap;
872
+ if (cap = this.rules.inline.url.exec(src)) {
873
+ let text, href;
874
+ if (cap[2] === '@') {
875
+ text = escape$1(cap[0]);
876
+ href = 'mailto:' + text;
877
+ }
878
+ else {
879
+ // do extended autolink path validation
880
+ let prevCapZero;
881
+ do {
882
+ prevCapZero = cap[0];
883
+ cap[0] = this.rules.inline._backpedal.exec(cap[0])?.[0] ?? '';
884
+ } while (prevCapZero !== cap[0]);
885
+ text = escape$1(cap[0]);
886
+ if (cap[1] === 'www.') {
887
+ href = 'http://' + cap[0];
888
+ }
889
+ else {
890
+ href = cap[0];
891
+ }
892
+ }
893
+ return {
894
+ type: 'link',
895
+ raw: cap[0],
896
+ text,
897
+ href,
898
+ tokens: [
899
+ {
900
+ type: 'text',
901
+ raw: text,
902
+ text
903
+ }
904
+ ]
905
+ };
906
+ }
907
+ }
908
+ inlineText(src) {
909
+ const cap = this.rules.inline.text.exec(src);
910
+ if (cap) {
911
+ let text;
912
+ if (this.lexer.state.inRawBlock) {
913
+ text = cap[0];
914
+ }
915
+ else {
916
+ text = escape$1(cap[0]);
917
+ }
918
+ return {
919
+ type: 'text',
920
+ raw: cap[0],
921
+ text
922
+ };
923
+ }
924
+ }
925
+ }
926
+
927
+ /**
928
+ * Block-Level Grammar
929
+ */
930
+ const newline = /^(?: *(?:\n|$))+/;
931
+ const blockCode = /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/;
932
+ const fences = /^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/;
933
+ const hr = /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/;
934
+ const heading = /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/;
935
+ const bullet = /(?:[*+-]|\d{1,9}[.)])/;
936
+ const lheading = edit(/^(?!bull )((?:.|\n(?!\s*?\n|bull ))+?)\n {0,3}(=+|-+) *(?:\n+|$)/)
937
+ .replace(/bull/g, bullet) // lists can interrupt
938
+ .getRegex();
939
+ const _paragraph = /^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/;
940
+ const blockText = /^[^\n]+/;
941
+ const _blockLabel = /(?!\s*\])(?:\\.|[^\[\]\\])+/;
942
+ const def = edit(/^ {0,3}\[(label)\]: *(?:\n *)?([^<\s][^\s]*|<.*?>)(?:(?: +(?:\n *)?| *\n *)(title))? *(?:\n+|$)/)
943
+ .replace('label', _blockLabel)
944
+ .replace('title', /(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/)
945
+ .getRegex();
946
+ const list = edit(/^( {0,3}bull)([ \t][^\n]+?)?(?:\n|$)/)
947
+ .replace(/bull/g, bullet)
948
+ .getRegex();
949
+ const _tag = 'address|article|aside|base|basefont|blockquote|body|caption'
950
+ + '|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption'
951
+ + '|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe'
952
+ + '|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option'
953
+ + '|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr'
954
+ + '|track|ul';
955
+ const _comment = /<!--(?!-?>)[\s\S]*?(?:-->|$)/;
956
+ const html = edit('^ {0,3}(?:' // optional indentation
957
+ + '<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)' // (1)
958
+ + '|comment[^\\n]*(\\n+|$)' // (2)
959
+ + '|<\\?[\\s\\S]*?(?:\\?>\\n*|$)' // (3)
960
+ + '|<![A-Z][\\s\\S]*?(?:>\\n*|$)' // (4)
961
+ + '|<!\\[CDATA\\[[\\s\\S]*?(?:\\]\\]>\\n*|$)' // (5)
962
+ + '|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (6)
963
+ + '|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (7) open tag
964
+ + '|</(?!script|pre|style|textarea)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)' // (7) closing tag
965
+ + ')', 'i')
966
+ .replace('comment', _comment)
967
+ .replace('tag', _tag)
968
+ .replace('attribute', / +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/)
969
+ .getRegex();
970
+ const paragraph = edit(_paragraph)
971
+ .replace('hr', hr)
972
+ .replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
973
+ .replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
974
+ .replace('|table', '')
975
+ .replace('blockquote', ' {0,3}>')
976
+ .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
977
+ .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
978
+ .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
979
+ .replace('tag', _tag) // pars can be interrupted by type (6) html blocks
980
+ .getRegex();
981
+ const blockquote = edit(/^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/)
982
+ .replace('paragraph', paragraph)
983
+ .getRegex();
984
+ /**
985
+ * Normal Block Grammar
986
+ */
987
+ const blockNormal = {
988
+ blockquote,
989
+ code: blockCode,
990
+ def,
991
+ fences,
992
+ heading,
993
+ hr,
994
+ html,
995
+ lheading,
996
+ list,
997
+ newline,
998
+ paragraph,
999
+ table: noopTest,
1000
+ text: blockText
1001
+ };
1002
+ /**
1003
+ * GFM Block Grammar
1004
+ */
1005
+ const gfmTable = edit('^ *([^\\n ].*)\\n' // Header
1006
+ + ' {0,3}((?:\\| *)?:?-+:? *(?:\\| *:?-+:? *)*(?:\\| *)?)' // Align
1007
+ + '(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)') // Cells
1008
+ .replace('hr', hr)
1009
+ .replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
1010
+ .replace('blockquote', ' {0,3}>')
1011
+ .replace('code', ' {4}[^\\n]')
1012
+ .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
1013
+ .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
1014
+ .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
1015
+ .replace('tag', _tag) // tables can be interrupted by type (6) html blocks
1016
+ .getRegex();
1017
+ const blockGfm = {
1018
+ ...blockNormal,
1019
+ table: gfmTable,
1020
+ paragraph: edit(_paragraph)
1021
+ .replace('hr', hr)
1022
+ .replace('heading', ' {0,3}#{1,6}(?:\\s|$)')
1023
+ .replace('|lheading', '') // setex headings don't interrupt commonmark paragraphs
1024
+ .replace('table', gfmTable) // interrupt paragraphs with table
1025
+ .replace('blockquote', ' {0,3}>')
1026
+ .replace('fences', ' {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n')
1027
+ .replace('list', ' {0,3}(?:[*+-]|1[.)]) ') // only lists starting from 1 can interrupt
1028
+ .replace('html', '</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)')
1029
+ .replace('tag', _tag) // pars can be interrupted by type (6) html blocks
1030
+ .getRegex()
1031
+ };
1032
+ /**
1033
+ * Pedantic grammar (original John Gruber's loose markdown specification)
1034
+ */
1035
+ const blockPedantic = {
1036
+ ...blockNormal,
1037
+ html: edit('^ *(?:comment *(?:\\n|\\s*$)'
1038
+ + '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
1039
+ + '|<tag(?:"[^"]*"|\'[^\']*\'|\\s[^\'"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))')
1040
+ .replace('comment', _comment)
1041
+ .replace(/tag/g, '(?!(?:'
1042
+ + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub'
1043
+ + '|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)'
1044
+ + '\\b)\\w+(?!:|[^\\w\\s@]*@)\\b')
1045
+ .getRegex(),
1046
+ def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,
1047
+ heading: /^(#{1,6})(.*)(?:\n+|$)/,
1048
+ fences: noopTest, // fences not supported
1049
+ lheading: /^(.+?)\n {0,3}(=+|-+) *(?:\n+|$)/,
1050
+ paragraph: edit(_paragraph)
1051
+ .replace('hr', hr)
1052
+ .replace('heading', ' *#{1,6} *[^\n]')
1053
+ .replace('lheading', lheading)
1054
+ .replace('|table', '')
1055
+ .replace('blockquote', ' {0,3}>')
1056
+ .replace('|fences', '')
1057
+ .replace('|list', '')
1058
+ .replace('|html', '')
1059
+ .replace('|tag', '')
1060
+ .getRegex()
1061
+ };
1062
+ /**
1063
+ * Inline-Level Grammar
1064
+ */
1065
+ const escape = /^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/;
1066
+ const inlineCode = /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/;
1067
+ const br = /^( {2,}|\\)\n(?!\s*$)/;
1068
+ const inlineText = /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<!\[`*_]|\b_|$)|[^ ](?= {2,}\n)))/;
1069
+ // list of unicode punctuation marks, plus any missing characters from CommonMark spec
1070
+ const _punctuation = '\\p{P}$+<=>`^|~';
1071
+ const punctuation = edit(/^((?![*_])[\spunctuation])/, 'u')
1072
+ .replace(/punctuation/g, _punctuation).getRegex();
1073
+ // sequences em should skip over [title](link), `code`, <html>
1074
+ const blockSkip = /\[[^[\]]*?\]\([^\(\)]*?\)|`[^`]*?`|<[^<>]*?>/g;
1075
+ const emStrongLDelim = edit(/^(?:\*+(?:((?!\*)[punct])|[^\s*]))|^_+(?:((?!_)[punct])|([^\s_]))/, 'u')
1076
+ .replace(/punct/g, _punctuation)
1077
+ .getRegex();
1078
+ const emStrongRDelimAst = edit('^[^_*]*?__[^_*]*?\\*[^_*]*?(?=__)' // Skip orphan inside strong
1079
+ + '|[^*]+(?=[^*])' // Consume to delim
1080
+ + '|(?!\\*)[punct](\\*+)(?=[\\s]|$)' // (1) #*** can only be a Right Delimiter
1081
+ + '|[^punct\\s](\\*+)(?!\\*)(?=[punct\\s]|$)' // (2) a***#, a*** can only be a Right Delimiter
1082
+ + '|(?!\\*)[punct\\s](\\*+)(?=[^punct\\s])' // (3) #***a, ***a can only be Left Delimiter
1083
+ + '|[\\s](\\*+)(?!\\*)(?=[punct])' // (4) ***# can only be Left Delimiter
1084
+ + '|(?!\\*)[punct](\\*+)(?!\\*)(?=[punct])' // (5) #***# can be either Left or Right Delimiter
1085
+ + '|[^punct\\s](\\*+)(?=[^punct\\s])', 'gu') // (6) a***a can be either Left or Right Delimiter
1086
+ .replace(/punct/g, _punctuation)
1087
+ .getRegex();
1088
+ // (6) Not allowed for _
1089
+ const emStrongRDelimUnd = edit('^[^_*]*?\\*\\*[^_*]*?_[^_*]*?(?=\\*\\*)' // Skip orphan inside strong
1090
+ + '|[^_]+(?=[^_])' // Consume to delim
1091
+ + '|(?!_)[punct](_+)(?=[\\s]|$)' // (1) #___ can only be a Right Delimiter
1092
+ + '|[^punct\\s](_+)(?!_)(?=[punct\\s]|$)' // (2) a___#, a___ can only be a Right Delimiter
1093
+ + '|(?!_)[punct\\s](_+)(?=[^punct\\s])' // (3) #___a, ___a can only be Left Delimiter
1094
+ + '|[\\s](_+)(?!_)(?=[punct])' // (4) ___# can only be Left Delimiter
1095
+ + '|(?!_)[punct](_+)(?!_)(?=[punct])', 'gu') // (5) #___# can be either Left or Right Delimiter
1096
+ .replace(/punct/g, _punctuation)
1097
+ .getRegex();
1098
+ const anyPunctuation = edit(/\\([punct])/, 'gu')
1099
+ .replace(/punct/g, _punctuation)
1100
+ .getRegex();
1101
+ const autolink = edit(/^<(scheme:[^\s\x00-\x1f<>]*|email)>/)
1102
+ .replace('scheme', /[a-zA-Z][a-zA-Z0-9+.-]{1,31}/)
1103
+ .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])?)+(?![-_])/)
1104
+ .getRegex();
1105
+ const _inlineComment = edit(_comment).replace('(?:-->|$)', '-->').getRegex();
1106
+ const tag = edit('^comment'
1107
+ + '|^</[a-zA-Z][\\w:-]*\\s*>' // self-closing tag
1108
+ + '|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>' // open tag
1109
+ + '|^<\\?[\\s\\S]*?\\?>' // processing instruction, e.g. <?php ?>
1110
+ + '|^<![a-zA-Z]+\\s[\\s\\S]*?>' // declaration, e.g. <!DOCTYPE html>
1111
+ + '|^<!\\[CDATA\\[[\\s\\S]*?\\]\\]>') // CDATA section
1112
+ .replace('comment', _inlineComment)
1113
+ .replace('attribute', /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/)
1114
+ .getRegex();
1115
+ const _inlineLabel = /(?:\[(?:\\.|[^\[\]\\])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/;
1116
+ const link = edit(/^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/)
1117
+ .replace('label', _inlineLabel)
1118
+ .replace('href', /<(?:\\.|[^\n<>\\])+>|[^\s\x00-\x1f]*/)
1119
+ .replace('title', /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/)
1120
+ .getRegex();
1121
+ const reflink = edit(/^!?\[(label)\]\[(ref)\]/)
1122
+ .replace('label', _inlineLabel)
1123
+ .replace('ref', _blockLabel)
1124
+ .getRegex();
1125
+ const nolink = edit(/^!?\[(ref)\](?:\[\])?/)
1126
+ .replace('ref', _blockLabel)
1127
+ .getRegex();
1128
+ const reflinkSearch = edit('reflink|nolink(?!\\()', 'g')
1129
+ .replace('reflink', reflink)
1130
+ .replace('nolink', nolink)
1131
+ .getRegex();
1132
+ /**
1133
+ * Normal Inline Grammar
1134
+ */
1135
+ const inlineNormal = {
1136
+ _backpedal: noopTest, // only used for GFM url
1137
+ anyPunctuation,
1138
+ autolink,
1139
+ blockSkip,
1140
+ br,
1141
+ code: inlineCode,
1142
+ del: noopTest,
1143
+ emStrongLDelim,
1144
+ emStrongRDelimAst,
1145
+ emStrongRDelimUnd,
1146
+ escape,
1147
+ link,
1148
+ nolink,
1149
+ punctuation,
1150
+ reflink,
1151
+ reflinkSearch,
1152
+ tag,
1153
+ text: inlineText,
1154
+ url: noopTest
1155
+ };
1156
+ /**
1157
+ * Pedantic Inline Grammar
1158
+ */
1159
+ const inlinePedantic = {
1160
+ ...inlineNormal,
1161
+ link: edit(/^!?\[(label)\]\((.*?)\)/)
1162
+ .replace('label', _inlineLabel)
1163
+ .getRegex(),
1164
+ reflink: edit(/^!?\[(label)\]\s*\[([^\]]*)\]/)
1165
+ .replace('label', _inlineLabel)
1166
+ .getRegex()
1167
+ };
1168
+ /**
1169
+ * GFM Inline Grammar
1170
+ */
1171
+ const inlineGfm = {
1172
+ ...inlineNormal,
1173
+ escape: edit(escape).replace('])', '~|])').getRegex(),
1174
+ url: edit(/^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/, 'i')
1175
+ .replace('email', /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/)
1176
+ .getRegex(),
1177
+ _backpedal: /(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/,
1178
+ del: /^(~~?)(?=[^\s~])([\s\S]*?[^\s~])\1(?=[^~]|$)/,
1179
+ text: /^([`~]+|[^`~])(?:(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\<!\[`*~_]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)))/
1180
+ };
1181
+ /**
1182
+ * GFM + Line Breaks Inline Grammar
1183
+ */
1184
+ const inlineBreaks = {
1185
+ ...inlineGfm,
1186
+ br: edit(br).replace('{2,}', '*').getRegex(),
1187
+ text: edit(inlineGfm.text)
1188
+ .replace('\\b_', '\\b_| {2,}\\n')
1189
+ .replace(/\{2,\}/g, '*')
1190
+ .getRegex()
1191
+ };
1192
+ /**
1193
+ * exports
1194
+ */
1195
+ const block = {
1196
+ normal: blockNormal,
1197
+ gfm: blockGfm,
1198
+ pedantic: blockPedantic
1199
+ };
1200
+ const inline = {
1201
+ normal: inlineNormal,
1202
+ gfm: inlineGfm,
1203
+ breaks: inlineBreaks,
1204
+ pedantic: inlinePedantic
1205
+ };
1206
+
1207
+ /**
1208
+ * Block Lexer
1209
+ */
1210
+ class _Lexer {
1211
+ tokens;
1212
+ options;
1213
+ state;
1214
+ tokenizer;
1215
+ inlineQueue;
1216
+ constructor(options) {
1217
+ // TokenList cannot be created in one go
1218
+ this.tokens = [];
1219
+ this.tokens.links = Object.create(null);
1220
+ this.options = options || exports.defaults;
1221
+ this.options.tokenizer = this.options.tokenizer || new _Tokenizer();
1222
+ this.tokenizer = this.options.tokenizer;
1223
+ this.tokenizer.options = this.options;
1224
+ this.tokenizer.lexer = this;
1225
+ this.inlineQueue = [];
1226
+ this.state = {
1227
+ inLink: false,
1228
+ inRawBlock: false,
1229
+ top: true
1230
+ };
1231
+ const rules = {
1232
+ block: block.normal,
1233
+ inline: inline.normal
1234
+ };
1235
+ if (this.options.pedantic) {
1236
+ rules.block = block.pedantic;
1237
+ rules.inline = inline.pedantic;
1238
+ }
1239
+ else if (this.options.gfm) {
1240
+ rules.block = block.gfm;
1241
+ if (this.options.breaks) {
1242
+ rules.inline = inline.breaks;
1243
+ }
1244
+ else {
1245
+ rules.inline = inline.gfm;
1246
+ }
1247
+ }
1248
+ this.tokenizer.rules = rules;
1249
+ }
1250
+ /**
1251
+ * Expose Rules
1252
+ */
1253
+ static get rules() {
1254
+ return {
1255
+ block,
1256
+ inline
1257
+ };
1258
+ }
1259
+ /**
1260
+ * Static Lex Method
1261
+ */
1262
+ static lex(src, options) {
1263
+ const lexer = new _Lexer(options);
1264
+ return lexer.lex(src);
1265
+ }
1266
+ /**
1267
+ * Static Lex Inline Method
1268
+ */
1269
+ static lexInline(src, options) {
1270
+ const lexer = new _Lexer(options);
1271
+ return lexer.inlineTokens(src);
1272
+ }
1273
+ /**
1274
+ * Preprocessing
1275
+ */
1276
+ lex(src) {
1277
+ src = src
1278
+ .replace(/\r\n|\r/g, '\n');
1279
+ this.blockTokens(src, this.tokens);
1280
+ for (let i = 0; i < this.inlineQueue.length; i++) {
1281
+ const next = this.inlineQueue[i];
1282
+ this.inlineTokens(next.src, next.tokens);
1283
+ }
1284
+ this.inlineQueue = [];
1285
+ return this.tokens;
1286
+ }
1287
+ blockTokens(src, tokens = []) {
1288
+ if (this.options.pedantic) {
1289
+ src = src.replace(/\t/g, ' ').replace(/^ +$/gm, '');
1290
+ }
1291
+ else {
1292
+ src = src.replace(/^( *)(\t+)/gm, (_, leading, tabs) => {
1293
+ return leading + ' '.repeat(tabs.length);
1294
+ });
1295
+ }
1296
+ let token;
1297
+ let lastToken;
1298
+ let cutSrc;
1299
+ let lastParagraphClipped;
1300
+ while (src) {
1301
+ if (this.options.extensions
1302
+ && this.options.extensions.block
1303
+ && this.options.extensions.block.some((extTokenizer) => {
1304
+ if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
1305
+ src = src.substring(token.raw.length);
1306
+ tokens.push(token);
1307
+ return true;
1308
+ }
1309
+ return false;
1310
+ })) {
1311
+ continue;
1312
+ }
1313
+ // newline
1314
+ if (token = this.tokenizer.space(src)) {
1315
+ src = src.substring(token.raw.length);
1316
+ if (token.raw.length === 1 && tokens.length > 0) {
1317
+ // if there's a single \n as a spacer, it's terminating the last line,
1318
+ // so move it there so that we don't get unnecessary paragraph tags
1319
+ tokens[tokens.length - 1].raw += '\n';
1320
+ }
1321
+ else {
1322
+ tokens.push(token);
1323
+ }
1324
+ continue;
1325
+ }
1326
+ // code
1327
+ if (token = this.tokenizer.code(src)) {
1328
+ src = src.substring(token.raw.length);
1329
+ lastToken = tokens[tokens.length - 1];
1330
+ // An indented code block cannot interrupt a paragraph.
1331
+ if (lastToken && (lastToken.type === 'paragraph' || lastToken.type === 'text')) {
1332
+ lastToken.raw += '\n' + token.raw;
1333
+ lastToken.text += '\n' + token.text;
1334
+ this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
1335
+ }
1336
+ else {
1337
+ tokens.push(token);
1338
+ }
1339
+ continue;
1340
+ }
1341
+ // fences
1342
+ if (token = this.tokenizer.fences(src)) {
1343
+ src = src.substring(token.raw.length);
1344
+ tokens.push(token);
1345
+ continue;
1346
+ }
1347
+ // heading
1348
+ if (token = this.tokenizer.heading(src)) {
1349
+ src = src.substring(token.raw.length);
1350
+ tokens.push(token);
1351
+ continue;
1352
+ }
1353
+ // hr
1354
+ if (token = this.tokenizer.hr(src)) {
1355
+ src = src.substring(token.raw.length);
1356
+ tokens.push(token);
1357
+ continue;
1358
+ }
1359
+ // blockquote
1360
+ if (token = this.tokenizer.blockquote(src)) {
1361
+ src = src.substring(token.raw.length);
1362
+ tokens.push(token);
1363
+ continue;
1364
+ }
1365
+ // list
1366
+ if (token = this.tokenizer.list(src)) {
1367
+ src = src.substring(token.raw.length);
1368
+ tokens.push(token);
1369
+ continue;
1370
+ }
1371
+ // html
1372
+ if (token = this.tokenizer.html(src)) {
1373
+ src = src.substring(token.raw.length);
1374
+ tokens.push(token);
1375
+ continue;
1376
+ }
1377
+ // def
1378
+ if (token = this.tokenizer.def(src)) {
1379
+ src = src.substring(token.raw.length);
1380
+ lastToken = tokens[tokens.length - 1];
1381
+ if (lastToken && (lastToken.type === 'paragraph' || lastToken.type === 'text')) {
1382
+ lastToken.raw += '\n' + token.raw;
1383
+ lastToken.text += '\n' + token.raw;
1384
+ this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
1385
+ }
1386
+ else if (!this.tokens.links[token.tag]) {
1387
+ this.tokens.links[token.tag] = {
1388
+ href: token.href,
1389
+ title: token.title
1390
+ };
1391
+ }
1392
+ continue;
1393
+ }
1394
+ // table (gfm)
1395
+ if (token = this.tokenizer.table(src)) {
1396
+ src = src.substring(token.raw.length);
1397
+ tokens.push(token);
1398
+ continue;
1399
+ }
1400
+ // lheading
1401
+ if (token = this.tokenizer.lheading(src)) {
1402
+ src = src.substring(token.raw.length);
1403
+ tokens.push(token);
1404
+ continue;
1405
+ }
1406
+ // top-level paragraph
1407
+ // prevent paragraph consuming extensions by clipping 'src' to extension start
1408
+ cutSrc = src;
1409
+ if (this.options.extensions && this.options.extensions.startBlock) {
1410
+ let startIndex = Infinity;
1411
+ const tempSrc = src.slice(1);
1412
+ let tempStart;
1413
+ this.options.extensions.startBlock.forEach((getStartIndex) => {
1414
+ tempStart = getStartIndex.call({ lexer: this }, tempSrc);
1415
+ if (typeof tempStart === 'number' && tempStart >= 0) {
1416
+ startIndex = Math.min(startIndex, tempStart);
1417
+ }
1418
+ });
1419
+ if (startIndex < Infinity && startIndex >= 0) {
1420
+ cutSrc = src.substring(0, startIndex + 1);
1421
+ }
1422
+ }
1423
+ if (this.state.top && (token = this.tokenizer.paragraph(cutSrc))) {
1424
+ lastToken = tokens[tokens.length - 1];
1425
+ if (lastParagraphClipped && lastToken.type === 'paragraph') {
1426
+ lastToken.raw += '\n' + token.raw;
1427
+ lastToken.text += '\n' + token.text;
1428
+ this.inlineQueue.pop();
1429
+ this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
1430
+ }
1431
+ else {
1432
+ tokens.push(token);
1433
+ }
1434
+ lastParagraphClipped = (cutSrc.length !== src.length);
1435
+ src = src.substring(token.raw.length);
1436
+ continue;
1437
+ }
1438
+ // text
1439
+ if (token = this.tokenizer.text(src)) {
1440
+ src = src.substring(token.raw.length);
1441
+ lastToken = tokens[tokens.length - 1];
1442
+ if (lastToken && lastToken.type === 'text') {
1443
+ lastToken.raw += '\n' + token.raw;
1444
+ lastToken.text += '\n' + token.text;
1445
+ this.inlineQueue.pop();
1446
+ this.inlineQueue[this.inlineQueue.length - 1].src = lastToken.text;
1447
+ }
1448
+ else {
1449
+ tokens.push(token);
1450
+ }
1451
+ continue;
1452
+ }
1453
+ if (src) {
1454
+ const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
1455
+ if (this.options.silent) {
1456
+ console.error(errMsg);
1457
+ break;
1458
+ }
1459
+ else {
1460
+ throw new Error(errMsg);
1461
+ }
1462
+ }
1463
+ }
1464
+ this.state.top = true;
1465
+ return tokens;
1466
+ }
1467
+ inline(src, tokens = []) {
1468
+ this.inlineQueue.push({ src, tokens });
1469
+ return tokens;
1470
+ }
1471
+ /**
1472
+ * Lexing/Compiling
1473
+ */
1474
+ inlineTokens(src, tokens = []) {
1475
+ let token, lastToken, cutSrc;
1476
+ // String with links masked to avoid interference with em and strong
1477
+ let maskedSrc = src;
1478
+ let match;
1479
+ let keepPrevChar, prevChar;
1480
+ // Mask out reflinks
1481
+ if (this.tokens.links) {
1482
+ const links = Object.keys(this.tokens.links);
1483
+ if (links.length > 0) {
1484
+ while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
1485
+ if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
1486
+ maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
1487
+ }
1488
+ }
1489
+ }
1490
+ }
1491
+ // Mask out other blocks
1492
+ while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
1493
+ maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
1494
+ }
1495
+ // Mask out escaped characters
1496
+ while ((match = this.tokenizer.rules.inline.anyPunctuation.exec(maskedSrc)) != null) {
1497
+ maskedSrc = maskedSrc.slice(0, match.index) + '++' + maskedSrc.slice(this.tokenizer.rules.inline.anyPunctuation.lastIndex);
1498
+ }
1499
+ while (src) {
1500
+ if (!keepPrevChar) {
1501
+ prevChar = '';
1502
+ }
1503
+ keepPrevChar = false;
1504
+ // extensions
1505
+ if (this.options.extensions
1506
+ && this.options.extensions.inline
1507
+ && this.options.extensions.inline.some((extTokenizer) => {
1508
+ if (token = extTokenizer.call({ lexer: this }, src, tokens)) {
1509
+ src = src.substring(token.raw.length);
1510
+ tokens.push(token);
1511
+ return true;
1512
+ }
1513
+ return false;
1514
+ })) {
1515
+ continue;
1516
+ }
1517
+ // escape
1518
+ if (token = this.tokenizer.escape(src)) {
1519
+ src = src.substring(token.raw.length);
1520
+ tokens.push(token);
1521
+ continue;
1522
+ }
1523
+ // tag
1524
+ if (token = this.tokenizer.tag(src)) {
1525
+ src = src.substring(token.raw.length);
1526
+ lastToken = tokens[tokens.length - 1];
1527
+ if (lastToken && token.type === 'text' && lastToken.type === 'text') {
1528
+ lastToken.raw += token.raw;
1529
+ lastToken.text += token.text;
1530
+ }
1531
+ else {
1532
+ tokens.push(token);
1533
+ }
1534
+ continue;
1535
+ }
1536
+ // link
1537
+ if (token = this.tokenizer.link(src)) {
1538
+ src = src.substring(token.raw.length);
1539
+ tokens.push(token);
1540
+ continue;
1541
+ }
1542
+ // reflink, nolink
1543
+ if (token = this.tokenizer.reflink(src, this.tokens.links)) {
1544
+ src = src.substring(token.raw.length);
1545
+ lastToken = tokens[tokens.length - 1];
1546
+ if (lastToken && token.type === 'text' && lastToken.type === 'text') {
1547
+ lastToken.raw += token.raw;
1548
+ lastToken.text += token.text;
1549
+ }
1550
+ else {
1551
+ tokens.push(token);
1552
+ }
1553
+ continue;
1554
+ }
1555
+ // em & strong
1556
+ if (token = this.tokenizer.emStrong(src, maskedSrc, prevChar)) {
1557
+ src = src.substring(token.raw.length);
1558
+ tokens.push(token);
1559
+ continue;
1560
+ }
1561
+ // code
1562
+ if (token = this.tokenizer.codespan(src)) {
1563
+ src = src.substring(token.raw.length);
1564
+ tokens.push(token);
1565
+ continue;
1566
+ }
1567
+ // br
1568
+ if (token = this.tokenizer.br(src)) {
1569
+ src = src.substring(token.raw.length);
1570
+ tokens.push(token);
1571
+ continue;
1572
+ }
1573
+ // del (gfm)
1574
+ if (token = this.tokenizer.del(src)) {
1575
+ src = src.substring(token.raw.length);
1576
+ tokens.push(token);
1577
+ continue;
1578
+ }
1579
+ // autolink
1580
+ if (token = this.tokenizer.autolink(src)) {
1581
+ src = src.substring(token.raw.length);
1582
+ tokens.push(token);
1583
+ continue;
1584
+ }
1585
+ // url (gfm)
1586
+ if (!this.state.inLink && (token = this.tokenizer.url(src))) {
1587
+ src = src.substring(token.raw.length);
1588
+ tokens.push(token);
1589
+ continue;
1590
+ }
1591
+ // text
1592
+ // prevent inlineText consuming extensions by clipping 'src' to extension start
1593
+ cutSrc = src;
1594
+ if (this.options.extensions && this.options.extensions.startInline) {
1595
+ let startIndex = Infinity;
1596
+ const tempSrc = src.slice(1);
1597
+ let tempStart;
1598
+ this.options.extensions.startInline.forEach((getStartIndex) => {
1599
+ tempStart = getStartIndex.call({ lexer: this }, tempSrc);
1600
+ if (typeof tempStart === 'number' && tempStart >= 0) {
1601
+ startIndex = Math.min(startIndex, tempStart);
1602
+ }
1603
+ });
1604
+ if (startIndex < Infinity && startIndex >= 0) {
1605
+ cutSrc = src.substring(0, startIndex + 1);
1606
+ }
1607
+ }
1608
+ if (token = this.tokenizer.inlineText(cutSrc)) {
1609
+ src = src.substring(token.raw.length);
1610
+ if (token.raw.slice(-1) !== '_') { // Track prevChar before string of ____ started
1611
+ prevChar = token.raw.slice(-1);
1612
+ }
1613
+ keepPrevChar = true;
1614
+ lastToken = tokens[tokens.length - 1];
1615
+ if (lastToken && lastToken.type === 'text') {
1616
+ lastToken.raw += token.raw;
1617
+ lastToken.text += token.text;
1618
+ }
1619
+ else {
1620
+ tokens.push(token);
1621
+ }
1622
+ continue;
1623
+ }
1624
+ if (src) {
1625
+ const errMsg = 'Infinite loop on byte: ' + src.charCodeAt(0);
1626
+ if (this.options.silent) {
1627
+ console.error(errMsg);
1628
+ break;
1629
+ }
1630
+ else {
1631
+ throw new Error(errMsg);
1632
+ }
1633
+ }
1634
+ }
1635
+ return tokens;
1636
+ }
1637
+ }
1638
+
1639
+ /**
1640
+ * Renderer
1641
+ */
1642
+ class _Renderer {
1643
+ options;
1644
+ constructor(options) {
1645
+ this.options = options || exports.defaults;
1646
+ }
1647
+ code(code, infostring, escaped) {
1648
+ const lang = (infostring || '').match(/^\S*/)?.[0];
1649
+ code = code.replace(/\n$/, '') + '\n';
1650
+ if (!lang) {
1651
+ return '<pre><code>'
1652
+ + (escaped ? code : escape$1(code, true))
1653
+ + '</code></pre>\n';
1654
+ }
1655
+ return '<pre><code class="language-'
1656
+ + escape$1(lang)
1657
+ + '">'
1658
+ + (escaped ? code : escape$1(code, true))
1659
+ + '</code></pre>\n';
1660
+ }
1661
+ blockquote(quote) {
1662
+ return `<blockquote>\n${quote}</blockquote>\n`;
1663
+ }
1664
+ html(html, block) {
1665
+ return html;
1666
+ }
1667
+ heading(text, level, raw) {
1668
+ // ignore IDs
1669
+ return `<h${level}>${text}</h${level}>\n`;
1670
+ }
1671
+ hr() {
1672
+ return '<hr>\n';
1673
+ }
1674
+ list(body, ordered, start) {
1675
+ const type = ordered ? 'ol' : 'ul';
1676
+ const startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
1677
+ return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
1678
+ }
1679
+ listitem(text, task, checked) {
1680
+ return `<li>${text}</li>\n`;
1681
+ }
1682
+ checkbox(checked) {
1683
+ return '<input '
1684
+ + (checked ? 'checked="" ' : '')
1685
+ + 'disabled="" type="checkbox">';
1686
+ }
1687
+ paragraph(text) {
1688
+ return `<p>${text}</p>\n`;
1689
+ }
1690
+ table(header, body) {
1691
+ if (body)
1692
+ body = `<tbody>${body}</tbody>`;
1693
+ return '<table>\n'
1694
+ + '<thead>\n'
1695
+ + header
1696
+ + '</thead>\n'
1697
+ + body
1698
+ + '</table>\n';
1699
+ }
1700
+ tablerow(content) {
1701
+ return `<tr>\n${content}</tr>\n`;
1702
+ }
1703
+ tablecell(content, flags) {
1704
+ const type = flags.header ? 'th' : 'td';
1705
+ const tag = flags.align
1706
+ ? `<${type} align="${flags.align}">`
1707
+ : `<${type}>`;
1708
+ return tag + content + `</${type}>\n`;
1709
+ }
1710
+ /**
1711
+ * span level renderer
1712
+ */
1713
+ strong(text) {
1714
+ return `<strong>${text}</strong>`;
1715
+ }
1716
+ em(text) {
1717
+ return `<em>${text}</em>`;
1718
+ }
1719
+ codespan(text) {
1720
+ return `<code>${text}</code>`;
1721
+ }
1722
+ br() {
1723
+ return '<br>';
1724
+ }
1725
+ del(text) {
1726
+ return `<del>${text}</del>`;
1727
+ }
1728
+ link(href, title, text) {
1729
+ const cleanHref = cleanUrl(href);
1730
+ if (cleanHref === null) {
1731
+ return text;
1732
+ }
1733
+ href = cleanHref;
1734
+ let out = '<a href="' + href + '"';
1735
+ if (title) {
1736
+ out += ' title="' + title + '"';
1737
+ }
1738
+ out += '>' + text + '</a>';
1739
+ return out;
1740
+ }
1741
+ image(href, title, text) {
1742
+ const cleanHref = cleanUrl(href);
1743
+ if (cleanHref === null) {
1744
+ return text;
1745
+ }
1746
+ href = cleanHref;
1747
+ let out = `<img src="${href}" alt="${text}"`;
1748
+ if (title) {
1749
+ out += ` title="${title}"`;
1750
+ }
1751
+ out += '>';
1752
+ return out;
1753
+ }
1754
+ text(text) {
1755
+ return text;
1756
+ }
1757
+ }
1758
+
1759
+ /**
1760
+ * TextRenderer
1761
+ * returns only the textual part of the token
1762
+ */
1763
+ class _TextRenderer {
1764
+ // no need for block level renderers
1765
+ strong(text) {
1766
+ return text;
1767
+ }
1768
+ em(text) {
1769
+ return text;
1770
+ }
1771
+ codespan(text) {
1772
+ return text;
1773
+ }
1774
+ del(text) {
1775
+ return text;
1776
+ }
1777
+ html(text) {
1778
+ return text;
1779
+ }
1780
+ text(text) {
1781
+ return text;
1782
+ }
1783
+ link(href, title, text) {
1784
+ return '' + text;
1785
+ }
1786
+ image(href, title, text) {
1787
+ return '' + text;
1788
+ }
1789
+ br() {
1790
+ return '';
1791
+ }
1792
+ }
1793
+
1794
+ /**
1795
+ * Parsing & Compiling
1796
+ */
1797
+ class _Parser {
1798
+ options;
1799
+ renderer;
1800
+ textRenderer;
1801
+ constructor(options) {
1802
+ this.options = options || exports.defaults;
1803
+ this.options.renderer = this.options.renderer || new _Renderer();
1804
+ this.renderer = this.options.renderer;
1805
+ this.renderer.options = this.options;
1806
+ this.textRenderer = new _TextRenderer();
1807
+ }
1808
+ /**
1809
+ * Static Parse Method
1810
+ */
1811
+ static parse(tokens, options) {
1812
+ const parser = new _Parser(options);
1813
+ return parser.parse(tokens);
1814
+ }
1815
+ /**
1816
+ * Static Parse Inline Method
1817
+ */
1818
+ static parseInline(tokens, options) {
1819
+ const parser = new _Parser(options);
1820
+ return parser.parseInline(tokens);
1821
+ }
1822
+ /**
1823
+ * Parse Loop
1824
+ */
1825
+ parse(tokens, top = true) {
1826
+ let out = '';
1827
+ for (let i = 0; i < tokens.length; i++) {
1828
+ const token = tokens[i];
1829
+ // Run any renderer extensions
1830
+ if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[token.type]) {
1831
+ const genericToken = token;
1832
+ const ret = this.options.extensions.renderers[genericToken.type].call({ parser: this }, genericToken);
1833
+ if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'html', 'paragraph', 'text'].includes(genericToken.type)) {
1834
+ out += ret || '';
1835
+ continue;
1836
+ }
1837
+ }
1838
+ switch (token.type) {
1839
+ case 'space': {
1840
+ continue;
1841
+ }
1842
+ case 'hr': {
1843
+ out += this.renderer.hr();
1844
+ continue;
1845
+ }
1846
+ case 'heading': {
1847
+ const headingToken = token;
1848
+ out += this.renderer.heading(this.parseInline(headingToken.tokens), headingToken.depth, unescape(this.parseInline(headingToken.tokens, this.textRenderer)));
1849
+ continue;
1850
+ }
1851
+ case 'code': {
1852
+ const codeToken = token;
1853
+ out += this.renderer.code(codeToken.text, codeToken.lang, !!codeToken.escaped);
1854
+ continue;
1855
+ }
1856
+ case 'table': {
1857
+ const tableToken = token;
1858
+ let header = '';
1859
+ // header
1860
+ let cell = '';
1861
+ for (let j = 0; j < tableToken.header.length; j++) {
1862
+ cell += this.renderer.tablecell(this.parseInline(tableToken.header[j].tokens), { header: true, align: tableToken.align[j] });
1863
+ }
1864
+ header += this.renderer.tablerow(cell);
1865
+ let body = '';
1866
+ for (let j = 0; j < tableToken.rows.length; j++) {
1867
+ const row = tableToken.rows[j];
1868
+ cell = '';
1869
+ for (let k = 0; k < row.length; k++) {
1870
+ cell += this.renderer.tablecell(this.parseInline(row[k].tokens), { header: false, align: tableToken.align[k] });
1871
+ }
1872
+ body += this.renderer.tablerow(cell);
1873
+ }
1874
+ out += this.renderer.table(header, body);
1875
+ continue;
1876
+ }
1877
+ case 'blockquote': {
1878
+ const blockquoteToken = token;
1879
+ const body = this.parse(blockquoteToken.tokens);
1880
+ out += this.renderer.blockquote(body);
1881
+ continue;
1882
+ }
1883
+ case 'list': {
1884
+ const listToken = token;
1885
+ const ordered = listToken.ordered;
1886
+ const start = listToken.start;
1887
+ const loose = listToken.loose;
1888
+ let body = '';
1889
+ for (let j = 0; j < listToken.items.length; j++) {
1890
+ const item = listToken.items[j];
1891
+ const checked = item.checked;
1892
+ const task = item.task;
1893
+ let itemBody = '';
1894
+ if (item.task) {
1895
+ const checkbox = this.renderer.checkbox(!!checked);
1896
+ if (loose) {
1897
+ if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
1898
+ item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
1899
+ if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
1900
+ item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
1901
+ }
1902
+ }
1903
+ else {
1904
+ item.tokens.unshift({
1905
+ type: 'text',
1906
+ text: checkbox + ' '
1907
+ });
1908
+ }
1909
+ }
1910
+ else {
1911
+ itemBody += checkbox + ' ';
1912
+ }
1913
+ }
1914
+ itemBody += this.parse(item.tokens, loose);
1915
+ body += this.renderer.listitem(itemBody, task, !!checked);
1916
+ }
1917
+ out += this.renderer.list(body, ordered, start);
1918
+ continue;
1919
+ }
1920
+ case 'html': {
1921
+ const htmlToken = token;
1922
+ out += this.renderer.html(htmlToken.text, htmlToken.block);
1923
+ continue;
1924
+ }
1925
+ case 'paragraph': {
1926
+ const paragraphToken = token;
1927
+ out += this.renderer.paragraph(this.parseInline(paragraphToken.tokens));
1928
+ continue;
1929
+ }
1930
+ case 'text': {
1931
+ let textToken = token;
1932
+ let body = textToken.tokens ? this.parseInline(textToken.tokens) : textToken.text;
1933
+ while (i + 1 < tokens.length && tokens[i + 1].type === 'text') {
1934
+ textToken = tokens[++i];
1935
+ body += '\n' + (textToken.tokens ? this.parseInline(textToken.tokens) : textToken.text);
1936
+ }
1937
+ out += top ? this.renderer.paragraph(body) : body;
1938
+ continue;
1939
+ }
1940
+ default: {
1941
+ const errMsg = 'Token with "' + token.type + '" type was not found.';
1942
+ if (this.options.silent) {
1943
+ console.error(errMsg);
1944
+ return '';
1945
+ }
1946
+ else {
1947
+ throw new Error(errMsg);
1948
+ }
1949
+ }
1950
+ }
1951
+ }
1952
+ return out;
1953
+ }
1954
+ /**
1955
+ * Parse Inline Tokens
1956
+ */
1957
+ parseInline(tokens, renderer) {
1958
+ renderer = renderer || this.renderer;
1959
+ let out = '';
1960
+ for (let i = 0; i < tokens.length; i++) {
1961
+ const token = tokens[i];
1962
+ // Run any renderer extensions
1963
+ if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[token.type]) {
1964
+ const ret = this.options.extensions.renderers[token.type].call({ parser: this }, token);
1965
+ if (ret !== false || !['escape', 'html', 'link', 'image', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(token.type)) {
1966
+ out += ret || '';
1967
+ continue;
1968
+ }
1969
+ }
1970
+ switch (token.type) {
1971
+ case 'escape': {
1972
+ const escapeToken = token;
1973
+ out += renderer.text(escapeToken.text);
1974
+ break;
1975
+ }
1976
+ case 'html': {
1977
+ const tagToken = token;
1978
+ out += renderer.html(tagToken.text);
1979
+ break;
1980
+ }
1981
+ case 'link': {
1982
+ const linkToken = token;
1983
+ out += renderer.link(linkToken.href, linkToken.title, this.parseInline(linkToken.tokens, renderer));
1984
+ break;
1985
+ }
1986
+ case 'image': {
1987
+ const imageToken = token;
1988
+ out += renderer.image(imageToken.href, imageToken.title, imageToken.text);
1989
+ break;
1990
+ }
1991
+ case 'strong': {
1992
+ const strongToken = token;
1993
+ out += renderer.strong(this.parseInline(strongToken.tokens, renderer));
1994
+ break;
1995
+ }
1996
+ case 'em': {
1997
+ const emToken = token;
1998
+ out += renderer.em(this.parseInline(emToken.tokens, renderer));
1999
+ break;
2000
+ }
2001
+ case 'codespan': {
2002
+ const codespanToken = token;
2003
+ out += renderer.codespan(codespanToken.text);
2004
+ break;
2005
+ }
2006
+ case 'br': {
2007
+ out += renderer.br();
2008
+ break;
2009
+ }
2010
+ case 'del': {
2011
+ const delToken = token;
2012
+ out += renderer.del(this.parseInline(delToken.tokens, renderer));
2013
+ break;
2014
+ }
2015
+ case 'text': {
2016
+ const textToken = token;
2017
+ out += renderer.text(textToken.text);
2018
+ break;
2019
+ }
2020
+ default: {
2021
+ const errMsg = 'Token with "' + token.type + '" type was not found.';
2022
+ if (this.options.silent) {
2023
+ console.error(errMsg);
2024
+ return '';
2025
+ }
2026
+ else {
2027
+ throw new Error(errMsg);
2028
+ }
2029
+ }
2030
+ }
2031
+ }
2032
+ return out;
2033
+ }
2034
+ }
2035
+
2036
+ class _Hooks {
2037
+ options;
2038
+ constructor(options) {
2039
+ this.options = options || exports.defaults;
2040
+ }
2041
+ static passThroughHooks = new Set([
2042
+ 'preprocess',
2043
+ 'postprocess',
2044
+ 'processAllTokens'
2045
+ ]);
2046
+ /**
2047
+ * Process markdown before marked
2048
+ */
2049
+ preprocess(markdown) {
2050
+ return markdown;
2051
+ }
2052
+ /**
2053
+ * Process HTML after marked is finished
2054
+ */
2055
+ postprocess(html) {
2056
+ return html;
2057
+ }
2058
+ /**
2059
+ * Process all tokens before walk tokens
2060
+ */
2061
+ processAllTokens(tokens) {
2062
+ return tokens;
2063
+ }
2064
+ }
2065
+
2066
+ class Marked {
2067
+ defaults = _getDefaults();
2068
+ options = this.setOptions;
2069
+ parse = this.#parseMarkdown(_Lexer.lex, _Parser.parse);
2070
+ parseInline = this.#parseMarkdown(_Lexer.lexInline, _Parser.parseInline);
2071
+ Parser = _Parser;
2072
+ Renderer = _Renderer;
2073
+ TextRenderer = _TextRenderer;
2074
+ Lexer = _Lexer;
2075
+ Tokenizer = _Tokenizer;
2076
+ Hooks = _Hooks;
2077
+ constructor(...args) {
2078
+ this.use(...args);
2079
+ }
2080
+ /**
2081
+ * Run callback for every token
2082
+ */
2083
+ walkTokens(tokens, callback) {
2084
+ let values = [];
2085
+ for (const token of tokens) {
2086
+ values = values.concat(callback.call(this, token));
2087
+ switch (token.type) {
2088
+ case 'table': {
2089
+ const tableToken = token;
2090
+ for (const cell of tableToken.header) {
2091
+ values = values.concat(this.walkTokens(cell.tokens, callback));
2092
+ }
2093
+ for (const row of tableToken.rows) {
2094
+ for (const cell of row) {
2095
+ values = values.concat(this.walkTokens(cell.tokens, callback));
2096
+ }
2097
+ }
2098
+ break;
2099
+ }
2100
+ case 'list': {
2101
+ const listToken = token;
2102
+ values = values.concat(this.walkTokens(listToken.items, callback));
2103
+ break;
2104
+ }
2105
+ default: {
2106
+ const genericToken = token;
2107
+ if (this.defaults.extensions?.childTokens?.[genericToken.type]) {
2108
+ this.defaults.extensions.childTokens[genericToken.type].forEach((childTokens) => {
2109
+ const tokens = genericToken[childTokens].flat(Infinity);
2110
+ values = values.concat(this.walkTokens(tokens, callback));
2111
+ });
2112
+ }
2113
+ else if (genericToken.tokens) {
2114
+ values = values.concat(this.walkTokens(genericToken.tokens, callback));
2115
+ }
2116
+ }
2117
+ }
2118
+ }
2119
+ return values;
2120
+ }
2121
+ use(...args) {
2122
+ const extensions = this.defaults.extensions || { renderers: {}, childTokens: {} };
2123
+ args.forEach((pack) => {
2124
+ // copy options to new object
2125
+ const opts = { ...pack };
2126
+ // set async to true if it was set to true before
2127
+ opts.async = this.defaults.async || opts.async || false;
2128
+ // ==-- Parse "addon" extensions --== //
2129
+ if (pack.extensions) {
2130
+ pack.extensions.forEach((ext) => {
2131
+ if (!ext.name) {
2132
+ throw new Error('extension name required');
2133
+ }
2134
+ if ('renderer' in ext) { // Renderer extensions
2135
+ const prevRenderer = extensions.renderers[ext.name];
2136
+ if (prevRenderer) {
2137
+ // Replace extension with func to run new extension but fall back if false
2138
+ extensions.renderers[ext.name] = function (...args) {
2139
+ let ret = ext.renderer.apply(this, args);
2140
+ if (ret === false) {
2141
+ ret = prevRenderer.apply(this, args);
2142
+ }
2143
+ return ret;
2144
+ };
2145
+ }
2146
+ else {
2147
+ extensions.renderers[ext.name] = ext.renderer;
2148
+ }
2149
+ }
2150
+ if ('tokenizer' in ext) { // Tokenizer Extensions
2151
+ if (!ext.level || (ext.level !== 'block' && ext.level !== 'inline')) {
2152
+ throw new Error("extension level must be 'block' or 'inline'");
2153
+ }
2154
+ const extLevel = extensions[ext.level];
2155
+ if (extLevel) {
2156
+ extLevel.unshift(ext.tokenizer);
2157
+ }
2158
+ else {
2159
+ extensions[ext.level] = [ext.tokenizer];
2160
+ }
2161
+ if (ext.start) { // Function to check for start of token
2162
+ if (ext.level === 'block') {
2163
+ if (extensions.startBlock) {
2164
+ extensions.startBlock.push(ext.start);
2165
+ }
2166
+ else {
2167
+ extensions.startBlock = [ext.start];
2168
+ }
2169
+ }
2170
+ else if (ext.level === 'inline') {
2171
+ if (extensions.startInline) {
2172
+ extensions.startInline.push(ext.start);
2173
+ }
2174
+ else {
2175
+ extensions.startInline = [ext.start];
2176
+ }
2177
+ }
2178
+ }
2179
+ }
2180
+ if ('childTokens' in ext && ext.childTokens) { // Child tokens to be visited by walkTokens
2181
+ extensions.childTokens[ext.name] = ext.childTokens;
2182
+ }
2183
+ });
2184
+ opts.extensions = extensions;
2185
+ }
2186
+ // ==-- Parse "overwrite" extensions --== //
2187
+ if (pack.renderer) {
2188
+ const renderer = this.defaults.renderer || new _Renderer(this.defaults);
2189
+ for (const prop in pack.renderer) {
2190
+ if (!(prop in renderer)) {
2191
+ throw new Error(`renderer '${prop}' does not exist`);
2192
+ }
2193
+ if (prop === 'options') {
2194
+ // ignore options property
2195
+ continue;
2196
+ }
2197
+ const rendererProp = prop;
2198
+ const rendererFunc = pack.renderer[rendererProp];
2199
+ const prevRenderer = renderer[rendererProp];
2200
+ // Replace renderer with func to run extension, but fall back if false
2201
+ renderer[rendererProp] = (...args) => {
2202
+ let ret = rendererFunc.apply(renderer, args);
2203
+ if (ret === false) {
2204
+ ret = prevRenderer.apply(renderer, args);
2205
+ }
2206
+ return ret || '';
2207
+ };
2208
+ }
2209
+ opts.renderer = renderer;
2210
+ }
2211
+ if (pack.tokenizer) {
2212
+ const tokenizer = this.defaults.tokenizer || new _Tokenizer(this.defaults);
2213
+ for (const prop in pack.tokenizer) {
2214
+ if (!(prop in tokenizer)) {
2215
+ throw new Error(`tokenizer '${prop}' does not exist`);
2216
+ }
2217
+ if (['options', 'rules', 'lexer'].includes(prop)) {
2218
+ // ignore options, rules, and lexer properties
2219
+ continue;
2220
+ }
2221
+ const tokenizerProp = prop;
2222
+ const tokenizerFunc = pack.tokenizer[tokenizerProp];
2223
+ const prevTokenizer = tokenizer[tokenizerProp];
2224
+ // Replace tokenizer with func to run extension, but fall back if false
2225
+ // @ts-expect-error cannot type tokenizer function dynamically
2226
+ tokenizer[tokenizerProp] = (...args) => {
2227
+ let ret = tokenizerFunc.apply(tokenizer, args);
2228
+ if (ret === false) {
2229
+ ret = prevTokenizer.apply(tokenizer, args);
2230
+ }
2231
+ return ret;
2232
+ };
2233
+ }
2234
+ opts.tokenizer = tokenizer;
2235
+ }
2236
+ // ==-- Parse Hooks extensions --== //
2237
+ if (pack.hooks) {
2238
+ const hooks = this.defaults.hooks || new _Hooks();
2239
+ for (const prop in pack.hooks) {
2240
+ if (!(prop in hooks)) {
2241
+ throw new Error(`hook '${prop}' does not exist`);
2242
+ }
2243
+ if (prop === 'options') {
2244
+ // ignore options property
2245
+ continue;
2246
+ }
2247
+ const hooksProp = prop;
2248
+ const hooksFunc = pack.hooks[hooksProp];
2249
+ const prevHook = hooks[hooksProp];
2250
+ if (_Hooks.passThroughHooks.has(prop)) {
2251
+ // @ts-expect-error cannot type hook function dynamically
2252
+ hooks[hooksProp] = (arg) => {
2253
+ if (this.defaults.async) {
2254
+ return Promise.resolve(hooksFunc.call(hooks, arg)).then(ret => {
2255
+ return prevHook.call(hooks, ret);
2256
+ });
2257
+ }
2258
+ const ret = hooksFunc.call(hooks, arg);
2259
+ return prevHook.call(hooks, ret);
2260
+ };
2261
+ }
2262
+ else {
2263
+ // @ts-expect-error cannot type hook function dynamically
2264
+ hooks[hooksProp] = (...args) => {
2265
+ let ret = hooksFunc.apply(hooks, args);
2266
+ if (ret === false) {
2267
+ ret = prevHook.apply(hooks, args);
2268
+ }
2269
+ return ret;
2270
+ };
2271
+ }
2272
+ }
2273
+ opts.hooks = hooks;
2274
+ }
2275
+ // ==-- Parse WalkTokens extensions --== //
2276
+ if (pack.walkTokens) {
2277
+ const walkTokens = this.defaults.walkTokens;
2278
+ const packWalktokens = pack.walkTokens;
2279
+ opts.walkTokens = function (token) {
2280
+ let values = [];
2281
+ values.push(packWalktokens.call(this, token));
2282
+ if (walkTokens) {
2283
+ values = values.concat(walkTokens.call(this, token));
2284
+ }
2285
+ return values;
2286
+ };
2287
+ }
2288
+ this.defaults = { ...this.defaults, ...opts };
2289
+ });
2290
+ return this;
2291
+ }
2292
+ setOptions(opt) {
2293
+ this.defaults = { ...this.defaults, ...opt };
2294
+ return this;
2295
+ }
2296
+ lexer(src, options) {
2297
+ return _Lexer.lex(src, options ?? this.defaults);
2298
+ }
2299
+ parser(tokens, options) {
2300
+ return _Parser.parse(tokens, options ?? this.defaults);
2301
+ }
2302
+ #parseMarkdown(lexer, parser) {
2303
+ return (src, options) => {
2304
+ const origOpt = { ...options };
2305
+ const opt = { ...this.defaults, ...origOpt };
2306
+ // Show warning if an extension set async to true but the parse was called with async: false
2307
+ if (this.defaults.async === true && origOpt.async === false) {
2308
+ if (!opt.silent) {
2309
+ console.warn('marked(): The async option was set to true by an extension. The async: false option sent to parse will be ignored.');
2310
+ }
2311
+ opt.async = true;
2312
+ }
2313
+ const throwError = this.#onError(!!opt.silent, !!opt.async);
2314
+ // throw error in case of non string input
2315
+ if (typeof src === 'undefined' || src === null) {
2316
+ return throwError(new Error('marked(): input parameter is undefined or null'));
2317
+ }
2318
+ if (typeof src !== 'string') {
2319
+ return throwError(new Error('marked(): input parameter is of type '
2320
+ + Object.prototype.toString.call(src) + ', string expected'));
2321
+ }
2322
+ if (opt.hooks) {
2323
+ opt.hooks.options = opt;
2324
+ }
2325
+ if (opt.async) {
2326
+ return Promise.resolve(opt.hooks ? opt.hooks.preprocess(src) : src)
2327
+ .then(src => lexer(src, opt))
2328
+ .then(tokens => opt.hooks ? opt.hooks.processAllTokens(tokens) : tokens)
2329
+ .then(tokens => opt.walkTokens ? Promise.all(this.walkTokens(tokens, opt.walkTokens)).then(() => tokens) : tokens)
2330
+ .then(tokens => parser(tokens, opt))
2331
+ .then(html => opt.hooks ? opt.hooks.postprocess(html) : html)
2332
+ .catch(throwError);
2333
+ }
2334
+ try {
2335
+ if (opt.hooks) {
2336
+ src = opt.hooks.preprocess(src);
2337
+ }
2338
+ let tokens = lexer(src, opt);
2339
+ if (opt.hooks) {
2340
+ tokens = opt.hooks.processAllTokens(tokens);
2341
+ }
2342
+ if (opt.walkTokens) {
2343
+ this.walkTokens(tokens, opt.walkTokens);
2344
+ }
2345
+ let html = parser(tokens, opt);
2346
+ if (opt.hooks) {
2347
+ html = opt.hooks.postprocess(html);
2348
+ }
2349
+ return html;
2350
+ }
2351
+ catch (e) {
2352
+ return throwError(e);
2353
+ }
2354
+ };
2355
+ }
2356
+ #onError(silent, async) {
2357
+ return (e) => {
2358
+ e.message += '\nPlease report this to https://github.com/markedjs/marked.';
2359
+ if (silent) {
2360
+ const msg = '<p>An error occurred:</p><pre>'
2361
+ + escape$1(e.message + '', true)
2362
+ + '</pre>';
2363
+ if (async) {
2364
+ return Promise.resolve(msg);
2365
+ }
2366
+ return msg;
2367
+ }
2368
+ if (async) {
2369
+ return Promise.reject(e);
2370
+ }
2371
+ throw e;
2372
+ };
2373
+ }
2374
+ }
2375
+
2376
+ const markedInstance = new Marked();
2377
+ function marked(src, opt) {
2378
+ return markedInstance.parse(src, opt);
2379
+ }
2380
+ /**
2381
+ * Sets the default options.
2382
+ *
2383
+ * @param options Hash of options
2384
+ */
2385
+ marked.options =
2386
+ marked.setOptions = function (options) {
2387
+ markedInstance.setOptions(options);
2388
+ marked.defaults = markedInstance.defaults;
2389
+ changeDefaults(marked.defaults);
2390
+ return marked;
2391
+ };
2392
+ /**
2393
+ * Gets the original marked default options.
2394
+ */
2395
+ marked.getDefaults = _getDefaults;
2396
+ marked.defaults = exports.defaults;
2397
+ /**
2398
+ * Use Extension
2399
+ */
2400
+ marked.use = function (...args) {
2401
+ markedInstance.use(...args);
2402
+ marked.defaults = markedInstance.defaults;
2403
+ changeDefaults(marked.defaults);
2404
+ return marked;
2405
+ };
2406
+ /**
2407
+ * Run callback for every token
2408
+ */
2409
+ marked.walkTokens = function (tokens, callback) {
2410
+ return markedInstance.walkTokens(tokens, callback);
2411
+ };
2412
+ /**
2413
+ * Compiles markdown to HTML without enclosing `p` tag.
2414
+ *
2415
+ * @param src String of markdown source to be compiled
2416
+ * @param options Hash of options
2417
+ * @return String of compiled HTML
2418
+ */
2419
+ marked.parseInline = markedInstance.parseInline;
2420
+ /**
2421
+ * Expose
2422
+ */
2423
+ marked.Parser = _Parser;
2424
+ marked.parser = _Parser.parse;
2425
+ marked.Renderer = _Renderer;
2426
+ marked.TextRenderer = _TextRenderer;
2427
+ marked.Lexer = _Lexer;
2428
+ marked.lexer = _Lexer.lex;
2429
+ marked.Tokenizer = _Tokenizer;
2430
+ marked.Hooks = _Hooks;
2431
+ marked.parse = marked;
2432
+ const options = marked.options;
2433
+ const setOptions = marked.setOptions;
2434
+ const use = marked.use;
2435
+ const walkTokens = marked.walkTokens;
2436
+ const parseInline = marked.parseInline;
2437
+ const parse = marked;
2438
+ const parser = _Parser.parse;
2439
+ const lexer = _Lexer.lex;
2440
+
2441
+ exports.Hooks = _Hooks;
2442
+ exports.Lexer = _Lexer;
2443
+ exports.Marked = Marked;
2444
+ exports.Parser = _Parser;
2445
+ exports.Renderer = _Renderer;
2446
+ exports.TextRenderer = _TextRenderer;
2447
+ exports.Tokenizer = _Tokenizer;
2448
+ exports.getDefaults = _getDefaults;
2449
+ exports.lexer = lexer;
2450
+ exports.marked = marked;
2451
+ exports.options = options;
2452
+ exports.parse = parse;
2453
+ exports.parseInline = parseInline;
2454
+ exports.parser = parser;
2455
+ exports.setOptions = setOptions;
2456
+ exports.use = use;
2457
+ exports.walkTokens = walkTokens;
2458
+
2459
+ }));
2460
+ //# sourceMappingURL=marked.umd.js.map
2461
+
2462
+
2463
+ // ===== Utility Functions =====
2464
+ /**
2465
+ * Utility helper functions
2466
+ */
2467
+
2468
+ /**
2469
+ * Format date to readable string
2470
+ */
2471
+ function formatDate(timestamp) {
2472
+ const date = new Date(timestamp * 1000);
2473
+ return date.toLocaleDateString('en-US', {
2474
+ year: 'numeric',
2475
+ month: 'short',
2476
+ day: 'numeric'
2477
+ });
2478
+ }
2479
+
2480
+ /**
2481
+ * Calculate reading time from word count or content
2482
+ * @param {number|string} wordCountOrContent - Word count (number) or content text (string)
2483
+ * @returns {string} Reading time string (e.g., "5 min read")
2484
+ */
2485
+ function calculateReadingTime(wordCountOrContent) {
2486
+ if (!wordCountOrContent) return 'Unknown';
2487
+
2488
+ const wordsPerMinute = 200;
2489
+ let words;
2490
+
2491
+ // If it's a number, use it directly as word count
2492
+ if (typeof wordCountOrContent === 'number') {
2493
+ words = wordCountOrContent;
2494
+ if (words === 0) return 'Unknown'; // No word count available
2495
+ } else {
2496
+ // Otherwise, calculate from content text (fallback)
2497
+ words = wordCountOrContent.trim().split(/\s+/).filter(w => w.length > 0).length;
2498
+ }
2499
+
2500
+ const minutes = Math.ceil(words / wordsPerMinute);
2501
+ return `${minutes} min read`;
2502
+ }
2503
+
2504
+ /**
2505
+ * Truncate text to specified length
2506
+ */
2507
+ function truncate(text, maxLength = 150) {
2508
+ if (!text || text.length <= maxLength) return text;
2509
+ return text.substring(0, maxLength).trim() + '...';
2510
+ }
2511
+
2512
+ /**
2513
+ * Truncate text to specified max UTF-8 byte length
2514
+ */
2515
+ function truncateBytes(text, maxBytes) {
2516
+ if (!text || !maxBytes || maxBytes <= 0) return text || '';
2517
+ const encoder = new TextEncoder();
2518
+ const bytes = encoder.encode(text);
2519
+ if (bytes.length <= maxBytes) return text;
2520
+ // Binary search for max codepoint length within byte budget
2521
+ let lo = 0, hi = text.length;
2522
+ while (lo < hi) {
2523
+ const mid = Math.floor((lo + hi + 1) / 2);
2524
+ const slice = text.slice(0, mid);
2525
+ const len = encoder.encode(slice).length;
2526
+ if (len <= maxBytes) lo = mid; else hi = mid - 1;
2527
+ }
2528
+ return text.slice(0, lo).trimEnd() + '...';
2529
+ }
2530
+
2531
+ /**
2532
+ * Escape HTML to prevent XSS
2533
+ */
2534
+ function escapeHtml(text) {
2535
+ const div = document.createElement('div');
2536
+ div.textContent = text;
2537
+ return div.innerHTML;
2538
+ }
2539
+
2540
+ /**
2541
+ * Debounce function calls
2542
+ */
2543
+ function debounce(func, wait) {
2544
+ let timeout;
2545
+ return function executedFunction(...args) {
2546
+ const later = () => {
2547
+ clearTimeout(timeout);
2548
+ func(...args);
2549
+ };
2550
+ clearTimeout(timeout);
2551
+ timeout = setTimeout(later, wait);
2552
+ };
2553
+ }
2554
+
2555
+ // ===== API Client =====
2556
+ /**
2557
+ * Content Growth API Client
2558
+ * Handles fetching articles from the widget API (requires API key)
2559
+ */
2560
+ class ContentGrowthAPI {
2561
+ constructor(config) {
2562
+ this.apiKey = config.apiKey;
2563
+ this.baseUrl = config.baseUrl || 'https://api.content-growth.com';
2564
+ this.cache = new Map();
2565
+ this.cacheTTL = 5 * 60 * 1000; // 5 minutes
2566
+ }
2567
+
2568
+ /**
2569
+ * Fetch list of articles
2570
+ */
2571
+ async fetchArticles(options = {}) {
2572
+ const { page = 1, limit = 12, tags = [], category } = options;
2573
+
2574
+ const params = new URLSearchParams({
2575
+ page: page.toString(),
2576
+ limit: limit.toString()
2577
+ });
2578
+
2579
+ if (tags.length > 0) {
2580
+ params.set('tag', tags.join(','));
2581
+ }
2582
+
2583
+ if (category) {
2584
+ params.set('category', category);
2585
+ }
2586
+
2587
+ const url = `${this.baseUrl}/widget/articles?${params}`;
2588
+ const cacheKey = url;
2589
+
2590
+ // Check cache
2591
+ const cached = this.getFromCache(cacheKey);
2592
+ if (cached) {
2593
+ return cached;
2594
+ }
2595
+
2596
+ try {
2597
+
2598
+ const response = await fetch(url, {
2599
+ headers: {
2600
+ 'X-API-Key': this.apiKey
2601
+ }
2602
+ });
2603
+
2604
+
2605
+ if (!response.ok) {
2606
+ const errorText = await response.text();
2607
+ console.error('[ContentGrowthAPI] Error response body:', errorText);
2608
+ throw new Error(`API Error: ${response.status} ${response.statusText}`);
2609
+ }
2610
+
2611
+ const data = await response.json();
2612
+
2613
+ // Cache the result
2614
+ this.setCache(cacheKey, data);
2615
+
2616
+ return data;
2617
+ } catch (error) {
2618
+ console.error('[ContentGrowthAPI] Failed to fetch articles:', error);
2619
+ throw error;
2620
+ }
2621
+ }
2622
+
2623
+ /**
2624
+ * Fetch single article by UUID
2625
+ */
2626
+ async fetchArticle(uuid) {
2627
+ const url = `${this.baseUrl}/widget/articles/${uuid}`;
2628
+ const cacheKey = url;
2629
+
2630
+ // Check cache
2631
+ const cached = this.getFromCache(cacheKey);
2632
+ if (cached) {
2633
+ return cached;
2634
+ }
2635
+
2636
+ try {
2637
+
2638
+ const response = await fetch(url, {
2639
+ headers: {
2640
+ 'X-API-Key': this.apiKey
2641
+ }
2642
+ });
2643
+
2644
+
2645
+ if (!response.ok) {
2646
+ const errorText = await response.text();
2647
+ console.error('[ContentGrowthAPI] Error response body:', errorText);
2648
+ throw new Error(`API Error: ${response.status} ${response.statusText}`);
2649
+ }
2650
+
2651
+ const data = await response.json();
2652
+
2653
+ // Cache the result
2654
+ this.setCache(cacheKey, data);
2655
+
2656
+ return data;
2657
+ } catch (error) {
2658
+ console.error('[ContentGrowthAPI] Failed to fetch article:', error);
2659
+ throw error;
2660
+ }
2661
+ }
2662
+
2663
+ /**
2664
+ * Fetch single article by slug
2665
+ */
2666
+ async fetchArticleBySlug(slug) {
2667
+ const url = `${this.baseUrl}/widget/articles/slug/${slug}`;
2668
+ const cacheKey = url;
2669
+
2670
+ // Check cache
2671
+ const cached = this.getFromCache(cacheKey);
2672
+ if (cached) {
2673
+ return cached;
2674
+ }
2675
+
2676
+ try {
2677
+
2678
+ const response = await fetch(url, {
2679
+ headers: {
2680
+ 'X-API-Key': this.apiKey
2681
+ }
2682
+ });
2683
+
2684
+
2685
+ if (!response.ok) {
2686
+ const errorText = await response.text();
2687
+ console.error('[ContentGrowthAPI] Error response body:', errorText);
2688
+ throw new Error(`API Error: ${response.status} ${response.statusText}`);
2689
+ }
2690
+
2691
+ const data = await response.json();
2692
+
2693
+ // Cache the result
2694
+ this.setCache(cacheKey, data);
2695
+
2696
+ return data;
2697
+ } catch (error) {
2698
+ console.error('[ContentGrowthAPI] Failed to fetch article by slug:', error);
2699
+ throw error;
2700
+ }
2701
+ }
2702
+
2703
+ /**
2704
+ * Get from cache if not expired
2705
+ */
2706
+ getFromCache(key) {
2707
+ const cached = this.cache.get(key);
2708
+ if (!cached) return null;
2709
+
2710
+ const now = Date.now();
2711
+ if (now - cached.timestamp > this.cacheTTL) {
2712
+ this.cache.delete(key);
2713
+ return null;
2714
+ }
2715
+
2716
+ return cached.data;
2717
+ }
2718
+
2719
+ /**
2720
+ * Set cache with timestamp
2721
+ */
2722
+ setCache(key, data) {
2723
+ this.cache.set(key, {
2724
+ data,
2725
+ timestamp: Date.now()
2726
+ });
2727
+ }
2728
+
2729
+ /**
2730
+ * Clear all cache
2731
+ */
2732
+ clearCache() {
2733
+ this.cache.clear();
2734
+ }
2735
+ }
2736
+
2737
+ // ===== Components =====
2738
+ /**
2739
+ * Content Card Component
2740
+ * Displays a single content item in compact or expanded mode
2741
+ */
2742
+
2743
+
2744
+ class ContentCard {
2745
+ constructor(article, options = {}) {
2746
+ this.article = article;
2747
+ this.displayMode = options.displayMode || 'compact';
2748
+ this.viewerMode = options.viewerMode || 'inline';
2749
+ this.externalUrlPattern = options.externalUrlPattern || '/article/{id}';
2750
+ this.externalTarget = options.externalTarget || 'article-{id}';
2751
+ this.onExpand = options.onExpand || null;
2752
+ this.onClick = options.onClick || null;
2753
+ this.aiSummaryMaxBytes = options.aiSummaryMaxBytes;
2754
+ }
2755
+
2756
+ /**
2757
+ * Render the content card
2758
+ */
2759
+ render() {
2760
+ // Map display modes: compact/comfortable/spacious all use compact layout
2761
+ // expanded shows summary
2762
+ const layoutMode = this.displayMode === 'expanded' ? 'expanded' : 'compact';
2763
+
2764
+ // For external mode, wrap in <a> tag
2765
+ if (this.viewerMode === 'external') {
2766
+ const link = document.createElement('a');
2767
+ link.className = `cg-card cg-card--${this.displayMode}`;
2768
+ link.dataset.contentId = this.article.uuid;
2769
+
2770
+ // Generate URL and target - support both {id} and {slug} placeholders
2771
+ let url = this.externalUrlPattern
2772
+ .replace('{id}', this.article.uuid)
2773
+ .replace('{slug}', this.article.slug || this.article.uuid);
2774
+
2775
+ let target = this.externalTarget
2776
+ .replace('{id}', this.article.uuid)
2777
+ .replace('{slug}', this.article.slug || this.article.uuid);
2778
+
2779
+ link.href = url;
2780
+ link.target = target;
2781
+ link.rel = 'noopener'; // Security best practice
2782
+
2783
+ if (layoutMode === 'compact') {
2784
+ link.innerHTML = this.renderCompact();
2785
+ } else {
2786
+ link.innerHTML = this.renderExpanded();
2787
+ }
2788
+
2789
+ // Add expand button handler
2790
+ const expandBtn = link.querySelector('.cg-expand-btn');
2791
+ if (expandBtn && this.onExpand) {
2792
+ expandBtn.addEventListener('click', (e) => {
2793
+ e.preventDefault(); // Prevent link navigation
2794
+ e.stopPropagation();
2795
+ this.onExpand(this.article, link);
2796
+ });
2797
+ }
2798
+
2799
+ return link;
2800
+ }
2801
+
2802
+ // For inline/modal mode, use regular article element
2803
+ const card = document.createElement('article');
2804
+ card.className = `cg-card cg-card--${this.displayMode}`;
2805
+ card.dataset.contentId = this.article.uuid;
2806
+
2807
+ if (layoutMode === 'compact') {
2808
+ card.innerHTML = this.renderCompact();
2809
+ } else {
2810
+ card.innerHTML = this.renderExpanded();
2811
+ }
2812
+
2813
+ // Add click handler for the whole card
2814
+ card.addEventListener('click', (e) => {
2815
+ // Don't trigger if clicking expand button
2816
+ if (e.target.closest('.cg-expand-btn')) return;
2817
+
2818
+ if (this.onClick) {
2819
+ this.onClick(this.article);
2820
+ }
2821
+ });
2822
+
2823
+ // Add expand button handler if in compact mode
2824
+ const expandBtn = card.querySelector('.cg-expand-btn');
2825
+ if (expandBtn && this.onExpand) {
2826
+ expandBtn.addEventListener('click', (e) => {
2827
+ e.stopPropagation();
2828
+ this.onExpand(this.article, card);
2829
+ });
2830
+ }
2831
+
2832
+ return card;
2833
+ }
2834
+
2835
+ /**
2836
+ * Render compact mode (title, meta only)
2837
+ */
2838
+ renderCompact() {
2839
+ const readingTime = calculateReadingTime(this.article.wordCount);
2840
+
2841
+ return `
2842
+ <div class="cg-card-header">
2843
+ <h3 class="cg-card-title">${escapeHtml(this.article.title)}</h3>
2844
+ <button class="cg-expand-btn" aria-label="Show more" title="Show summary">
2845
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
2846
+ <path d="M4 6L8 10L12 6" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
2847
+ </svg>
2848
+ </button>
2849
+ </div>
2850
+ <div class="cg-card-meta">
2851
+ <span class="cg-meta-item cg-author">
2852
+ <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
2853
+ <path d="M7 7C8.65685 7 10 5.65685 10 4C10 2.34315 8.65685 1 7 1C5.34315 1 4 2.34315 4 4C4 5.65685 5.34315 7 7 7Z" stroke="currentColor" stroke-width="1.5"/>
2854
+ <path d="M13 13C13 10.7909 10.3137 9 7 9C3.68629 9 1 10.7909 1 13" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
2855
+ </svg>
2856
+ ${escapeHtml(this.article.authorName)}
2857
+ </span>
2858
+ <span class="cg-meta-item cg-date">
2859
+ <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
2860
+ <rect x="1" y="2" width="12" height="11" rx="2" stroke="currentColor" stroke-width="1.5"/>
2861
+ <path d="M4 1V3M10 1V3M1 5H13" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
2862
+ </svg>
2863
+ ${formatDate(this.article.publishedAt)}
2864
+ </span>
2865
+ <span class="cg-meta-item cg-reading-time">
2866
+ <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
2867
+ <circle cx="7" cy="7" r="6" stroke="currentColor" stroke-width="1.5"/>
2868
+ <path d="M7 3.5V7L9.5 9.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
2869
+ </svg>
2870
+ ${readingTime}
2871
+ </span>
2872
+ </div>
2873
+ `;
2874
+ }
2875
+
2876
+ /**
2877
+ * Render expanded mode (with summary and tags)
2878
+ */
2879
+ renderExpanded() {
2880
+ const readingTime = calculateReadingTime(this.article.wordCount);
2881
+ const summaryFull = this.article.summary || '';
2882
+ const summary = this.aiSummaryMaxBytes ? truncateBytes(summaryFull, this.aiSummaryMaxBytes) : summaryFull;
2883
+ const tags = this.article.tags || [];
2884
+
2885
+ return `
2886
+ <div class="cg-card-header">
2887
+ <h3 class="cg-card-title">${escapeHtml(this.article.title)}</h3>
2888
+ <button class="cg-expand-btn cg-expand-btn--collapse" aria-label="Show less" title="Hide summary">
2889
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
2890
+ <path d="M12 10L8 6L4 10" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
2891
+ </svg>
2892
+ </button>
2893
+ </div>
2894
+
2895
+ ${summary ? `
2896
+ <div class="cg-card-summary">
2897
+ <p>${escapeHtml(summary)}</p>
2898
+ </div>
2899
+ ` : ''}
2900
+
2901
+ ${tags.length > 0 ? `
2902
+ <div class="cg-card-tags">
2903
+ ${tags.map(tag => `
2904
+ <span class="cg-tag">${escapeHtml(tag)}</span>
2905
+ `).join('')}
2906
+ </div>
2907
+ ` : ''}
2908
+
2909
+ <div class="cg-card-meta">
2910
+ <span class="cg-meta-item cg-author">
2911
+ <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
2912
+ <path d="M7 7C8.65685 7 10 5.65685 10 4C10 2.34315 8.65685 1 7 1C5.34315 1 4 2.34315 4 4C4 5.65685 5.34315 7 7 7Z" stroke="currentColor" stroke-width="1.5"/>
2913
+ <path d="M13 13C13 10.7909 10.3137 9 7 9C3.68629 9 1 10.7909 1 13" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
2914
+ </svg>
2915
+ ${escapeHtml(this.article.authorName)}
2916
+ </span>
2917
+ <span class="cg-meta-item cg-date">
2918
+ <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
2919
+ <rect x="1" y="2" width="12" height="11" rx="2" stroke="currentColor" stroke-width="1.5"/>
2920
+ <path d="M4 1V3M10 1V3M1 5H13" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
2921
+ </svg>
2922
+ ${formatDate(this.article.publishedAt)}
2923
+ </span>
2924
+ <span class="cg-meta-item cg-reading-time">
2925
+ <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
2926
+ <circle cx="7" cy="7" r="6" stroke="currentColor" stroke-width="1.5"/>
2927
+ <path d="M7 3.5V7L9.5 9.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
2928
+ </svg>
2929
+ ${readingTime}
2930
+ </span>
2931
+ </div>
2932
+ `;
2933
+ }
2934
+ }
2935
+ /**
2936
+ * Content List Component
2937
+ * Displays a list of content items with pagination
2938
+ */
2939
+
2940
+
2941
+ class ContentList {
2942
+ constructor(container, api, options = {}) {
2943
+ console.log('[ContentList] Constructor called with options:', options);
2944
+ this.container = container;
2945
+ this.api = api;
2946
+ this.options = {
2947
+ layoutMode: options.layoutMode || 'cards', // 'cards' or 'rows'
2948
+ displayMode: options.displayMode || 'comfortable',
2949
+ pageSize: parseInt(options.pageSize) || 12,
2950
+ tags: options.tags || [],
2951
+ category: options.category,
2952
+ aiSummaryMaxBytes: options.aiSummaryMaxBytes,
2953
+ viewerMode: options.viewerMode || 'inline', // 'inline' | 'modal' | 'external'
2954
+ externalUrlPattern: options.externalUrlPattern || '/article/{id}',
2955
+ externalTarget: options.externalTarget || 'article-{id}',
2956
+ onArticleClick: options.onArticleClick || null
2957
+ };
2958
+
2959
+ console.log('[ContentList] Final options:', this.options);
2960
+
2961
+ this.currentPage = 1;
2962
+ this.totalPages = 1;
2963
+ this.articles = [];
2964
+ this.loading = false;
2965
+ this.expandedCards = new Set();
2966
+ }
2967
+
2968
+ /**
2969
+ * Initialize and render the list
2970
+ */
2971
+ async init() {
2972
+ console.log('[ContentList] Initializing...');
2973
+ this.render();
2974
+ await this.loadArticles();
2975
+ console.log('[ContentList] Initialization complete');
2976
+ }
2977
+
2978
+ /**
2979
+ * Render the list container
2980
+ */
2981
+ render() {
2982
+ const layoutClass = this.options.layoutMode === 'rows' ? 'cg-content-rows' : 'cg-content-grid';
2983
+
2984
+ this.container.innerHTML = `
2985
+ <div class="cg-content-list">
2986
+ <div class="cg-list-header">
2987
+ <button class="cg-display-toggle" title="Toggle display mode">
2988
+ <svg width="20" height="20" viewBox="0 0 20 20" fill="none">
2989
+ <rect x="2" y="2" width="16" height="4" rx="1" stroke="currentColor" stroke-width="1.5"/>
2990
+ <rect x="2" y="8" width="16" height="4" rx="1" stroke="currentColor" stroke-width="1.5"/>
2991
+ <rect x="2" y="14" width="16" height="4" rx="1" stroke="currentColor" stroke-width="1.5"/>
2992
+ </svg>
2993
+ <span>${this.options.displayMode === 'compact' ? 'Show summaries' : 'Hide summaries'}</span>
2994
+ </button>
2995
+ </div>
2996
+ <div class="${layoutClass}"></div>
2997
+ <div class="cg-pagination"></div>
2998
+ </div>
2999
+ `;
3000
+
3001
+ // Add toggle handler
3002
+ const toggle = this.container.querySelector('.cg-display-toggle');
3003
+ toggle.addEventListener('click', () => this.toggleDisplayMode());
3004
+ }
3005
+
3006
+ /**
3007
+ * Load articles from API
3008
+ */
3009
+ async loadArticles(page = 1) {
3010
+ if (this.loading) {
3011
+ console.log('[ContentList] Already loading, skipping...');
3012
+ return;
3013
+ }
3014
+
3015
+ console.log('[ContentList] Loading articles for page:', page);
3016
+ this.loading = true;
3017
+ this.showLoading();
3018
+
3019
+ try {
3020
+ console.log('[ContentList] Calling api.fetchArticles with:', {
3021
+ page,
3022
+ limit: this.options.pageSize,
3023
+ tags: this.options.tags,
3024
+ category: this.options.category
3025
+ });
3026
+
3027
+ const data = await this.api.fetchArticles({
3028
+ page,
3029
+ limit: this.options.pageSize,
3030
+ tags: this.options.tags,
3031
+ category: this.options.category
3032
+ });
3033
+
3034
+ console.log('[ContentList] Received data:', data);
3035
+
3036
+ this.articles = data.articles || [];
3037
+ this.currentPage = data.pagination?.page || 1;
3038
+ this.totalPages = data.pagination?.totalPages || 1;
3039
+
3040
+ console.log('[ContentList] Loaded', this.articles.length, 'articles');
3041
+
3042
+ this.renderArticles();
3043
+ this.renderPagination();
3044
+ } catch (error) {
3045
+ console.error('[ContentList] Error loading articles:', error);
3046
+ this.showError('Failed to load articles. Please try again.');
3047
+ } finally {
3048
+ this.loading = false;
3049
+ }
3050
+ }
3051
+
3052
+ /**
3053
+ * Render content grid
3054
+ */
3055
+ renderArticles() {
3056
+ const grid = this.container.querySelector('.cg-content-grid, .cg-content-rows');
3057
+
3058
+ if (this.articles.length === 0) {
3059
+ grid.innerHTML = `
3060
+ <div class="cg-empty-state">
3061
+ <svg width="64" height="64" viewBox="0 0 64 64" fill="none">
3062
+ <rect x="8" y="12" width="48" height="40" rx="4" stroke="currentColor" stroke-width="2"/>
3063
+ <path d="M16 24H48M16 32H48M16 40H32" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
3064
+ </svg>
3065
+ <p>No articles found</p>
3066
+ </div>
3067
+ `;
3068
+ return;
3069
+ }
3070
+
3071
+ grid.innerHTML = '';
3072
+
3073
+ this.articles.forEach(article => {
3074
+ const isExpanded = this.expandedCards.has(article.uuid);
3075
+ const card = new ContentCard(article, {
3076
+ displayMode: isExpanded ? 'expanded' : this.options.displayMode,
3077
+ viewerMode: this.options.viewerMode,
3078
+ externalUrlPattern: this.options.externalUrlPattern,
3079
+ externalTarget: this.options.externalTarget,
3080
+ aiSummaryMaxBytes: this.options.aiSummaryMaxBytes,
3081
+ onExpand: (article, cardElement) => this.handleExpand(article, cardElement),
3082
+ onClick: (article) => this.handleArticleClick(article)
3083
+ });
3084
+
3085
+ grid.appendChild(card.render());
3086
+ });
3087
+ }
3088
+
3089
+ /**
3090
+ * Handle expand/collapse of a card
3091
+ */
3092
+ handleExpand(article, cardElement) {
3093
+ const isExpanded = this.expandedCards.has(article.uuid);
3094
+
3095
+ if (isExpanded) {
3096
+ this.expandedCards.delete(article.uuid);
3097
+ } else {
3098
+ this.expandedCards.add(article.uuid);
3099
+ }
3100
+
3101
+ // Re-render just this card
3102
+ const newCard = new ContentCard(article, {
3103
+ displayMode: isExpanded ? this.options.displayMode : 'expanded',
3104
+ viewerMode: this.options.viewerMode,
3105
+ externalUrlPattern: this.options.externalUrlPattern,
3106
+ externalTarget: this.options.externalTarget,
3107
+ onExpand: (article, cardElement) => this.handleExpand(article, cardElement),
3108
+ onClick: (article) => this.handleArticleClick(article)
3109
+ });
3110
+
3111
+ cardElement.replaceWith(newCard.render());
3112
+ }
3113
+
3114
+ /**
3115
+ * Handle article click
3116
+ */
3117
+ handleArticleClick(article) {
3118
+ if (this.options.onArticleClick) {
3119
+ this.options.onArticleClick(article);
3120
+ }
3121
+ }
3122
+
3123
+ /**
3124
+ * Toggle display mode for all cards
3125
+ */
3126
+ toggleDisplayMode() {
3127
+ this.options.displayMode = this.options.displayMode === 'compact' ? 'expanded' : 'compact';
3128
+ this.expandedCards.clear(); // Reset individual expansions
3129
+ this.renderArticles();
3130
+
3131
+ // Update button text
3132
+ const toggle = this.container.querySelector('.cg-display-toggle span');
3133
+ toggle.textContent = this.options.displayMode === 'compact' ? 'Show summaries' : 'Hide summaries';
3134
+ }
3135
+
3136
+ /**
3137
+ * Render pagination controls
3138
+ */
3139
+ renderPagination() {
3140
+ const pagination = this.container.querySelector('.cg-pagination');
3141
+
3142
+ if (this.totalPages <= 1) {
3143
+ pagination.innerHTML = '';
3144
+ return;
3145
+ }
3146
+
3147
+ const prevDisabled = this.currentPage === 1;
3148
+ const nextDisabled = this.currentPage === this.totalPages;
3149
+
3150
+ pagination.innerHTML = `
3151
+ <button class="cg-btn-prev" ${prevDisabled ? 'disabled' : ''}>
3152
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
3153
+ <path d="M10 12L6 8L10 4" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
3154
+ </svg>
3155
+ Previous
3156
+ </button>
3157
+ <span class="cg-page-info">Page ${this.currentPage} of ${this.totalPages}</span>
3158
+ <button class="cg-btn-next" ${nextDisabled ? 'disabled' : ''}>
3159
+ Next
3160
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
3161
+ <path d="M6 4L10 8L6 12" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
3162
+ </svg>
3163
+ </button>
3164
+ `;
3165
+
3166
+ // Add event listeners
3167
+ const prevBtn = pagination.querySelector('.cg-btn-prev');
3168
+ const nextBtn = pagination.querySelector('.cg-btn-next');
3169
+
3170
+ prevBtn.addEventListener('click', () => {
3171
+ if (this.currentPage > 1) {
3172
+ this.loadArticles(this.currentPage - 1);
3173
+ this.scrollToTop();
3174
+ }
3175
+ });
3176
+
3177
+ nextBtn.addEventListener('click', () => {
3178
+ if (this.currentPage < this.totalPages) {
3179
+ this.loadArticles(this.currentPage + 1);
3180
+ this.scrollToTop();
3181
+ }
3182
+ });
3183
+ }
3184
+
3185
+ /**
3186
+ * Show loading state
3187
+ */
3188
+ showLoading() {
3189
+ const grid = this.container.querySelector('.cg-content-grid, .cg-content-rows');
3190
+ if (grid) {
3191
+ grid.innerHTML = `
3192
+ <div class="cg-loading">
3193
+ <div class="cg-spinner"></div>
3194
+ <p>Loading articles...</p>
3195
+ </div>
3196
+ `;
3197
+ }
3198
+ }
3199
+
3200
+ /**
3201
+ * Show error message
3202
+ */
3203
+ showError(message) {
3204
+ const grid = this.container.querySelector('.cg-content-grid, .cg-content-rows');
3205
+ if (grid) {
3206
+ grid.innerHTML = `
3207
+ <div class="cg-error">
3208
+ <svg width="48" height="48" viewBox="0 0 48 48" fill="none">
3209
+ <circle cx="24" cy="24" r="20" stroke="currentColor" stroke-width="2"/>
3210
+ <path d="M24 16V26M24 32V32.5" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
3211
+ </svg>
3212
+ <p>${message}</p>
3213
+ <button class="cg-retry-btn">Try Again</button>
3214
+ </div>
3215
+ `;
3216
+
3217
+ const retryBtn = grid.querySelector('.cg-retry-btn');
3218
+ retryBtn.addEventListener('click', () => this.loadArticles(this.currentPage));
3219
+ }
3220
+ }
3221
+
3222
+ /**
3223
+ * Scroll to top of widget
3224
+ */
3225
+ scrollToTop() {
3226
+ this.container.scrollIntoView({ behavior: 'smooth', block: 'start' });
3227
+ }
3228
+ }
3229
+ /**
3230
+ * Content Viewer Component
3231
+ * Displays full content with markdown rendering
3232
+ */
3233
+
3234
+
3235
+
3236
+ class ContentViewer {
3237
+ constructor(container, api, options = {}) {
3238
+ this.container = container;
3239
+ this.api = api;
3240
+ this.options = {
3241
+ displayMode: options.displayMode || 'inline', // 'inline' or 'modal'
3242
+ showBackButton: options.showBackButton !== false, // Default true, can be disabled
3243
+ showSummary: options.showSummary !== false, // Default true, can be disabled
3244
+ onBack: options.onBack || null
3245
+ };
3246
+
3247
+ this.article = null;
3248
+ this.loading = false;
3249
+ this.summaryExpanded = true; // Summary visible by default
3250
+ }
3251
+
3252
+ /**
3253
+ * Load and display an article by UUID
3254
+ */
3255
+ async loadArticle(uuid) {
3256
+ if (this.loading) return;
3257
+
3258
+ this.loading = true;
3259
+ this.showLoading();
3260
+
3261
+ try {
3262
+ this.article = await this.api.fetchArticle(uuid);
3263
+ this.render();
3264
+ } catch (error) {
3265
+ this.showError('Failed to load article. Please try again.');
3266
+ console.error(error);
3267
+ } finally {
3268
+ this.loading = false;
3269
+ }
3270
+ }
3271
+
3272
+ /**
3273
+ * Load and display an article by slug
3274
+ */
3275
+ async loadArticleBySlug(slug) {
3276
+ if (this.loading) return;
3277
+
3278
+ this.loading = true;
3279
+ this.showLoading();
3280
+
3281
+ try {
3282
+ this.article = await this.api.fetchArticleBySlug(slug);
3283
+ this.render();
3284
+ } catch (error) {
3285
+ this.showError('Failed to load article. Please try again.');
3286
+ console.error(error);
3287
+ } finally {
3288
+ this.loading = false;
3289
+ }
3290
+ }
3291
+
3292
+ /**
3293
+ * Render the article
3294
+ */
3295
+ render() {
3296
+ if (!this.article) return;
3297
+
3298
+ const readingTime = calculateReadingTime(this.article.wordCount || this.article.content);
3299
+ const content = this.renderMarkdown(this.article.content || '');
3300
+
3301
+ this.container.innerHTML = `
3302
+ <div class="cg-content-viewer">
3303
+ ${this.options.showBackButton ? `
3304
+ <div class="cg-viewer-header">
3305
+ <button class="cg-back-btn">
3306
+ <svg width="20" height="20" viewBox="0 0 20 20" fill="none">
3307
+ <path d="M12 16L6 10L12 4" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
3308
+ </svg>
3309
+ Back to list
3310
+ </button>
3311
+ </div>
3312
+ ` : ''}
3313
+
3314
+ <article class="cg-viewer-content">
3315
+ <header class="cg-content-header">
3316
+ <h1 class="cg-content-title">${escapeHtml(this.article.title)}</h1>
3317
+
3318
+ ${this.options.showSummary && this.article.summary && this.article.category !== 'announce' ? `
3319
+ <div class="cg-ai-summary ${this.summaryExpanded ? 'expanded' : 'collapsed'}">
3320
+ <div class="cg-ai-summary-header">
3321
+ <div class="cg-ai-summary-label">
3322
+ <svg class="cg-ai-icon" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor">
3323
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z" />
3324
+ </svg>
3325
+ <span>AI Generated Summary</span>
3326
+ </div>
3327
+ <button class="cg-summary-toggle" aria-label="Toggle summary">
3328
+ <svg class="cg-chevron" width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor">
3329
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6l4 4 4-4" />
3330
+ </svg>
3331
+ </button>
3332
+ </div>
3333
+ <div class="cg-ai-summary-content">
3334
+ <p>${escapeHtml(this.article.summary)}</p>
3335
+ </div>
3336
+ </div>
3337
+ ` : ''}
3338
+
3339
+ <div class="cg-content-meta">
3340
+ <span class="cg-meta-item">
3341
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
3342
+ <path d="M8 8C9.65685 8 11 6.65685 11 5C11 3.34315 9.65685 2 8 2C6.34315 2 5 3.34315 5 5C5 6.65685 6.34315 8 8 8Z" stroke="currentColor" stroke-width="1.5"/>
3343
+ <path d="M14 14C14 11.7909 11.3137 10 8 10C4.68629 10 2 11.7909 2 14" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
3344
+ </svg>
3345
+ ${escapeHtml(this.article.authorName)}
3346
+ </span>
3347
+ <span class="cg-meta-item">
3348
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
3349
+ <rect x="2" y="3" width="12" height="11" rx="2" stroke="currentColor" stroke-width="1.5"/>
3350
+ <path d="M5 2V4M11 2V4M2 6H14" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
3351
+ </svg>
3352
+ ${formatDate(this.article.publishedAt)}
3353
+ </span>
3354
+ <span class="cg-meta-item">
3355
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
3356
+ <circle cx="8" cy="8" r="6" stroke="currentColor" stroke-width="1.5"/>
3357
+ <path d="M8 4V8L10.5 10.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
3358
+ </svg>
3359
+ ${readingTime}
3360
+ </span>
3361
+ </div>
3362
+ </header>
3363
+
3364
+ <div class="cg-content-body">
3365
+ ${content}
3366
+ </div>
3367
+ </article>
3368
+ </div>
3369
+ `;
3370
+
3371
+ // Add back button handler if button exists
3372
+ if (this.options.showBackButton) {
3373
+ const backBtn = this.container.querySelector('.cg-back-btn');
3374
+ if (backBtn) {
3375
+ backBtn.addEventListener('click', () => this.handleBack());
3376
+ }
3377
+ }
3378
+
3379
+ // Add summary toggle handler
3380
+ const summaryToggle = this.container.querySelector('.cg-summary-toggle');
3381
+ if (summaryToggle) {
3382
+ summaryToggle.addEventListener('click', () => this.toggleSummary());
3383
+ }
3384
+ }
3385
+
3386
+ /**
3387
+ * Toggle AI summary visibility
3388
+ */
3389
+ toggleSummary() {
3390
+ this.summaryExpanded = !this.summaryExpanded;
3391
+ const summaryEl = this.container.querySelector('.cg-ai-summary');
3392
+ if (summaryEl) {
3393
+ if (this.summaryExpanded) {
3394
+ summaryEl.classList.add('expanded');
3395
+ summaryEl.classList.remove('collapsed');
3396
+ } else {
3397
+ summaryEl.classList.add('collapsed');
3398
+ summaryEl.classList.remove('expanded');
3399
+ }
3400
+ }
3401
+ }
3402
+
3403
+ /**
3404
+ * Render markdown content to HTML
3405
+ */
3406
+ renderMarkdown(markdown) {
3407
+ // Configure marked
3408
+ marked.setOptions({
3409
+ breaks: true,
3410
+ gfm: true,
3411
+ headerIds: true,
3412
+ mangle: false
3413
+ });
3414
+
3415
+ try {
3416
+ return marked.parse(markdown);
3417
+ } catch (error) {
3418
+ console.error('Markdown parsing error:', error);
3419
+ return `<p>${escapeHtml(markdown)}</p>`;
3420
+ }
3421
+ }
3422
+
3423
+ /**
3424
+ * Handle back button click
3425
+ */
3426
+ handleBack() {
3427
+ if (this.options.onBack) {
3428
+ this.options.onBack();
3429
+ }
3430
+ }
3431
+
3432
+ /**
3433
+ * Show loading state
3434
+ */
3435
+ showLoading() {
3436
+ this.container.innerHTML = `
3437
+ <div class="cg-content-viewer">
3438
+ <div class="cg-viewer-loading">
3439
+ <div class="cg-spinner"></div>
3440
+ <p>Loading article...</p>
3441
+ </div>
3442
+ </div>
3443
+ `;
3444
+ }
3445
+
3446
+ /**
3447
+ * Show error message
3448
+ */
3449
+ showError(message) {
3450
+ this.container.innerHTML = `
3451
+ <div class="cg-content-viewer">
3452
+ <div class="cg-viewer-error">
3453
+ <svg width="48" height="48" viewBox="0 0 48 48" fill="none">
3454
+ <circle cx="24" cy="24" r="20" stroke="currentColor" stroke-width="2"/>
3455
+ <path d="M24 16V26M24 32V32.5" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
3456
+ </svg>
3457
+ <p>${message}</p>
3458
+ ${this.options.showBackButton ? '<button class="cg-back-btn">Back to articles</button>' : ''}
3459
+ </div>
3460
+ </div>
3461
+ `;
3462
+
3463
+ if (this.options.showBackButton) {
3464
+ const backBtn = this.container.querySelector('.cg-back-btn');
3465
+ if (backBtn) {
3466
+ backBtn.addEventListener('click', () => this.handleBack());
3467
+ }
3468
+ }
3469
+ }
3470
+
3471
+ /**
3472
+ * Clear the post view
3473
+ */
3474
+ clear() {
3475
+ this.container.innerHTML = '';
3476
+ this.article = null;
3477
+ }
3478
+ }
3479
+
3480
+ // ===== Main Widget Class =====
3481
+ /**
3482
+ * Content Growth Widget
3483
+ * Main widget class that combines list and viewer
3484
+ */
3485
+
3486
+
3487
+
3488
+
3489
+ class ContentGrowthWidget {
3490
+ // Version will be injected during build from package.json
3491
+ static version = '1.1.2';
3492
+
3493
+ constructor(container, config) {
3494
+
3495
+ this.container = typeof container === 'string'
3496
+ ? document.querySelector(container)
3497
+ : container;
3498
+
3499
+ if (!this.container) {
3500
+ throw new Error('Container not found');
3501
+ }
3502
+
3503
+ this.config = {
3504
+ apiKey: config.apiKey || config['api-key'],
3505
+ baseUrl: config.baseUrl || 'https://api.content-growth.com',
3506
+ tags: this.parseTags(config.tags),
3507
+ category: config.category,
3508
+ theme: config.theme || 'light',
3509
+ layoutMode: config.layoutMode || config['layout-mode'] || 'cards', // 'cards' or 'rows'
3510
+ displayMode: config.displayMode || config['display-mode'] || 'comfortable',
3511
+ aiSummaryMaxBytes: config.aiSummaryMaxBytes || config['ai-summary-max-bytes'],
3512
+ viewerMode: config.viewerMode || config['viewer-mode'] || 'inline', // 'inline' | 'modal' | 'external'
3513
+ externalUrlPattern: config.externalUrlPattern || config['external-url-pattern'] || '/article/{id}',
3514
+ externalTarget: config.externalTarget || config['external-target'] || 'article-{id}', // Tab name with {id}
3515
+ pageSize: config.pageSize || config['page-size'] || 12,
3516
+ mode: config.mode || 'list', // 'list' or 'article-only'
3517
+ articleId: config.articleId || config['article-id'], // Article ID for article-only mode
3518
+ slug: config.slug // Article slug for article-only mode (alternative to articleId)
3519
+ };
3520
+
3521
+ if (!this.config.apiKey) {
3522
+ throw new Error('API key is required');
3523
+ }
3524
+
3525
+ this.api = new ContentGrowthAPI({
3526
+ apiKey: this.config.apiKey,
3527
+ baseUrl: this.config.baseUrl
3528
+ });
3529
+
3530
+ this.currentView = 'list'; // 'list' or 'viewer'
3531
+ this.contentList = null;
3532
+ this.contentViewer = null;
3533
+
3534
+ this.init();
3535
+ }
3536
+
3537
+ /**
3538
+ * Initialize the widget
3539
+ */
3540
+ init() {
3541
+ // Apply theme
3542
+ this.container.classList.add('cg-widget');
3543
+ this.container.setAttribute('data-theme', this.config.theme);
3544
+
3545
+ // Check if article-only mode
3546
+ if (this.config.mode === 'article-only') {
3547
+ if (this.config.slug) {
3548
+ this.showPostInlineBySlug(this.config.slug);
3549
+ } else if (this.config.articleId) {
3550
+ this.showPostInline(this.config.articleId);
3551
+ }
3552
+ } else {
3553
+ // Create views
3554
+ this.showList();
3555
+ }
3556
+ }
3557
+
3558
+ /**
3559
+ * Show the list view
3560
+ */
3561
+ showList() {
3562
+ this.currentView = 'list';
3563
+ this.container.innerHTML = '';
3564
+
3565
+ const listContainer = document.createElement('div');
3566
+ listContainer.className = 'cg-list-view';
3567
+ this.container.appendChild(listContainer);
3568
+
3569
+ this.contentList = new ContentList(listContainer, this.api, {
3570
+ layoutMode: this.config.layoutMode,
3571
+ displayMode: this.config.displayMode,
3572
+ pageSize: this.config.pageSize,
3573
+ tags: this.config.tags,
3574
+ category: this.config.category,
3575
+ aiSummaryMaxBytes: this.config.aiSummaryMaxBytes,
3576
+ viewerMode: this.config.viewerMode,
3577
+ externalUrlPattern: this.config.externalUrlPattern,
3578
+ externalTarget: this.config.externalTarget,
3579
+ onArticleClick: (article) => this.showPost(article.uuid)
3580
+ });
3581
+
3582
+ this.contentList.init();
3583
+ }
3584
+
3585
+ /**
3586
+ * Show the content viewer
3587
+ */
3588
+ showPost(uuid) {
3589
+ this.currentView = 'viewer';
3590
+
3591
+ if (this.config.viewerMode === 'modal') {
3592
+ this.showPostModal(uuid);
3593
+ } else {
3594
+ this.showPostInline(uuid);
3595
+ }
3596
+ }
3597
+
3598
+ /**
3599
+ * Show content inline (replaces list)
3600
+ */
3601
+ showPostInline(uuid) {
3602
+ this.container.innerHTML = '';
3603
+
3604
+ const viewerContainer = document.createElement('div');
3605
+ viewerContainer.className = 'cg-viewer-view';
3606
+ this.container.appendChild(viewerContainer);
3607
+
3608
+ // In article-only mode, don't show back button (no list to go back to)
3609
+ const showBackButton = this.config.mode !== 'article-only';
3610
+
3611
+ this.contentViewer = new ContentViewer(viewerContainer, this.api, {
3612
+ displayMode: 'inline',
3613
+ showBackButton: showBackButton,
3614
+ onBack: showBackButton ? () => this.showList() : null
3615
+ });
3616
+
3617
+ this.contentViewer.loadArticle(uuid);
3618
+ }
3619
+
3620
+ /**
3621
+ * Show content inline by slug (replaces list)
3622
+ */
3623
+ showPostInlineBySlug(slug) {
3624
+ this.container.innerHTML = '';
3625
+
3626
+ const viewerContainer = document.createElement('div');
3627
+ viewerContainer.className = 'cg-viewer-view';
3628
+ this.container.appendChild(viewerContainer);
3629
+
3630
+ // In article-only mode, don't show back button (no list to go back to)
3631
+ const showBackButton = this.config.mode !== 'article-only';
3632
+
3633
+ this.contentViewer = new ContentViewer(viewerContainer, this.api, {
3634
+ displayMode: 'inline',
3635
+ showBackButton: showBackButton,
3636
+ onBack: showBackButton ? () => this.showList() : null
3637
+ });
3638
+
3639
+ this.contentViewer.loadArticleBySlug(slug);
3640
+ }
3641
+
3642
+ /**
3643
+ * Show content in modal
3644
+ */
3645
+ showPostModal(uuid) {
3646
+ // Create modal
3647
+ const modal = document.createElement('div');
3648
+ modal.className = 'cg-modal';
3649
+ // Apply theme to modal
3650
+ if (this.config.theme) {
3651
+ modal.setAttribute('data-theme', this.config.theme);
3652
+ }
3653
+ modal.innerHTML = `
3654
+ <div class="cg-modal-overlay"></div>
3655
+ <div class="cg-modal-content">
3656
+ <button class="cg-modal-close" aria-label="Close">
3657
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
3658
+ <path d="M18 6L6 18M6 6L18 18" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
3659
+ </svg>
3660
+ </button>
3661
+ <div class="cg-modal-body"></div>
3662
+ </div>
3663
+ `;
3664
+
3665
+ document.body.appendChild(modal);
3666
+
3667
+ // Prevent body scroll
3668
+ document.body.style.overflow = 'hidden';
3669
+
3670
+ // Load content
3671
+ const modalBody = modal.querySelector('.cg-modal-body');
3672
+ this.contentViewer = new ContentViewer(modalBody, this.api, {
3673
+ displayMode: 'modal',
3674
+ onBack: () => this.closeModal(modal)
3675
+ });
3676
+
3677
+ this.contentViewer.loadArticle(uuid);
3678
+
3679
+ // Close handlers
3680
+ const closeBtn = modal.querySelector('.cg-modal-close');
3681
+ const overlay = modal.querySelector('.cg-modal-overlay');
3682
+
3683
+ const closeModal = () => this.closeModal(modal);
3684
+ closeBtn.addEventListener('click', closeModal);
3685
+ overlay.addEventListener('click', closeModal);
3686
+
3687
+ // ESC key
3688
+ const handleEsc = (e) => {
3689
+ if (e.key === 'Escape') {
3690
+ closeModal();
3691
+ document.removeEventListener('keydown', handleEsc);
3692
+ }
3693
+ };
3694
+ document.addEventListener('keydown', handleEsc);
3695
+
3696
+ // Fade in
3697
+ requestAnimationFrame(() => {
3698
+ modal.classList.add('cg-modal--active');
3699
+ });
3700
+ }
3701
+
3702
+ /**
3703
+ * Close modal
3704
+ */
3705
+ closeModal(modal) {
3706
+ modal.classList.remove('cg-modal--active');
3707
+
3708
+ setTimeout(() => {
3709
+ modal.remove();
3710
+ document.body.style.overflow = '';
3711
+ }, 300); // Match CSS transition duration
3712
+ }
3713
+
3714
+ /**
3715
+ * Parse tags from string or array
3716
+ */
3717
+ parseTags(tags) {
3718
+ if (!tags) return [];
3719
+ if (Array.isArray(tags)) return tags;
3720
+ return tags.split(',').map(t => t.trim()).filter(Boolean);
3721
+ }
3722
+
3723
+ /**
3724
+ * Update configuration
3725
+ */
3726
+ updateConfig(newConfig) {
3727
+ Object.assign(this.config, newConfig);
3728
+ this.init();
3729
+ }
3730
+
3731
+ /**
3732
+ * Destroy the widget
3733
+ */
3734
+ destroy() {
3735
+ this.container.innerHTML = '';
3736
+ this.container.classList.remove('cg-widget');
3737
+ this.container.removeAttribute('data-theme');
3738
+ }
3739
+ }
3740
+
3741
+ // ===== Auto-initialization =====
3742
+ /**
3743
+ * Content Growth Widget - Entry Point
3744
+ * Auto-initializes widgets on page load
3745
+ */
3746
+
3747
+
3748
+ // Note: CSS import is commented out for non-bundled usage
3749
+ // In production, this will be bundled and the CSS will be included
3750
+ // For testing, include <link rel="stylesheet" href="./styles/widget.css"> in HTML
3751
+ //
3752
+
3753
+ // Auto-initialize widgets
3754
+ function initWidgets() {
3755
+ const containers = document.querySelectorAll('[data-cg-content]');
3756
+
3757
+ containers.forEach((container, index) => {
3758
+
3759
+ const config = {
3760
+ apiKey: container.dataset.apiKey || container.dataset.cgApiKey,
3761
+ baseUrl: window.WIDGET_BASE_URL || container.dataset.baseUrl || container.dataset.cgBaseUrl,
3762
+ tags: container.dataset.tags || container.dataset.cgTags,
3763
+ theme: container.dataset.theme || container.dataset.cgTheme || 'light',
3764
+ layoutMode: container.dataset.layoutMode || container.dataset.cgLayoutMode || 'cards',
3765
+ displayMode: container.dataset.displayMode || container.dataset.cgDisplayMode || 'comfortable',
3766
+ viewerMode: container.dataset.viewerMode || container.dataset.cgViewerMode || 'inline',
3767
+ externalUrlPattern: container.dataset.externalUrlPattern || container.dataset.cgExternalUrlPattern,
3768
+ externalTarget: container.dataset.externalTarget || container.dataset.cgExternalTarget,
3769
+ pageSize: container.dataset.pageSize || container.dataset.cgPageSize || 12,
3770
+ mode: container.dataset.mode || container.dataset.cgMode || 'list',
3771
+ articleId: container.dataset.articleId || container.dataset.cgArticleId
3772
+ };
3773
+
3774
+ try {
3775
+ new ContentGrowthWidget(container, config);
3776
+ } catch (error) {
3777
+ console.error(`[Widget ${index}] Failed to initialize:`, error);
3778
+ container.innerHTML = `
3779
+ <div style="padding: 2rem; text-align: center; color: #ef4444;">
3780
+ <p>Failed to load widget: ${error.message}</p>
3781
+ <p style="font-size: 0.875rem; margin-top: 0.5rem;">Check console for details</p>
3782
+ </div>
3783
+ `;
3784
+ }
3785
+ });
3786
+ }
3787
+
3788
+ // Initialize on DOM ready
3789
+ if (document.readyState === 'loading') {
3790
+ document.addEventListener('DOMContentLoaded', initWidgets);
3791
+ } else {
3792
+ initWidgets();
3793
+ }
3794
+
3795
+ // Export for manual initialization
3796
+ window.ContentGrowthWidget = ContentGrowthWidget;
3797
+
3798
+ { ContentGrowthWidget };
3799
+
3800
+ // ===== Expose to window =====
3801
+ window.ContentGrowthWidget = ContentGrowthWidget;
3802
+
3803
+ console.log('[ContentGrowthWidget] Loaded successfully v1.1.2');
3804
+
3805
+ })(window);