@meandr/chat-sdk 0.0.0-stage → 0.0.1

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,927 @@
1
+ // src/markdown.ts
2
+ var MAX_DEPTH = 8;
3
+ var MAX_INLINE = 16;
4
+ var MAX_COLS = 64;
5
+ var ASCII_PUNCT = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
6
+ var PUNCT = /[\p{P}\p{S}]/u;
7
+ var SPACE = /\s/;
8
+ var LANG = /^[A-Za-z0-9_+-]+$/;
9
+ var ALNUM = /[A-Za-z0-9]/;
10
+ var HEX = /[0-9A-Fa-f]/;
11
+ var DEC = /[0-9]/;
12
+ var MAX_LABEL = 120;
13
+ var MAX_SHOWN = 200;
14
+ var ELLIPSIS = String.fromCharCode(8230);
15
+ var FILLERS = String.fromCharCode(10240, 12644, 4447, 4448, 65440);
16
+ var BLANK = new RegExp("[\\s\\p{Default_Ignorable_Code_Point}" + FILLERS + "]", "u");
17
+ var HIDDEN = new RegExp("[\\s\\p{Cc}\\p{Default_Ignorable_Code_Point}" + FILLERS + "]", "gu");
18
+ var ZWNJ = String.fromCharCode(8204);
19
+ var ZWJ = String.fromCharCode(8205);
20
+ var VS = new RegExp("[" + String.fromCharCode(65024) + "-" + String.fromCharCode(65039) + String.fromCodePoint(917760) + "-" + String.fromCodePoint(917999) + "]", "u");
21
+ var HOSTNAME = /^(?:[a-z0-9.-]+|\[[0-9a-f:.]+\])$/;
22
+ var BIDI = new RegExp("[" + String.fromCharCode(8234) + "-" + String.fromCharCode(8238) + String.fromCharCode(8294) + "-" + String.fromCharCode(8297) + "]", "g");
23
+ var NAMED = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
24
+ var MORE = "nbsp a0 mdash 2014 ndash 2013 hellip 2026 copy a9 reg ae trade 2122 laquo ab raquo bb lsquo 2018 rsquo 2019 ldquo 201c rdquo 201d times d7 euro 20ac pound a3 yen a5 deg b0 middot b7 bull 2022".split(" ");
25
+ for (let k = 0; k < MORE.length; k += 2) NAMED[MORE[k]] = String.fromCharCode(parseInt(MORE[k + 1], 16));
26
+ var TEXT = 0;
27
+ var DELIM = 1;
28
+ var EL = 2;
29
+ var BR = 3;
30
+ var CODE = 4;
31
+ var NOTE = 5;
32
+ function parseMarkdown(text) {
33
+ const lines = String(text == null ? "" : text).replace(/\r\n?/g, "\n").split("\n").map(detab);
34
+ return blockTree(parseBlocks(lines, 0).out, false);
35
+ }
36
+ function renderMarkdown(text, doc = document) {
37
+ const frag = doc.createDocumentFragment();
38
+ build(parseMarkdown(text), frag, doc);
39
+ return frag;
40
+ }
41
+ function parseBlocks(lines, depth) {
42
+ const out = [];
43
+ const nest = depth < MAX_DEPTH;
44
+ const n = lines.length;
45
+ let loose = false, gap = false, i = 0;
46
+ while (i < n) {
47
+ const line = lines[i];
48
+ if (isBlank(line)) {
49
+ gap = out.length > 0;
50
+ i++;
51
+ continue;
52
+ }
53
+ if (gap) loose = true;
54
+ gap = false;
55
+ let f, h, li, align;
56
+ if (f = fenceOpen(line)) {
57
+ const code = [];
58
+ for (i++; i < n && !fenceClose(lines[i], f); i++) code.push(dedent(lines[i], f.ind));
59
+ i++;
60
+ const lang = f.info.split(/\s/)[0];
61
+ out.push({ t: "pre", s: code.join("\n"), lang: LANG.test(lang) ? lang : "" });
62
+ } else if (h = heading(line)) {
63
+ out.push({ t: "h" + h.level, s: h.text });
64
+ i++;
65
+ } else if (isHr(line)) {
66
+ out.push({ t: "hr", s: "" });
67
+ i++;
68
+ } else if (nest && quoteStrip(line) !== null) {
69
+ const inner = [];
70
+ let fs = null;
71
+ while (i < n) {
72
+ const q = quoteStrip(lines[i]);
73
+ if (q === null && (isBlank(lines[i]) || fs || isBlank(inner[inner.length - 1]) || startsBlock(lines[i], nest))) break;
74
+ const l = q === null ? lines[i] : q;
75
+ inner.push(l);
76
+ fs = fenceStep(fs, l);
77
+ i++;
78
+ }
79
+ out.push({ t: "blockquote", s: "", kids: parseBlocks(inner, depth + 1).out });
80
+ } else if (nest && (li = listItem(line))) {
81
+ const first = li, items = [];
82
+ let cur = li, lz = false;
83
+ for (; ; ) {
84
+ const body = [cur.content];
85
+ let fs = fenceStep(null, cur.content), blank = -1;
86
+ for (i++; i < n; i++) {
87
+ const l = lines[i];
88
+ if (isBlank(l)) {
89
+ if (blank < 0) blank = i;
90
+ continue;
91
+ }
92
+ if (indentOf(l) >= cur.indent) {
93
+ for (; blank >= 0 && blank < i; blank++) body.push("");
94
+ blank = -1;
95
+ const d = dedent(l, cur.indent);
96
+ body.push(d);
97
+ fs = fenceStep(fs, d);
98
+ continue;
99
+ }
100
+ if (blank >= 0 || fs || isBlank(body[body.length - 1]) || listItem(l) || startsBlock(l, nest)) break;
101
+ body.push(l);
102
+ }
103
+ if (blank >= 0) i = blank;
104
+ const r = parseBlocks(body, depth + 1);
105
+ items.push(r.out);
106
+ if (r.loose) lz = true;
107
+ let j = i;
108
+ while (j < n && isBlank(lines[j])) j++;
109
+ const next = j < n ? listItem(lines[j]) : null;
110
+ if (!next || next.ordered !== first.ordered || next.mark !== first.mark) break;
111
+ if (j > i) lz = true;
112
+ i = j;
113
+ cur = next;
114
+ }
115
+ out.push({ t: first.ordered ? "ol" : "ul", s: "", start: first.start, tight: !lz, items });
116
+ } else if (align = tableAt(lines, i)) {
117
+ const rows = [cells(line, MAX_COLS)];
118
+ for (i += 2; i < n && !isBlank(lines[i]) && !startsBlock(lines[i], nest); i++) rows.push(cells(lines[i], MAX_COLS));
119
+ out.push({ t: "table", s: "", rows, align: align.slice(0, MAX_COLS) });
120
+ } else {
121
+ const buf = [line.trim()];
122
+ for (i++; i < n && !isBlank(lines[i]) && !startsBlock(lines[i], nest) && !tableAt(lines, i); i++) buf.push(lines[i].trim());
123
+ out.push({ t: "p", s: buf.join("\n") });
124
+ }
125
+ }
126
+ return { out, loose };
127
+ }
128
+ function isBlank(l) {
129
+ if (l === void 0) return true;
130
+ for (let k = 0; k < l.length; k++) {
131
+ const c = l.charCodeAt(k);
132
+ if (c !== 32 && c !== 9) return false;
133
+ }
134
+ return true;
135
+ }
136
+ function indentOf(l) {
137
+ let k = 0;
138
+ while (l.charCodeAt(k) === 32) k++;
139
+ return k;
140
+ }
141
+ function lead(l) {
142
+ let k = 0;
143
+ while (k < 3 && l.charCodeAt(k) === 32) k++;
144
+ return k;
145
+ }
146
+ function dedent(l, k) {
147
+ let p = 0;
148
+ while (p < k && l.charCodeAt(p) === 32) p++;
149
+ return l.slice(p);
150
+ }
151
+ function detab(l) {
152
+ let k = 0, col = 0, tab = false;
153
+ for (; k < l.length; k++) {
154
+ const c = l.charCodeAt(k);
155
+ if (c === 9) {
156
+ col += 4 - col % 4;
157
+ tab = true;
158
+ } else if (c === 32) col++;
159
+ else break;
160
+ }
161
+ return tab ? " ".repeat(col) + l.slice(k) : l;
162
+ }
163
+ function fenceOpen(l) {
164
+ const ind = lead(l), ch = l.charAt(ind);
165
+ if (ch !== "`" && ch !== "~") return null;
166
+ let e = ind;
167
+ while (l.charAt(e) === ch) e++;
168
+ const info = l.slice(e).trim();
169
+ return e - ind >= 3 && !(ch === "`" && info.indexOf("`") >= 0) ? { ch, len: e - ind, ind, info } : null;
170
+ }
171
+ function fenceClose(l, f) {
172
+ const s = lead(l);
173
+ let e = s;
174
+ while (l.charAt(e) === f.ch) e++;
175
+ return e - s >= f.len && isBlank(l.slice(e));
176
+ }
177
+ function fenceStep(f, l) {
178
+ return f ? fenceClose(l, f) ? null : f : fenceOpen(l);
179
+ }
180
+ function heading(l) {
181
+ const s = lead(l);
182
+ let e = s;
183
+ while (e - s < 7 && l.charAt(e) === "#") e++;
184
+ const level = e - s, c = l.charAt(e);
185
+ if (level < 1 || level > 6 || c !== "" && c !== " " && c !== " ") return null;
186
+ let t = l.slice(e).trim(), k = t.length;
187
+ while (k > 0 && t.charAt(k - 1) === "#") k--;
188
+ if (k === 0 || t.charAt(k - 1) === " " || t.charAt(k - 1) === " ") t = t.slice(0, k).trim();
189
+ return { level, text: t };
190
+ }
191
+ function isHr(l) {
192
+ const s = lead(l), ch = l.charAt(s);
193
+ if (ch !== "-" && ch !== "*" && ch !== "_") return false;
194
+ let count = 0;
195
+ for (let k = s; k < l.length; k++) {
196
+ const c = l.charAt(k);
197
+ if (c === ch) count++;
198
+ else if (c !== " " && c !== " ") return false;
199
+ }
200
+ return count >= 3;
201
+ }
202
+ function quoteStrip(l) {
203
+ let s = lead(l);
204
+ if (l.charAt(s) !== ">") return null;
205
+ s++;
206
+ if (l.charAt(s) === " " || l.charAt(s) === " ") s++;
207
+ return detab(l.slice(s));
208
+ }
209
+ function listItem(l) {
210
+ let p = lead(l), start = 1;
211
+ const c = l.charAt(p), ordered = c >= "0" && c <= "9";
212
+ let mark = c;
213
+ if (ordered) {
214
+ let e2 = p;
215
+ while (e2 - p < 9 && l.charAt(e2) >= "0" && l.charAt(e2) <= "9") e2++;
216
+ mark = l.charAt(e2);
217
+ if (mark !== "." && mark !== ")") return null;
218
+ start = parseInt(l.slice(p, e2), 10);
219
+ p = e2;
220
+ } else if (c !== "-" && c !== "*" && c !== "+") return null;
221
+ p++;
222
+ let e = p;
223
+ while (l.charAt(e) === " " || l.charAt(e) === " ") e++;
224
+ if (e === p && e < l.length) return null;
225
+ const empty = e >= l.length, wide = e - p > 4;
226
+ return { ordered, mark, start, indent: empty || wide ? p + 1 : e, content: empty ? "" : l.slice(wide ? p + 1 : e), empty };
227
+ }
228
+ function startsBlock(l, nest) {
229
+ if (fenceOpen(l) || heading(l) || isHr(l)) return true;
230
+ if (!nest) return false;
231
+ if (quoteStrip(l) !== null) return true;
232
+ const m = listItem(l);
233
+ return m !== null && !m.empty && (!m.ordered || m.start === 1);
234
+ }
235
+ function cells(l, max) {
236
+ const s = l.trim(), out = [];
237
+ let k = s.charAt(0) === "|" ? 1 : 0, end = s.length, cur = "";
238
+ if (end > k && s.charAt(end - 1) === "|") {
239
+ let bs = 0;
240
+ while (s.charAt(end - 2 - bs) === "\\") bs++;
241
+ if (bs % 2 === 0) end--;
242
+ }
243
+ for (; k < end; k++) {
244
+ const c = s.charAt(k);
245
+ if (c === "\\" && k + 1 < end) {
246
+ const d = s.charAt(++k);
247
+ cur += d === "|" ? d : c + d;
248
+ } else if (c === "|" && out.length < max - 1) {
249
+ out.push(cur.trim());
250
+ cur = "";
251
+ } else cur += c;
252
+ }
253
+ out.push(cur.trim());
254
+ return out;
255
+ }
256
+ function delimRow(l) {
257
+ if (l.indexOf("|") < 0 || l.indexOf("-") < 0) return null;
258
+ const out = [];
259
+ for (const c of cells(l, Infinity)) {
260
+ if (!/^:?-+:?$/.test(c)) return null;
261
+ const left = c.charAt(0) === ":", right = c.charAt(c.length - 1) === ":";
262
+ out.push(left && right ? "center" : right ? "right" : left ? "left" : "");
263
+ }
264
+ return out;
265
+ }
266
+ function tableAt(lines, i) {
267
+ const align = i + 1 < lines.length ? delimRow(lines[i + 1]) : null;
268
+ return align && lines[i].indexOf("|") >= 0 && cells(lines[i], Infinity).length === align.length ? align : null;
269
+ }
270
+ function mk(k, s) {
271
+ return { k, s, href: "", dest: "", kid: null, prev: null, next: null };
272
+ }
273
+ function anchor(href, kid) {
274
+ const a = mk(EL, "a");
275
+ a.href = href;
276
+ a.kid = kid;
277
+ return a;
278
+ }
279
+ function isAsciiPunct(ch) {
280
+ return ch !== "" && ASCII_PUNCT.indexOf(ch) >= 0;
281
+ }
282
+ function special(c) {
283
+ switch (c) {
284
+ case 10:
285
+ case 33:
286
+ case 38:
287
+ case 42:
288
+ case 60:
289
+ case 72:
290
+ case 91:
291
+ case 92:
292
+ case 93:
293
+ case 95:
294
+ case 96:
295
+ case 104:
296
+ case 126:
297
+ return true;
298
+ }
299
+ return false;
300
+ }
301
+ function parseInline(src) {
302
+ const head = mk(EL, ""), len = src.length;
303
+ let tail = head, dtop = null, btop = null, ids = 0, i = 0, rp = 0, url = -1;
304
+ const push = (x) => {
305
+ tail.next = x;
306
+ x.prev = tail;
307
+ tail = x;
308
+ };
309
+ const text = (s) => {
310
+ if (tail.k === TEXT) tail.s += s;
311
+ else push(mk(TEXT, s));
312
+ };
313
+ const unlink = (x) => {
314
+ x.prev.next = x.next;
315
+ if (x.next) x.next.prev = x.prev;
316
+ else tail = x.prev;
317
+ };
318
+ const drop = (d) => {
319
+ d.node.s = d.node.s.slice(0, d.n);
320
+ if (d.prev) d.prev.next = d.next;
321
+ if (d.next) d.next.prev = d.prev;
322
+ else dtop = d.prev;
323
+ };
324
+ const emphasis = (bottom) => {
325
+ const bid = bottom ? bottom.id : -1, floor = {};
326
+ let c = dtop;
327
+ if (!c || c.id <= bid) return;
328
+ while (c.prev && c.prev.id > bid) c = c.prev;
329
+ while (c) {
330
+ if (!c.close) {
331
+ c = c.next;
332
+ continue;
333
+ }
334
+ const key = c.ch + (c.open ? "1" : "0") + c.orig % 3;
335
+ const lim = Math.max(bid, key in floor ? floor[key] : -1);
336
+ let o = c.prev;
337
+ while (o && o.id > lim && !(o.ch === c.ch && o.open && pairs(o, c))) o = o.prev;
338
+ if (o && o.id > lim) {
339
+ const use = c.ch === "~" || c.n > 1 && o.n > 1 ? 2 : 1;
340
+ o.n -= use;
341
+ c.n -= use;
342
+ const el = mk(EL, c.ch === "~" ? "del" : use === 2 ? "strong" : "em");
343
+ if (o.node.next !== c.node) {
344
+ const first = o.node.next, last = c.node.prev;
345
+ first.prev = null;
346
+ last.next = null;
347
+ el.kid = first;
348
+ }
349
+ o.node.next = el;
350
+ el.prev = o.node;
351
+ el.next = c.node;
352
+ c.node.prev = el;
353
+ for (let d = o.next; d && d !== c; d = d.next) d.node.s = d.node.s.slice(0, d.n);
354
+ o.next = c;
355
+ c.prev = o;
356
+ if (!o.n) {
357
+ unlink(o.node);
358
+ drop(o);
359
+ }
360
+ if (!c.n) {
361
+ unlink(c.node);
362
+ const nx = c.next;
363
+ drop(c);
364
+ c = nx;
365
+ }
366
+ } else {
367
+ floor[key] = c.prev ? c.prev.id : -1;
368
+ const nx = c.next;
369
+ if (!c.open) drop(c);
370
+ c = nx;
371
+ }
372
+ }
373
+ while (dtop && dtop.id > bid) drop(dtop);
374
+ };
375
+ const runAt = [], runLen = [], nextSame = [];
376
+ if (src.indexOf("`") >= 0) {
377
+ for (let k = 0; k < len; ) {
378
+ if (src.charCodeAt(k) !== 96) {
379
+ k++;
380
+ continue;
381
+ }
382
+ let e = k;
383
+ while (src.charCodeAt(e) === 96) e++;
384
+ runAt.push(k);
385
+ runLen.push(e - k);
386
+ k = e;
387
+ }
388
+ const last = {};
389
+ for (let r = runAt.length - 1; r >= 0; r--) {
390
+ nextSame[r] = runLen[r] in last ? last[runLen[r]] : -1;
391
+ last[runLen[r]] = r;
392
+ }
393
+ }
394
+ while (i < len) {
395
+ const c = src.charCodeAt(i);
396
+ if (c === 92) {
397
+ const d = src.charAt(i + 1);
398
+ if (d === "\n") {
399
+ push(mk(BR, ""));
400
+ i += 2;
401
+ } else if (isAsciiPunct(d)) {
402
+ let e = i + 2;
403
+ if (d === "`") while (src.charCodeAt(e) === 96) e++;
404
+ text(src.slice(i + 1, e));
405
+ i = e;
406
+ } else {
407
+ text("\\");
408
+ i++;
409
+ }
410
+ } else if (c === 96) {
411
+ while (rp < runAt.length && runAt[rp] < i) rp++;
412
+ if (runAt[rp] !== i) {
413
+ text("`");
414
+ i++;
415
+ continue;
416
+ }
417
+ const n = runLen[rp], cl = nextSame[rp];
418
+ if (cl < 0) {
419
+ text(src.slice(i, i + n));
420
+ i += n;
421
+ continue;
422
+ }
423
+ let code = src.slice(i + n, runAt[cl]).replace(/\n/g, " ");
424
+ if (code.length > 1 && code.charAt(0) === " " && code.charAt(code.length - 1) === " " && /[^ ]/.test(code)) code = code.slice(1, -1);
425
+ push(mk(CODE, code));
426
+ i = runAt[cl] + n;
427
+ } else if (c === 42 || c === 95 || c === 126) {
428
+ let e = i + 1;
429
+ while (src.charCodeAt(e) === c) e++;
430
+ const b = src.charAt(i - 1), a = src.charAt(e);
431
+ const bw = b === "" || SPACE.test(b), aw = a === "" || SPACE.test(a), bp = PUNCT.test(b), ap = PUNCT.test(a);
432
+ const left = !aw && (!ap || bw || bp), right = !bw && (!bp || aw || ap);
433
+ const open = c === 95 ? left && (!right || bp) : left;
434
+ const close = c === 95 ? right && (!left || ap) : right;
435
+ const x = mk(DELIM, src.slice(i, e));
436
+ push(x);
437
+ if ((open || close) && (c !== 126 || e - i === 2)) {
438
+ const d = { node: x, ch: src.charAt(i), n: e - i, orig: e - i, open, close, id: ids++, prev: dtop, next: null };
439
+ if (dtop) dtop.next = d;
440
+ dtop = d;
441
+ }
442
+ i = e;
443
+ } else if (c === 91 || c === 33 && src.charCodeAt(i + 1) === 91) {
444
+ const img = c === 33, x = mk(DELIM, img ? "![" : "[");
445
+ push(x);
446
+ btop = { node: x, img, active: true, inner: false, delim: dtop, prev: btop };
447
+ i += img ? 2 : 1;
448
+ } else if (c === 93) {
449
+ const b = btop, L = b && b.active ? linkTail(src, i + 1) : null;
450
+ if (b) btop = b.prev;
451
+ if (!b || !L) {
452
+ if (b && b.inner && btop) btop.inner = true;
453
+ text("]");
454
+ i++;
455
+ continue;
456
+ }
457
+ emphasis(b.delim);
458
+ const kids = b.node.next, href = safeHref(L.dest);
459
+ if (b.img || href) {
460
+ let label = !b.img && kids ? kids : mk(TEXT, b.img && !(href && b.inner) ? plain(kids) : "");
461
+ if (href) tidy(label);
462
+ if (!plain(label)) label = mk(TEXT, href ? shown(href) : L.dest);
463
+ label.prev = null;
464
+ b.node.next = null;
465
+ tail = b.node;
466
+ if (href) {
467
+ const a = anchor(href, label);
468
+ let p = b.node.prev;
469
+ a.dest = note(plain(label), href);
470
+ if (a.dest) {
471
+ const n = mk(NOTE, "[" + a.dest + "] ");
472
+ p.next = n;
473
+ n.prev = p;
474
+ p = n;
475
+ }
476
+ p.next = a;
477
+ a.prev = p;
478
+ tail = a;
479
+ } else {
480
+ b.node.k = TEXT;
481
+ b.node.s = label.s;
482
+ }
483
+ } else unlink(b.node);
484
+ if (!b.img) for (let x = btop; x && x.active; x = x.prev) x.active = false;
485
+ if (btop && (b.img || href || b.inner)) btop.inner = true;
486
+ i = L.end;
487
+ } else if (c === 60) {
488
+ const al = autolink(src, i);
489
+ if (!al) {
490
+ text("<");
491
+ i++;
492
+ continue;
493
+ }
494
+ if (al.href) {
495
+ push(anchor(al.href, mk(TEXT, shown(al.href))));
496
+ if (btop) btop.inner = true;
497
+ } else text(al.text);
498
+ i = al.end;
499
+ } else if (c === 38) {
500
+ const en = entity(src, i);
501
+ if (en) {
502
+ text(en[0]);
503
+ i = en[1];
504
+ } else {
505
+ text("&");
506
+ i++;
507
+ }
508
+ } else if (c === 10) {
509
+ push(mk(BR, ""));
510
+ i++;
511
+ while (src.charCodeAt(i) === 32) i++;
512
+ } else if ((c === 104 || c === 72) && (url = bareUrl(src, i, btop !== null)) > i) {
513
+ const u = src.slice(i, url), href = safeHref(u);
514
+ if (href) {
515
+ push(anchor(href, mk(TEXT, shown(href))));
516
+ if (btop) btop.inner = true;
517
+ } else text(u);
518
+ i = url;
519
+ } else {
520
+ let e = i + 1;
521
+ while (e < len && !special(src.charCodeAt(e))) e++;
522
+ text(src.slice(i, e));
523
+ i = e;
524
+ }
525
+ }
526
+ emphasis(null);
527
+ return head.next;
528
+ }
529
+ function pairs(o, c) {
530
+ return c.ch === "~" || !((o.close || c.open) && (o.orig + c.orig) % 3 === 0 && (o.orig % 3 !== 0 || c.orig % 3 !== 0));
531
+ }
532
+ function plain(n) {
533
+ let s = "";
534
+ const resume = [];
535
+ for (; ; ) {
536
+ if (!n) {
537
+ if (!resume.length) return s;
538
+ n = resume.pop();
539
+ continue;
540
+ }
541
+ if (n.k === EL) {
542
+ resume.push(n.next);
543
+ n = n.kid;
544
+ continue;
545
+ }
546
+ if (n.k !== NOTE) s += n.k === BR ? " " : n.s;
547
+ n = n.next;
548
+ }
549
+ }
550
+ function skipWs(s, p, lim) {
551
+ while (p < lim && (s.charAt(p) === " " || s.charAt(p) === " " || s.charAt(p) === "\n")) p++;
552
+ return p;
553
+ }
554
+ function linkTail(s, p) {
555
+ if (s.charAt(p) !== "(") return null;
556
+ const lim = Math.min(s.length, p + 4096);
557
+ p = skipWs(s, p + 1, lim);
558
+ let dest;
559
+ if (s.charAt(p) === "<") {
560
+ const q = ++p;
561
+ for (; p < lim; p++) {
562
+ const c = s.charAt(p);
563
+ if (c === "\\") p++;
564
+ else if (c === ">" || c === "<" || c === "\n") break;
565
+ }
566
+ if (s.charAt(p) !== ">") return null;
567
+ dest = s.slice(q, p++);
568
+ } else {
569
+ const q = p;
570
+ let depth = 0;
571
+ for (; p < lim; p++) {
572
+ const c = s.charCodeAt(p);
573
+ if (c === 92 && isAsciiPunct(s.charAt(p + 1))) p++;
574
+ else if (c <= 32) break;
575
+ else if (c === 40) {
576
+ if (++depth > 32) return null;
577
+ } else if (c === 41) {
578
+ if (!depth) break;
579
+ depth--;
580
+ }
581
+ }
582
+ if (depth || p >= lim) return null;
583
+ dest = s.slice(q, p);
584
+ }
585
+ const p0 = p;
586
+ p = skipWs(s, p, lim);
587
+ const t = s.charAt(p);
588
+ if (p > p0 && (t === '"' || t === "'" || t === "(")) {
589
+ const close = t === "(" ? ")" : t;
590
+ for (p++; p < lim; p++) {
591
+ const c = s.charAt(p);
592
+ if (c === "\\") p++;
593
+ else if (c === close) break;
594
+ else if (t === "(" && c === "(") return null;
595
+ }
596
+ if (p >= lim) return null;
597
+ p = skipWs(s, p + 1, lim);
598
+ }
599
+ if (s.charAt(p) !== ")") return null;
600
+ return { dest: decode(dest), end: p + 1 };
601
+ }
602
+ function autolink(s, p) {
603
+ const lim = Math.min(s.length, p + 2048);
604
+ let q = p + 1;
605
+ while (q < lim) {
606
+ const c = s.charCodeAt(q);
607
+ if (c <= 32 || c === 60 || c === 62) break;
608
+ q++;
609
+ }
610
+ if (s.charAt(q) !== ">" || q === p + 1) return null;
611
+ const t = s.slice(p + 1, q);
612
+ if (/^[A-Za-z][A-Za-z0-9+.-]{1,31}:/.test(t)) return { text: t, href: safeHref(t), end: q + 1 };
613
+ if (/^[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*$/.test(t)) return { text: t, href: safeHref("mailto:" + t), end: q + 1 };
614
+ return null;
615
+ }
616
+ function bareUrl(s, p, inBracket) {
617
+ const b = s.charAt(p - 1);
618
+ if (b !== "" && !SPACE.test(b) && "*_~(".indexOf(b) < 0) return -1;
619
+ if (b === "(" && s.charAt(p - 2) === "]") return -1;
620
+ const h = s.slice(p, p + 8).toLowerCase();
621
+ const st = h.indexOf("https://") === 0 ? p + 8 : h.indexOf("http://") === 0 ? p + 7 : -1;
622
+ if (st < 0) return -1;
623
+ let e = st, opens = 0, closes = 0;
624
+ for (; e < s.length; e++) {
625
+ const c = s.charCodeAt(e);
626
+ if (c <= 32 || c === 60 || c === 160 || inBracket && c === 93) break;
627
+ if (c === 40) opens++;
628
+ else if (c === 41) closes++;
629
+ }
630
+ for (; ; ) {
631
+ const c = s.charAt(e - 1);
632
+ if (`?!.,:*_~'"`.indexOf(c) >= 0) e--;
633
+ else if (c === ")" && closes > opens) {
634
+ e--;
635
+ closes--;
636
+ } else if (c === ";") {
637
+ let k = e - 2;
638
+ while (k > st && ALNUM.test(s.charAt(k))) k--;
639
+ if (s.charAt(k) !== "&" || k === e - 2) break;
640
+ e = k;
641
+ } else break;
642
+ }
643
+ return e > st ? e : -1;
644
+ }
645
+ function entity(s, p) {
646
+ let q = p + 1;
647
+ if (s.charAt(q) === "#") {
648
+ const hex = s.charAt(q + 1) === "x" || s.charAt(q + 1) === "X", digit = hex ? HEX : DEC, st = q += hex ? 2 : 1;
649
+ while (q - st < (hex ? 6 : 7) && digit.test(s.charAt(q))) q++;
650
+ if (q === st || s.charAt(q) !== ";") return null;
651
+ let cp = parseInt(s.slice(st, q), hex ? 16 : 10);
652
+ if (!cp || cp > 1114111 || cp >= 55296 && cp <= 57343) cp = 65533;
653
+ return [String.fromCodePoint(cp), q + 1];
654
+ }
655
+ while (q - p < 10 && ALNUM.test(s.charAt(q))) q++;
656
+ const name = s.slice(p + 1, q);
657
+ return s.charAt(q) === ";" && Object.prototype.hasOwnProperty.call(NAMED, name) ? [NAMED[name], q + 1] : null;
658
+ }
659
+ function decode(s) {
660
+ if (s.indexOf("\\") < 0 && s.indexOf("&") < 0) return s;
661
+ let out = "";
662
+ for (let k = 0; k < s.length; k++) {
663
+ const c = s.charAt(k);
664
+ if (c === "\\" && isAsciiPunct(s.charAt(k + 1))) {
665
+ out += s.charAt(++k);
666
+ continue;
667
+ }
668
+ const en = c === "&" ? entity(s, k) : null;
669
+ if (en) {
670
+ out += en[0];
671
+ k = en[1] - 1;
672
+ } else out += c;
673
+ }
674
+ return out;
675
+ }
676
+ function safeHref(raw) {
677
+ let u;
678
+ try {
679
+ u = new URL(raw);
680
+ } catch {
681
+ return null;
682
+ }
683
+ if (u.username || u.password) return null;
684
+ if (u.protocol === "mailto:") return mailbox(u) ? u.href : null;
685
+ return (u.protocol === "https:" || u.protocol === "http:") && HOSTNAME.test(u.hostname) ? u.href : null;
686
+ }
687
+ function mailbox(u) {
688
+ const path = u.pathname, at = path.lastIndexOf("@"), domain = path.slice(at + 1);
689
+ if (at < 1 || /[/\\?#:]/.test(domain)) return null;
690
+ let local, host;
691
+ try {
692
+ local = decodeURIComponent(path.slice(0, at));
693
+ host = new URL("http://" + domain).hostname;
694
+ } catch {
695
+ return null;
696
+ }
697
+ return HOSTNAME.test(host) && !/[@"()\s]/.test(local) ? path.slice(0, at + 1) + host + u.search : null;
698
+ }
699
+ function shown(href) {
700
+ const u = new URL(href);
701
+ const text = u.protocol === "mailto:" ? "mailto:" + (mailbox(u) || u.pathname) : u.protocol + "//" + u.host + decodeOr(u.pathname + u.search + u.hash);
702
+ return points(text) > MAX_SHOWN ? firstPoints(text, MAX_SHOWN - 1) + ELLIPSIS : text;
703
+ }
704
+ function decodeOr(s) {
705
+ try {
706
+ return decodeURI(s).replace(HIDDEN, encodeURIComponent);
707
+ } catch {
708
+ return s;
709
+ }
710
+ }
711
+ function points(s) {
712
+ let n = 0;
713
+ for (let k = 0; k < s.length; k += s.codePointAt(k) > 65535 ? 2 : 1) n++;
714
+ return n;
715
+ }
716
+ function firstPoints(s, n) {
717
+ let k = 0;
718
+ for (; n > 0 && k < s.length; n--) k += s.codePointAt(k) > 65535 ? 2 : 1;
719
+ return s.slice(0, k);
720
+ }
721
+ function note(label, href) {
722
+ const u = new URL(href);
723
+ if (u.protocol === "mailto:") {
724
+ const to = mailbox(u) || u.pathname;
725
+ return label === to ? "" : to;
726
+ }
727
+ const t = unscheme(label), host = u.hostname;
728
+ return t === unscheme(href) || t === unscheme(shown(href)) || t === host || t === "www." + host ? "" : host;
729
+ }
730
+ function tidy(first) {
731
+ const nodes = [], outs = [];
732
+ let all = "";
733
+ eachText(first, (x) => {
734
+ if (x.k === BR) {
735
+ x.k = TEXT;
736
+ x.s = " ";
737
+ }
738
+ nodes.push(x);
739
+ all += x.s;
740
+ return true;
741
+ });
742
+ let pos = 0, count = 0, space = true, joinTo = -1, join = false;
743
+ for (const x of nodes) {
744
+ let out = "";
745
+ for (const end = pos + x.s.length; pos < end; ) {
746
+ const ch = String.fromCodePoint(all.codePointAt(pos));
747
+ pos += ch.length;
748
+ if (!space && (ch === ZWNJ || ch === ZWJ) && pos > joinTo) {
749
+ for (joinTo = pos; joinTo < all.length; ) {
750
+ const c = String.fromCodePoint(all.codePointAt(joinTo));
751
+ if (c !== ZWNJ && c !== ZWJ && !VS.test(c)) break;
752
+ joinTo += c.length;
753
+ }
754
+ join = joinTo < all.length && !BLANK.test(String.fromCodePoint(all.codePointAt(joinTo)));
755
+ }
756
+ const blank = BLANK.test(ch) && (space || (ch === ZWNJ || ch === ZWJ ? !join : !VS.test(ch)));
757
+ if (blank && space) continue;
758
+ out += blank ? " " : ch;
759
+ count++;
760
+ space = blank;
761
+ }
762
+ outs.push(out);
763
+ }
764
+ for (let k = outs.length - 1; k >= 0; k--) {
765
+ if (!outs[k]) continue;
766
+ if (outs[k].charAt(outs[k].length - 1) === " ") {
767
+ outs[k] = outs[k].slice(0, -1);
768
+ count--;
769
+ }
770
+ break;
771
+ }
772
+ if (count > MAX_LABEL) {
773
+ let left = MAX_LABEL - 1, k = 0;
774
+ while (points(outs[k]) < left) left -= points(outs[k++]);
775
+ let head = firstPoints(outs[k], left);
776
+ if (head.charAt(head.length - 1) === " ") head = head.slice(0, -1);
777
+ outs[k] = head + ELLIPSIS;
778
+ while (++k < outs.length) outs[k] = "";
779
+ }
780
+ nodes.forEach((x, k) => {
781
+ x.s = outs[k];
782
+ });
783
+ }
784
+ function eachText(n, fn) {
785
+ const resume = [];
786
+ for (; ; ) {
787
+ if (!n) {
788
+ if (!resume.length) return;
789
+ n = resume.pop();
790
+ continue;
791
+ }
792
+ if (n.k === EL) {
793
+ resume.push(n.next);
794
+ n = n.kid;
795
+ continue;
796
+ }
797
+ if (n.k !== NOTE && !fn(n)) return;
798
+ n = n.next;
799
+ }
800
+ }
801
+ function unscheme(s) {
802
+ s = s.trim();
803
+ const head = s.slice(0, 8).toLowerCase();
804
+ s = head.indexOf("https://") === 0 ? s.slice(8) : head.indexOf("http://") === 0 ? s.slice(7) : s;
805
+ return s.charAt(s.length - 1) === "/" ? s.slice(0, -1) : s;
806
+ }
807
+ function blockTree(bs, tight) {
808
+ const out = [];
809
+ for (const b of bs) {
810
+ switch (b.t) {
811
+ case "p":
812
+ if (tight) inlineTree(parseInline(b.s), out, 0, false);
813
+ else out.push({ tag: "p", children: inlineTree(parseInline(b.s), [], 0, false) });
814
+ break;
815
+ case "pre":
816
+ out.push(b.lang ? { tag: "pre", language: b.lang, children: [clean(b.s)] } : { tag: "pre", children: [clean(b.s)] });
817
+ break;
818
+ case "blockquote":
819
+ out.push({ tag: "blockquote", children: blockTree(b.kids || [], false) });
820
+ break;
821
+ case "ul":
822
+ case "ol": {
823
+ const items = (b.items || []).map((item) => ({ tag: "li", children: blockTree(item, !!b.tight) }));
824
+ const list = { tag: b.t, children: items };
825
+ if (b.t === "ol" && b.start !== 1) list.start = b.start;
826
+ out.push(list);
827
+ break;
828
+ }
829
+ case "table":
830
+ out.push(tableTree(b.rows || [], b.align || []));
831
+ break;
832
+ case "hr":
833
+ out.push({ tag: "hr" });
834
+ break;
835
+ default:
836
+ out.push({ tag: b.t, children: inlineTree(parseInline(b.s), [], 0, false) });
837
+ }
838
+ }
839
+ return out;
840
+ }
841
+ function tableTree(rows, align) {
842
+ const sections = [], width = rows.length ? rows[0].length : 0;
843
+ let body = [], budget = 0;
844
+ for (const row of rows) budget += 2 * row.length;
845
+ for (let r = 0; r < rows.length; r++) {
846
+ if (r < 2) sections.push({ tag: r ? "tbody" : "thead", children: body = [] });
847
+ const has = rows[r].length;
848
+ let n = has;
849
+ if (has < width) {
850
+ if (width - has <= budget) {
851
+ budget -= width - has;
852
+ n = width;
853
+ } else budget = 0;
854
+ }
855
+ const row = [];
856
+ for (let c = 0; c < n; c++) {
857
+ const cell = { tag: r ? "td" : "th", children: c < has ? inlineTree(parseInline(rows[r][c]), [], 0, false) : [] };
858
+ const a = align[c];
859
+ if (a) cell.align = a;
860
+ row.push(cell);
861
+ }
862
+ body.push({ tag: "tr", children: row });
863
+ }
864
+ return { tag: "table", children: sections };
865
+ }
866
+ function inlineTree(n, out, depth, inA) {
867
+ let run = "";
868
+ for (; n; n = n.next) {
869
+ if (n.k === TEXT || n.k === DELIM) run += n.s;
870
+ else if (n.k === NOTE) {
871
+ if (inA || depth >= MAX_INLINE) continue;
872
+ pushText(out, run);
873
+ pushText(out, n.s);
874
+ run = "";
875
+ } else if (n.k === EL && (n.s === "a" && inA || depth >= MAX_INLINE)) run += plain(n.kid);
876
+ else {
877
+ pushText(out, run);
878
+ run = "";
879
+ if (n.k === BR) out.push({ tag: "br" });
880
+ else if (n.k === CODE) out.push({ tag: "code", children: [clean(n.s)] });
881
+ else {
882
+ const el = { tag: n.s, children: inlineTree(n.kid, [], depth + 1, inA || n.s === "a") };
883
+ if (n.s === "a") {
884
+ el.href = n.href;
885
+ if (n.dest) el.dest = n.dest;
886
+ }
887
+ out.push(el);
888
+ }
889
+ }
890
+ }
891
+ pushText(out, run);
892
+ return out;
893
+ }
894
+ function pushText(out, s) {
895
+ s = clean(s);
896
+ if (s) out.push(s);
897
+ }
898
+ function clean(s) {
899
+ return s.replace(BIDI, "");
900
+ }
901
+ function build(nodes, parent, doc) {
902
+ for (const n of nodes) {
903
+ if (typeof n === "string") {
904
+ parent.appendChild(doc.createTextNode(n));
905
+ continue;
906
+ }
907
+ const el = doc.createElement(n.tag);
908
+ let inner = el;
909
+ if (n.tag === "pre") {
910
+ inner = doc.createElement("code");
911
+ if (n.language) inner.setAttribute("class", "language-" + n.language);
912
+ el.appendChild(inner);
913
+ } else if (n.tag === "a" && n.href) {
914
+ el.setAttribute("href", n.href);
915
+ el.setAttribute("target", "_blank");
916
+ el.setAttribute("rel", "noopener noreferrer");
917
+ } else if (n.tag === "ol" && n.start !== void 0) el.setAttribute("start", String(n.start));
918
+ else if ((n.tag === "th" || n.tag === "td") && n.align) el.style.textAlign = n.align;
919
+ if (n.children) build(n.children, inner, doc);
920
+ parent.appendChild(el);
921
+ }
922
+ }
923
+ export {
924
+ parseMarkdown,
925
+ renderMarkdown
926
+ };
927
+ //# sourceMappingURL=markdown.js.map