limmat 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,1258 @@
1
+ // src/block.ts
2
+ var ATXRE = /^ {0,3}(#{1,6})(?:[ \t]([^\n]*))?$/;
3
+ var LI = /^( *)([-*+]|\d{1,9}[.)])(?:( +)([^\n]*)|$)/;
4
+ var FENCE = /^( {0,3})(`{3,}|~{3,})([^\n]*)$/;
5
+ var STAR_BOLD = /^ {0,3}\*[ \t]+\*{2,}$/;
6
+ var BARE = /^ {0,3}(?:[-*+]|\d{1,9}[.)])$/;
7
+ var HR = /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/;
8
+ var BQ = /^ {0,3}>/;
9
+ var PIPE = /^ {0,3}\|/;
10
+ var DELIM = /^ {0,3}\|?[ \t]*:?-+:?[ \t]*(?:\|[ \t]*:?-+:?[ \t]*)*(?:\|[ \t]*)?$/;
11
+ var PREFIX = /^[ \t|:-]*$/;
12
+ var OPEN = /^ {0,3}<details( open)?>(?:<summary>((?:(?!<\/summary>)[^\n])*)<\/summary>)?[ \t]*$/i;
13
+ var CLOSE = /^ {0,3}<\/details>[ \t]*$/i;
14
+ var SUMM = /^ {0,3}<summary>((?:(?!<\/summary>)[^\n])*)<\/summary>[ \t]*$/i;
15
+ var TAGS = ["<details>", "<details open>", "</details>"];
16
+ var PARTIAL_CLOSE = /^ {0,3}(?:`+|~+)$/;
17
+ var spaces = (l) => l.length - l.trimStart().length;
18
+ function atx(l) {
19
+ const m = ATXRE.exec(l);
20
+ if (!m) return null;
21
+ let t = (m[2] ?? "").trim();
22
+ let k = t.length;
23
+ while (k > 0 && t[k - 1] === "#") k--;
24
+ if (k < t.length && (k === 0 || t[k - 1] === " " || t[k - 1] === " ")) t = t.slice(0, k).trimEnd();
25
+ return [m[1].length, t];
26
+ }
27
+ function cells(l) {
28
+ l = l.trim();
29
+ if (l[0] === "|") l = l.slice(1);
30
+ if (l.endsWith("|") && !l.endsWith("\\|")) l = l.slice(0, -1);
31
+ const out = [];
32
+ let cur = "";
33
+ for (let i = 0; i < l.length; i++) {
34
+ if (l[i] === "\\" && l[i + 1] === "|") {
35
+ cur += "|";
36
+ i++;
37
+ } else if (l[i] === "|") {
38
+ out.push(cur.trim());
39
+ cur = "";
40
+ } else cur += l[i];
41
+ }
42
+ out.push(cur.trim());
43
+ return out;
44
+ }
45
+ var aligns = (l) => cells(l).map((c) => c[0] === ":" ? c.endsWith(":") ? "center" : "left" : c.endsWith(":") ? "right" : null);
46
+ var fence = (l) => {
47
+ const m = FENCE.exec(l);
48
+ return m && !(m[2][0] === "`" && m[3].includes("`")) ? m : null;
49
+ };
50
+ var closer = (m, indent) => new RegExp(`^${indent}${m[2][0]}{${m[2].length},}[ \\t]*$`);
51
+ function opener(l) {
52
+ const m = OPEN.exec(l);
53
+ return m && (m[2] === void 0 || m[2].trim()) ? m : null;
54
+ }
55
+ function tagPrefix(l, sum) {
56
+ let k = 0;
57
+ while (k < 4 && l[k] === " ") k++;
58
+ if (k > 3) return false;
59
+ const r = l.slice(k).toLowerCase();
60
+ let e = r.length;
61
+ while (e > 0 && (r[e - 1] === " " || r[e - 1] === " ")) e--;
62
+ const t = r.slice(0, e);
63
+ if (TAGS.some((x) => x.startsWith(r) || t.length < r.length && x === t)) return true;
64
+ const o = /^<details( open)?>/.exec(r);
65
+ if (o) {
66
+ const rest = r.slice(o[0].length);
67
+ return !!rest && ("<summary>".startsWith(rest) || rest.startsWith("<summary>") && !rest.includes("</summary>"));
68
+ }
69
+ if (sum && (r.length < 9 ? "<summary>".startsWith(r) : r.startsWith("<summary>"))) {
70
+ const rest = r.slice(9);
71
+ const e2 = rest.indexOf("</summary>");
72
+ return e2 < 0 || !rest.slice(e2 + 10).trim();
73
+ }
74
+ return false;
75
+ }
76
+ function interrupts(l, inTable, det = false) {
77
+ if (det && (opener(l) || CLOSE.test(l))) return true;
78
+ if (fence(l) || atx(l) || HR.test(l) || BQ.test(l)) return true;
79
+ if (!inTable && PIPE.test(l)) return true;
80
+ const m = LI.exec(l);
81
+ return !!m && !!m[4] && m[1].length < 4 && m[2].length < 4;
82
+ }
83
+ function fenceUnit(L, i, m, partial, final) {
84
+ const n = L.length;
85
+ const close = closer(m, " {0,3}");
86
+ let j = i + 1;
87
+ let closed = false;
88
+ while (j < n) {
89
+ if (close.test(L[j])) {
90
+ closed = true;
91
+ j++;
92
+ break;
93
+ }
94
+ j++;
95
+ }
96
+ const hide = !closed && !final && partial && j === n && j - 1 > i && PARTIAL_CLOSE.test(L[n - 1]);
97
+ return { k: "f", a: i, e: hide ? j - 1 : j, b: j, lang: m[3].trim().split(/\s+/)[0], closed, ind: m[1].length };
98
+ }
99
+ function quoteEnd(L, i, det) {
100
+ const n = L.length;
101
+ let j = i;
102
+ let para = false;
103
+ let fe = null;
104
+ for (; j < n && L[j].trim(); j++) {
105
+ if (!BQ.test(L[j])) {
106
+ if (!para || interrupts(L[j], false, det)) break;
107
+ continue;
108
+ }
109
+ const c = L[j].replace(/^ {0,3}> ?/, "");
110
+ const f = fe ? null : fence(c);
111
+ if (fe) {
112
+ if (fe.test(c)) fe = null;
113
+ para = false;
114
+ } else if (f) {
115
+ fe = closer(f, " {0,3}");
116
+ para = false;
117
+ } else para = !!c.trim() && !atx(c) && !HR.test(c);
118
+ }
119
+ return j;
120
+ }
121
+ function itemEnd(L, i, off, text, det) {
122
+ const n = L.length;
123
+ let j = i + 1;
124
+ let fe = null;
125
+ let blank = false;
126
+ let last = i + 1;
127
+ for (; j < n; j++) {
128
+ const x = L[j];
129
+ if (fe) {
130
+ if (x.trim() && spaces(x) < 2) break;
131
+ if (fe.test(x)) {
132
+ fe = null;
133
+ text = false;
134
+ }
135
+ last = j + 1;
136
+ continue;
137
+ }
138
+ if (!x.trim()) {
139
+ blank = true;
140
+ continue;
141
+ }
142
+ const xi = spaces(x);
143
+ if (xi >= 2) {
144
+ const f = fence(x.slice(Math.min(xi, off)));
145
+ if (f) fe = closer(f, " *");
146
+ } else if (LI.test(x) || interrupts(x, false, det) || blank || !text) break;
147
+ blank = false;
148
+ text = !fe;
149
+ last = j + 1;
150
+ }
151
+ return { last, j };
152
+ }
153
+ function scan(L, tbl, partial, final, det = 0, dh = false) {
154
+ var _a;
155
+ const U = [];
156
+ const n = L.length;
157
+ const fr = [];
158
+ let i = 0;
159
+ while (i < n) {
160
+ const l = L[i];
161
+ if (!l.trim()) {
162
+ if (U.length > (fr.length ? fr[fr.length - 1].u : 0)) U[U.length - 1].b = i + 1;
163
+ tbl = null;
164
+ i++;
165
+ continue;
166
+ }
167
+ if (det) {
168
+ const dm = opener(l);
169
+ if (dh && i === n - 1 && (dm || CLOSE.test(l) || tagPrefix(l, !!fr.length && i === fr[fr.length - 1].a + 1))) {
170
+ i++;
171
+ continue;
172
+ }
173
+ if (dm) {
174
+ fr.push({ a: i, u: U.length, open: !!dm[1], sum: dm[2] === void 0 ? null : dm[2].trim() });
175
+ tbl = null;
176
+ i++;
177
+ continue;
178
+ }
179
+ if (fr.length && CLOSE.test(l)) {
180
+ const f = fr.pop();
181
+ tbl = null;
182
+ if (!fr.length) {
183
+ U.length = f.u;
184
+ const s = body(L, f, i, false);
185
+ U.push({ k: "d", a: f.a, e: i + 1, b: i + 1, open: f.open, closed: true, sum: s[0], bs: s[1] });
186
+ }
187
+ i++;
188
+ continue;
189
+ }
190
+ }
191
+ if (tbl) {
192
+ if (!interrupts(l, true, !!det)) {
193
+ U.push({ k: "tr", a: i, e: i + 1, b: i + 1, al: tbl });
194
+ i++;
195
+ continue;
196
+ }
197
+ tbl = null;
198
+ }
199
+ let m;
200
+ let h;
201
+ if (m = fence(l)) {
202
+ const u = fenceUnit(L, i, m, partial, final);
203
+ U.push(u);
204
+ i = u.b;
205
+ } else if (h = atx(l)) {
206
+ U.push({ k: "h", a: i, e: i + 1, b: i + 1, lv: h[0] });
207
+ i++;
208
+ } else if (HR.test(l)) {
209
+ if (!final && partial && i === n - 1 && STAR_BOLD.test(l)) {
210
+ i++;
211
+ continue;
212
+ }
213
+ U.push({ k: "hr", a: i, e: i + 1, b: i + 1 });
214
+ i++;
215
+ } else if (BQ.test(l)) {
216
+ const j = quoteEnd(L, i, !!det);
217
+ U.push({ k: "q", a: i, e: j, b: j });
218
+ i = j;
219
+ } else if (m = LI.exec(l)) {
220
+ if (m[4] === void 0 && !final && partial && i === n - 1) {
221
+ i++;
222
+ continue;
223
+ }
224
+ const ind = m[1].length;
225
+ const ord = /\d/.test(m[2]);
226
+ const sp = ((_a = m[3]) == null ? void 0 : _a.length) ?? 1;
227
+ const off = ind + m[2].length + (m[4] === void 0 || sp > 4 ? 1 : sp);
228
+ const { last, j } = itemEnd(L, i, off, !!m[4], !!det);
229
+ U.push({ k: "li", a: i, e: last, b: j, ord, num: ord ? parseInt(m[2], 10) : 0, off });
230
+ i = j;
231
+ } else {
232
+ const nx = L[i + 1];
233
+ const lead = PIPE.test(l);
234
+ if (l.includes("|") && (lead || nx !== void 0 && DELIM.test(nx) && cells(l).length === cells(nx).length)) {
235
+ if (nx !== void 0 && DELIM.test(nx)) {
236
+ const al = aligns(nx);
237
+ U.push({ k: "th", a: i, e: i + 2, b: i + 2, al });
238
+ tbl = al;
239
+ i += 2;
240
+ continue;
241
+ }
242
+ if (lead && !final && (nx === void 0 || partial && i + 1 === n - 1 && PREFIX.test(nx))) {
243
+ U.push({ k: "th", a: i, e: n, b: n, pend: true, al: cells(l).map(() => null) });
244
+ break;
245
+ }
246
+ }
247
+ if (!lead && !final && partial && nx !== void 0 && i + 1 === n - 1 && l.includes("|") && PREFIX.test(nx)) {
248
+ const dashes = nx.split("-").length - 1;
249
+ if (dashes > 0 && (dashes > 1 || /[|:]/.test(nx)) && cells(nx).length <= cells(l).length) {
250
+ U.push({ k: "th", a: i, e: n, b: n, pend: true, al: cells(l).map(() => null) });
251
+ break;
252
+ }
253
+ }
254
+ let j = i + 1;
255
+ while (j < n && L[j].trim() && !interrupts(L[j], false, !!det)) j++;
256
+ const bare = !final && partial && j === n && j - 1 > i && BARE.test(L[n - 1]);
257
+ const dbare = !!det && dh && j === n && j - 1 > i && tagPrefix(L[n - 1], false);
258
+ U.push({ k: "p", a: i, e: bare || dbare ? j - 1 : j, b: j });
259
+ i = j;
260
+ }
261
+ }
262
+ if (fr.length) {
263
+ const f = fr[0];
264
+ U.length = f.u;
265
+ const s = body(L, f, n, dh);
266
+ let has = s[0] !== null;
267
+ for (let k = s[1]; !has && k < n; k++) has = !!L[k].trim() && !(dh && k === n - 1 && tagPrefix(L[k], false));
268
+ if (has) U.push({ k: "d", a: f.a, e: n, b: n, open: f.open, closed: false, sum: s[0], bs: s[1] });
269
+ else if (det === 2) U.push({ k: "p", a: f.a, e: f.a + 1, b: n });
270
+ }
271
+ return U;
272
+ }
273
+ function body(L, f, end, pend) {
274
+ if (f.sum !== null) return [f.sum, f.a + 1];
275
+ const k = f.a + 1;
276
+ if (k >= end) return [null, k];
277
+ if (pend && k === L.length - 1 && tagPrefix(L[k], true)) return [null, k + 1];
278
+ const m = SUMM.exec(L[k]);
279
+ return (m == null ? void 0 : m[1].trim()) ? [m[1].trim(), k + 1] : [null, k];
280
+ }
281
+
282
+ // src/inline.ts
283
+ var HTML_TAGS = ["b", "strong", "i", "em", "u", "s", "del", "ins", "mark", "small", "kbd", "sub", "sup"];
284
+ var DEFAULT_TAGS = new Set(HTML_TAGS);
285
+ var PUNCT = /[\p{P}\p{S}]/u;
286
+ var WS = /\s/;
287
+ var ESC = /[!-/:-@[-`{-~]/;
288
+ var WORD = /[A-Za-z0-9_]/;
289
+ var BR = /<br\s*\/?>/iy;
290
+ var TAG = /<(\/?)([A-Za-z]+)>/y;
291
+ var AUTO = /<([A-Za-z][A-Za-z0-9+.-]*:[^\s<>]*|[\w.+-]+@[\w-]+(?:\.[\w-]+)+)>/y;
292
+ var PARTIAL_AUTO = /<(?:[A-Za-z][\w.+-]*:[^\s<>]*|[\w.+-]+@[\w.-]*)$/y;
293
+ var URLRE = /(?:https?:\/\/|www\.)[^\s<\]]+/iy;
294
+ var DOMAIN = /[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+/y;
295
+ var MAXDEST = 8192;
296
+ var MAXDEPTH = 30;
297
+ var lastCp = (s, i) => {
298
+ const c = s.charCodeAt(i - 1);
299
+ return i > 1 && c >= 56320 && c <= 57343 ? s.slice(i - 2, i) : s[i - 1];
300
+ };
301
+ var firstCp = (s, i) => String.fromCodePoint(s.codePointAt(i));
302
+ function merge(t) {
303
+ const o = [];
304
+ for (const x of t) {
305
+ const l = o[o.length - 1];
306
+ if (x.k === "t" && l && l.k === "t") l.s = l.s + x.s;
307
+ else o.push(x);
308
+ }
309
+ return o;
310
+ }
311
+ function parseDest(s, j, w) {
312
+ const n = s.length;
313
+ if (w.n <= 0) return null;
314
+ j++;
315
+ while (s[j] === " " || s[j] === " ") j++;
316
+ if (j >= n) return "p";
317
+ let dest;
318
+ if (s[j] === "<") {
319
+ let k = j + 1;
320
+ while (k < n && s[k] !== ">" && s[k] !== "\n" && s[k] !== "<" && k - j < MAXDEST) k++;
321
+ w.n -= k - j;
322
+ if (k >= n) return "p";
323
+ if (s[k] !== ">") return null;
324
+ dest = s.slice(j + 1, k);
325
+ j = k + 1;
326
+ } else {
327
+ let depth = 0;
328
+ let k = j;
329
+ while (k < n) {
330
+ const c = s.charCodeAt(k);
331
+ if (c <= 32 || c === 127) break;
332
+ if (c === 92 && k + 1 < n) {
333
+ k += 2;
334
+ continue;
335
+ }
336
+ if (c === 40) depth++;
337
+ else if (c === 41 && !depth--) break;
338
+ if (++k - j > MAXDEST) {
339
+ w.n -= k - j;
340
+ return null;
341
+ }
342
+ }
343
+ w.n -= k - j;
344
+ if (k >= n) return "p";
345
+ dest = s.slice(j, k);
346
+ j = k;
347
+ }
348
+ const skip = () => {
349
+ while (s[j] === " " || s[j] === " " || s[j] === "\n") j++;
350
+ };
351
+ const href = () => dest.replace(/\\([!-/:-@[-`{-~])/g, "$1");
352
+ skip();
353
+ if (j >= n) return "p";
354
+ if (s[j] === ")") return { end: j + 1, href: href() };
355
+ const q = s[j];
356
+ if (q === '"' || q === "'" || q === "(") {
357
+ const close = q === "(" ? ")" : q;
358
+ const lim = Math.min(n, j + 501);
359
+ let k = j + 1;
360
+ while (k < lim && s[k] !== close) k++;
361
+ w.n -= k - j;
362
+ if (k >= lim) return k >= n ? "p" : null;
363
+ j = k + 1;
364
+ skip();
365
+ if (j >= n) return "p";
366
+ if (s[j] === ")") return { end: j + 1, href: href() };
367
+ }
368
+ return null;
369
+ }
370
+ function trimUrl(u) {
371
+ let o = 0, c = 0;
372
+ for (const ch of u) ch === "(" ? o++ : ch === ")" && c++;
373
+ for (; ; ) {
374
+ const l = u[u.length - 1];
375
+ if (/[?!.,:;*_~'"]/.test(l)) u = u.slice(0, -1);
376
+ else if (l === ")" && c > o) {
377
+ u = u.slice(0, -1);
378
+ c--;
379
+ } else return u;
380
+ }
381
+ }
382
+ function pairTags(t, spec) {
383
+ if (!t.some((x) => x.k === "g")) return t;
384
+ const out = [];
385
+ const open = [];
386
+ const text = (x) => ({ k: "t", s: x.s });
387
+ const wrap = (at, sp) => {
388
+ const kids = resolve(out.slice(at + 1), sp);
389
+ let md = 0;
390
+ for (const z of kids) md = Math.max(md, z.d ?? 0);
391
+ const tag = out[at].tag;
392
+ out.length = at;
393
+ out.push({ k: "e", tag, kids, d: md + 1 });
394
+ };
395
+ for (const x of t) {
396
+ if (x.k !== "g") out.push(x);
397
+ else if (x.o) {
398
+ if (open.length < MAXDEPTH) {
399
+ open.push(out.length);
400
+ out.push(x);
401
+ } else out.push(text(x));
402
+ } else {
403
+ let j = open.length - 1;
404
+ while (j >= 0 && out[open[j]].tag !== x.tag) j--;
405
+ if (j < 0) out.push(text(x));
406
+ else {
407
+ const at = open[j];
408
+ open.length = j;
409
+ wrap(at, false);
410
+ }
411
+ }
412
+ }
413
+ if (spec) while (open.length) wrap(open.pop(), true);
414
+ return out.map((z) => z.k === "g" ? text(z) : z);
415
+ }
416
+ function resolve(t, spec) {
417
+ t = pairTags(t, spec);
418
+ if (!t.some((x) => x.k === "d")) return merge(t);
419
+ const head = { k: "h", x: null };
420
+ let prev = head;
421
+ for (const x of t) {
422
+ prev.x = x;
423
+ x.p = prev;
424
+ x.x = null;
425
+ prev = x;
426
+ }
427
+ const stack = [];
428
+ const bottom = {};
429
+ const lit = (z) => ({ k: "t", s: z.ch.repeat(z.n) });
430
+ const between = (from, to) => {
431
+ const kids = [];
432
+ let md = 0;
433
+ for (let z = from; z && z !== to; z = z.x) {
434
+ kids.push(z.k === "d" ? lit(z) : z);
435
+ md = Math.max(md, z.d ?? 0);
436
+ }
437
+ return { kids, md };
438
+ };
439
+ const emphasis = (ch, use, kids, md) => ({
440
+ k: "e",
441
+ tag: use === 2 ? ch === "~" ? "del" : "strong" : "em",
442
+ kids,
443
+ d: md + 1
444
+ });
445
+ for (let cur = head.x; cur; ) {
446
+ if (cur.k !== "d") {
447
+ cur = cur.x;
448
+ continue;
449
+ }
450
+ const cl = cur;
451
+ if (cl.c) {
452
+ const key = cl.ch + (cl.o ? 1 : 0) + cl.orig % 3;
453
+ while (cl.n > 0) {
454
+ let f = -1;
455
+ for (let j = stack.length - 1, lo = Math.min(bottom[key] ?? 0, stack.length); j >= lo; j--) {
456
+ const op2 = stack[j];
457
+ if (op2.ch !== cl.ch || cl.ch === "~" && (op2.n < 2 || cl.n < 2)) continue;
458
+ if ((op2.c || cl.o) && (op2.orig + cl.orig) % 3 === 0 && !(op2.orig % 3 === 0 && cl.orig % 3 === 0)) continue;
459
+ f = j;
460
+ break;
461
+ }
462
+ if (f < 0) {
463
+ bottom[key] = stack.length;
464
+ break;
465
+ }
466
+ const op = stack[f];
467
+ const { kids, md } = between(op.x, cl);
468
+ if (md >= MAXDEPTH) {
469
+ bottom[key] = stack.length;
470
+ break;
471
+ }
472
+ const use = op.n >= 2 && cl.n >= 2 ? 2 : 1;
473
+ const node = emphasis(cl.ch, use, kids, md);
474
+ stack.length = f + 1;
475
+ op.n -= use;
476
+ cl.n -= use;
477
+ node.p = op;
478
+ node.x = cl;
479
+ op.x = node;
480
+ cl.p = node;
481
+ if (op.n === 0) {
482
+ op.p.x = node;
483
+ node.p = op.p;
484
+ stack.pop();
485
+ }
486
+ if (cl.n === 0) {
487
+ node.x = cl.x;
488
+ if (cl.x) cl.x.p = node;
489
+ }
490
+ for (const k in bottom) bottom[k] = Math.min(bottom[k], stack.length);
491
+ }
492
+ if (cl.n > 0 && cl.o) stack.push(cl);
493
+ } else stack.push(cl);
494
+ cur = cl.x;
495
+ }
496
+ if (spec) {
497
+ let last = head;
498
+ while (last.x) last = last.x;
499
+ while (last.k === "d" && last !== head) {
500
+ last = last.p;
501
+ last.x = null;
502
+ }
503
+ closing: for (let j = stack.length - 1; j >= 0; j--) {
504
+ const op = stack[j];
505
+ while (op.n > 0 && op.x && !(op.ch === "~" && op.n < 2)) {
506
+ const { kids, md } = between(op.x, null);
507
+ if (md >= MAXDEPTH) break closing;
508
+ const use = op.n >= 2 ? 2 : 1;
509
+ const node = emphasis(op.ch, use, kids, md);
510
+ node.p = op;
511
+ node.x = null;
512
+ op.x = node;
513
+ op.n -= use;
514
+ }
515
+ }
516
+ }
517
+ const out = [];
518
+ for (let z = head.x; z; z = z.x) {
519
+ if (z.k !== "d") out.push(z);
520
+ else if (z.n > 0) out.push(lit(z));
521
+ }
522
+ return merge(out);
523
+ }
524
+ function partialTag(r, tags) {
525
+ r = r.toLowerCase();
526
+ const full = ["<br>", "<br/>", "<br />", "<http:", "<https:", "<mailto:"];
527
+ for (const t of tags) full.push(`<${t}>`, `</${t}>`);
528
+ return full.some((f) => f.length > r.length && f.startsWith(r));
529
+ }
530
+ function partialAuto(s, i) {
531
+ PARTIAL_AUTO.lastIndex = i;
532
+ return PARTIAL_AUTO.test(s);
533
+ }
534
+ function parseInline(s, spec, tags = DEFAULT_TAGS) {
535
+ const out = [];
536
+ const brk = [];
537
+ const n = s.length;
538
+ let ts = 0;
539
+ let i = 0;
540
+ let runs;
541
+ const ptr = /* @__PURE__ */ new Map();
542
+ const work = { n: 8 * n + 8192 };
543
+ const text = (to) => {
544
+ if (to > ts) out.push({ k: "t", s: s.slice(ts, to) });
545
+ };
546
+ while (i < n) {
547
+ const c = s[i];
548
+ if (c === "\\") {
549
+ const nx = s[i + 1];
550
+ if (nx === "\n") {
551
+ text(i);
552
+ out.push({ k: "br" });
553
+ i += 2;
554
+ ts = i;
555
+ continue;
556
+ }
557
+ if (nx && ESC.test(nx)) {
558
+ text(i);
559
+ out.push({ k: "t", s: nx });
560
+ i += 2;
561
+ ts = i;
562
+ continue;
563
+ }
564
+ if (spec && nx === void 0) {
565
+ text(i);
566
+ i++;
567
+ ts = i;
568
+ continue;
569
+ }
570
+ i++;
571
+ } else if (c === "`") {
572
+ let e = i;
573
+ while (s[e] === "`") e++;
574
+ const len = e - i;
575
+ if (!runs) {
576
+ runs = /* @__PURE__ */ new Map();
577
+ for (const m of s.matchAll(/`+/g)) {
578
+ const l = runs.get(m[0].length);
579
+ l ? l.push(m.index) : runs.set(m[0].length, [m.index]);
580
+ }
581
+ }
582
+ const list = runs.get(len) ?? [];
583
+ let p = ptr.get(len) ?? 0;
584
+ while (p < list.length && list[p] <= i) p++;
585
+ ptr.set(len, p);
586
+ const close = p < list.length ? list[p] : spec ? n : -1;
587
+ if (close >= 0) {
588
+ text(i);
589
+ let code = s.slice(e, close).replace(/\n/g, " ");
590
+ if (code.length > 1 && code[0] === " " && code[code.length - 1] === " " && code.trim()) code = code.slice(1, -1);
591
+ out.push({ k: "c", s: code });
592
+ i = close < n ? close + len : n;
593
+ ts = i;
594
+ } else i = e;
595
+ } else if (c === "*" || c === "_" || c === "~") {
596
+ let e = i;
597
+ while (s[e] === c) e++;
598
+ const len = e - i;
599
+ if (c === "~" && len < 2) {
600
+ if (spec && e === n) {
601
+ text(i);
602
+ ts = e;
603
+ }
604
+ i = e;
605
+ continue;
606
+ }
607
+ const before = i ? lastCp(s, i) : " ";
608
+ const after = e < n ? firstCp(s, e) : " ";
609
+ const bw = WS.test(before), aw = WS.test(after), bp = PUNCT.test(before), ap = PUNCT.test(after);
610
+ const lf = !aw && (!ap || bw || bp);
611
+ const rf = !bw && (!bp || aw || ap);
612
+ const o = c === "_" ? lf && (!rf || bp) : lf;
613
+ const cl = c === "_" ? rf && (!lf || ap) : rf;
614
+ if (o || cl) {
615
+ text(i);
616
+ out.push({ k: "d", ch: c, n: len, orig: len, o, c: cl });
617
+ ts = e;
618
+ } else if (spec && e === n) {
619
+ text(i);
620
+ ts = e;
621
+ }
622
+ i = e;
623
+ } else if (c === "[" || c === "!" && s[i + 1] === "[") {
624
+ text(i);
625
+ brk.push(out.length);
626
+ out.push({ k: "[", img: c === "!" });
627
+ i += c === "!" ? 2 : 1;
628
+ ts = i;
629
+ } else if (c === "]") {
630
+ const bi = brk.pop();
631
+ if (bi === void 0) {
632
+ i++;
633
+ continue;
634
+ }
635
+ const open = out[bi];
636
+ const r = s[i + 1] === "(" ? parseDest(s, i + 1, work) : null;
637
+ if (r && r !== "p") {
638
+ text(i);
639
+ const kids = resolve(out.splice(bi + 1), false);
640
+ out.length = bi;
641
+ let md = 0;
642
+ for (const z of kids) md = Math.max(md, z.d ?? 0);
643
+ if (md >= MAXDEPTH) for (const z of kids) out.push(z);
644
+ else out.push({ k: "l", href: r.href, img: open.img, kids, d: md + 1 });
645
+ i = r.end;
646
+ ts = i;
647
+ } else if (spec && (r === "p" || i + 1 === n)) {
648
+ text(i);
649
+ const kids = resolve(out.splice(bi + 1), spec);
650
+ out.length = bi;
651
+ for (const z of kids) out.push(z);
652
+ i = n;
653
+ ts = n;
654
+ } else {
655
+ open.k = "t";
656
+ open.s = open.img ? "![" : "[";
657
+ i++;
658
+ }
659
+ } else if (c === "<") {
660
+ BR.lastIndex = i;
661
+ AUTO.lastIndex = i;
662
+ let m;
663
+ if (BR.test(s)) {
664
+ text(i);
665
+ out.push({ k: "br" });
666
+ i = BR.lastIndex;
667
+ ts = i;
668
+ } else if (m = AUTO.exec(s)) {
669
+ text(i);
670
+ const u = m[1];
671
+ out.push({ k: "a", s: u, href: !u.includes(":") ? "mailto:" + u : u, ang: true });
672
+ i += m[0].length;
673
+ ts = i;
674
+ } else if (tags.size && (TAG.lastIndex = i) >= 0 && (m = TAG.exec(s)) && tags.has(m[2].toLowerCase())) {
675
+ text(i);
676
+ out.push({ k: "g", tag: m[2].toLowerCase(), o: !m[1], s: m[0] });
677
+ i += m[0].length;
678
+ ts = i;
679
+ } else if (spec && (n - i <= 9 && partialTag(s.slice(i), tags) || partialAuto(s, i))) {
680
+ text(i);
681
+ i = n;
682
+ ts = n;
683
+ } else i++;
684
+ } else if (c === "\n") {
685
+ if (s[i - 1] === " " && s[i - 2] === " ") {
686
+ let t = i;
687
+ while (t > ts && s[t - 1] === " ") t--;
688
+ text(t);
689
+ out.push({ k: "br" });
690
+ ts = i + 1;
691
+ }
692
+ i++;
693
+ } else if ((c === "h" || c === "H" || c === "w" || c === "W") && (i === 0 || !WORD.test(s[i - 1]))) {
694
+ URLRE.lastIndex = i;
695
+ const m = URLRE.exec(s);
696
+ const u = m ? trimUrl(m[0]) : "";
697
+ if (u.replace(/^(?:https?:\/\/|www\.)/i, "")) {
698
+ text(i);
699
+ out.push({ k: "a", s: u, href: /^www\./i.test(u) ? "https://" + u : u });
700
+ i += u.length;
701
+ ts = i;
702
+ } else i++;
703
+ } else if (c === "@") {
704
+ const lm = /[A-Za-z0-9._+-]+$/.exec(s.slice(Math.max(ts, i - 64), i));
705
+ DOMAIN.lastIndex = i + 1;
706
+ const dm = DOMAIN.exec(s);
707
+ if (lm && dm) {
708
+ const dom = dm[0].replace(/[-_.]+$/, "");
709
+ text(i - lm[0].length);
710
+ out.push({ k: "a", s: lm[0] + "@" + dom, href: "mailto:" + lm[0] + "@" + dom });
711
+ i += 1 + dom.length;
712
+ ts = i;
713
+ } else i++;
714
+ } else i++;
715
+ }
716
+ text(n);
717
+ const fin = [];
718
+ for (const x of out) {
719
+ if (x.k !== "[") fin.push(x);
720
+ else if (!spec) fin.push({ k: "t", s: x.img ? "![" : "[" });
721
+ }
722
+ return resolve(fin, spec);
723
+ }
724
+
725
+ // src/url.ts
726
+ var BAD = /[\\\u0000-\u0020\u007f-\u00a0\u00ad\u061c\u180e\u2000-\u200f\u2028-\u202f\u205f-\u206f\u3000\ufeff]/;
727
+ var SCHEME = /^([a-z][a-z0-9+.-]*):/i;
728
+ function safeUrl(raw, allowed, internal) {
729
+ if (!raw || BAD.test(raw)) return null;
730
+ const m = SCHEME.exec(raw);
731
+ if (m) {
732
+ const proto = m[1].toLowerCase() + ":";
733
+ if (!allowed.includes(proto) || raw.length === m[0].length || /^(?:javascript|data|vbscript):$/.test(proto)) return null;
734
+ if (proto[0] === "h" && !/^https?:\/\/[^/?#]/i.test(raw)) return null;
735
+ try {
736
+ new URL(raw);
737
+ } catch {
738
+ return null;
739
+ }
740
+ return raw;
741
+ }
742
+ return raw[0] === "/" && raw[1] !== "/" && internal(raw) ? raw : null;
743
+ }
744
+
745
+ // src/index.ts
746
+ var MAX_DEPTH = 12;
747
+ function createRenderer(container, options = {}) {
748
+ if (!container || typeof container.addEventListener !== "function") throw new TypeError("limmat: container must be an element");
749
+ container.replaceChildren();
750
+ const o = options;
751
+ const protocols = (o.allowedProtocols ?? ["https", "http", "mailto"]).map((p) => `${p}`.toLowerCase().replace(/:?$/, ":"));
752
+ const internal = o.isInternalPath ? (p) => {
753
+ try {
754
+ return !!o.isInternalPath(p);
755
+ } catch {
756
+ return false;
757
+ }
758
+ } : () => true;
759
+ const originOf = (u) => {
760
+ try {
761
+ const x = new URL(u);
762
+ return x.protocol === "https:" || x.protocol === "http:" ? x.origin : "";
763
+ } catch {
764
+ return "";
765
+ }
766
+ };
767
+ const imgProtocols = protocols.filter((p) => p === "https:" || p === "http:");
768
+ const imgOrigins = Array.isArray(o.images) ? new Set(o.images.map((x) => originOf(`${x}`)).filter(Boolean)) : null;
769
+ const imageSrc = (href) => {
770
+ if (o.images === false) return null;
771
+ const s = safeUrl(href, imgProtocols, internal);
772
+ return s && (!imgOrigins || s[0] === "/" || imgOrigins.has(originOf(s))) ? s : null;
773
+ };
774
+ const newTab = o.externalLinksNewTab !== false;
775
+ const tags = new Set(
776
+ o.html === false ? [] : Array.isArray(o.html) ? o.html.map((x) => `${x}`.toLowerCase()).filter((x) => HTML_TAGS.includes(x)) : HTML_TAGS
777
+ );
778
+ const details = o.html === void 0 || o.html === true || Array.isArray(o.html) && o.html.some((x) => `${x}`.toLowerCase() === "details");
779
+ const throttle = Number.isFinite(o.throttleMs) ? o.throttleMs : 50;
780
+ const px = o.classPrefix ?? "limmat";
781
+ const lb = o.labels ?? {};
782
+ const label = {
783
+ copy: lb.copy ?? "Copy",
784
+ copied: lb.copied ?? "Copied",
785
+ table: lb.table ?? "Table",
786
+ done: lb.done ?? "Completed",
787
+ todo: lb.todo ?? "Not completed"
788
+ };
789
+ const cls = (name) => `${px}-${name}`;
790
+ const timers = /* @__PURE__ */ new Set();
791
+ const opened = /* @__PURE__ */ new WeakSet();
792
+ let src = "";
793
+ let held = "";
794
+ let ended = false;
795
+ let dead = false;
796
+ let epoch = 0;
797
+ let timer;
798
+ let lastRun = -Infinity;
799
+ const h = (tag, c, text) => {
800
+ const e = document.createElement(tag);
801
+ if (c) e.className = c;
802
+ if (text) e.textContent = text;
803
+ return e;
804
+ };
805
+ const plain = (ns) => ns.map((n) => n.k === "t" || n.k === "c" || n.k === "a" ? n.s : n.k === "br" ? " " : plain(n.kids)).join("");
806
+ function inline(parent, s, spec) {
807
+ const st = [
808
+ { nodes: parseInline(s, spec, tags), i: 0, el: parent, il: false }
809
+ ];
810
+ while (st.length) {
811
+ const f = st[st.length - 1];
812
+ if (f.i >= f.nodes.length) {
813
+ st.pop();
814
+ continue;
815
+ }
816
+ const x = f.nodes[f.i++];
817
+ if (x.k === "t") f.el.append(x.s);
818
+ else if (x.k === "c") f.el.append(h("code", cls("inline-code"), x.s));
819
+ else if (x.k === "br") f.el.append(h("br"));
820
+ else if (x.k === "e") {
821
+ const el = h(x.tag, x.tag === "kbd" ? cls("kbd") : void 0);
822
+ f.el.append(el);
823
+ st.push({ nodes: x.kids, i: 0, el, il: f.il });
824
+ } else {
825
+ const isImg = x.k === "l" && x.img;
826
+ const src2 = x.k === "l" && isImg ? imageSrc(x.href) : null;
827
+ if (x.k === "l" && src2) {
828
+ const im = h("img", cls("image"));
829
+ for (const [k, v] of [
830
+ ["alt", plain(x.kids)],
831
+ ["loading", "lazy"],
832
+ ["decoding", "async"],
833
+ ["referrerpolicy", "no-referrer"],
834
+ ["src", src2]
835
+ ])
836
+ im.setAttribute(k, v);
837
+ f.el.append(im);
838
+ continue;
839
+ }
840
+ const href = isImg ? null : safeUrl(x.href, protocols, internal);
841
+ if (!href || f.il) {
842
+ if (x.k === "a") f.el.append(x.ang && !href ? `<${x.s}>` : x.s);
843
+ else st.push({ nodes: x.kids, i: 0, el: f.el, il: true });
844
+ } else {
845
+ const a = h("a", cls("link"));
846
+ a.setAttribute("href", href);
847
+ if (newTab && /^https?:/i.test(href)) {
848
+ a.setAttribute("target", "_blank");
849
+ a.setAttribute("rel", "noopener noreferrer");
850
+ }
851
+ f.el.append(a);
852
+ if (x.k === "a") a.append(x.s);
853
+ else st.push({ nodes: x.kids, i: 0, el: a, il: true });
854
+ }
855
+ }
856
+ }
857
+ }
858
+ function highlight(code, text, lang) {
859
+ if (!o.highlight) return;
860
+ try {
861
+ const r = o.highlight(code, text, lang);
862
+ if (r && typeof r.catch === "function") r.catch(() => {
863
+ });
864
+ } catch {
865
+ }
866
+ }
867
+ const dedent = (x, k) => x.slice(Math.min(spaces(x), k));
868
+ const joinTrim = (ls) => ls.map((x) => x.trimStart()).join("\n").trimEnd();
869
+ function fenceText(u, L) {
870
+ return L.slice(u.a + 1, u.closed ? u.e - 1 : u.e).map((x) => dedent(x, u.ind)).join("\n");
871
+ }
872
+ function build(u, L, spec, depth, settled, hl, part = false) {
873
+ switch (u.k) {
874
+ case "p": {
875
+ const el = h("p");
876
+ inline(el, joinTrim(L.slice(u.a, u.e)), spec);
877
+ return { n: el };
878
+ }
879
+ case "h": {
880
+ const el = h("h" + u.lv);
881
+ inline(el, atx(L[u.a])[1], spec);
882
+ return { n: el };
883
+ }
884
+ case "hr":
885
+ return { n: h("hr") };
886
+ case "f": {
887
+ const lang = u.lang;
888
+ const text = fenceText(u, L);
889
+ const wrap = h("div", cls("code"));
890
+ const head = h("div", cls("code-head"));
891
+ if (lang) {
892
+ const l = h("span", cls("lang"), lang);
893
+ l.setAttribute("aria-hidden", "true");
894
+ head.append(l);
895
+ }
896
+ const btn = h("button", cls("copy"), label.copy);
897
+ btn.setAttribute("type", "button");
898
+ btn.setAttribute("aria-label", `${label.copy} ${lang || "code"}`);
899
+ const status = h("span", cls("status"));
900
+ status.setAttribute("role", "status");
901
+ head.append(btn, status);
902
+ const pre = h("pre");
903
+ pre.setAttribute("tabindex", "0");
904
+ const code = h("code", /^[\w+#-]{1,20}$/.test(lang) ? cls("lang-" + lang.toLowerCase()) : "");
905
+ code.append(document.createTextNode(text));
906
+ pre.append(code);
907
+ wrap.append(head, pre);
908
+ if (depth > 0) hl == null ? void 0 : hl.push([code, text, lang]);
909
+ else if (u.closed || settled) highlight(code, text, lang);
910
+ return { n: wrap, code };
911
+ }
912
+ case "q": {
913
+ const el = h("blockquote");
914
+ blocks(
915
+ L.slice(u.a, u.e).map((x) => x.replace(/^ {0,3}> ?/, "")),
916
+ el,
917
+ depth + 1,
918
+ spec,
919
+ false,
920
+ hl,
921
+ part
922
+ );
923
+ return { n: el };
924
+ }
925
+ case "d": {
926
+ const el = h("details", cls("details"));
927
+ if (u.open || !u.closed && !spec) {
928
+ el.setAttribute("open", "");
929
+ opened.add(el);
930
+ }
931
+ if (u.sum !== null) {
932
+ const sm = h("summary", cls("summary"));
933
+ inline(sm, u.sum, false);
934
+ el.append(sm);
935
+ }
936
+ blocks(L.slice(u.bs, u.closed ? u.e - 1 : u.e), el, depth + 1, spec && !u.closed, false, hl, part);
937
+ return { n: el };
938
+ }
939
+ case "li": {
940
+ const m = LI.exec(L[u.a]);
941
+ const body2 = [m[4] ?? "", ...L.slice(u.a + 1, u.e).map((x) => dedent(x, u.off))];
942
+ const li = h("li");
943
+ const tk = /^\[([ xX])\](?:[ \t]+|$)/.exec(body2[0]);
944
+ if (tk) {
945
+ li.className = cls("task");
946
+ const box = h("input");
947
+ box.type = "checkbox";
948
+ box.disabled = true;
949
+ box.defaultChecked = tk[1] !== " ";
950
+ box.setAttribute("aria-label", tk[1] === " " ? label.todo : label.done);
951
+ li.append(box);
952
+ body2[0] = body2[0].slice(tk[0].length);
953
+ }
954
+ blocks(body2, li, depth + 1, spec, true, hl, part);
955
+ return { n: li };
956
+ }
957
+ case "th": {
958
+ const w = h("div", cls("table-wrap"));
959
+ w.setAttribute("tabindex", "0");
960
+ w.setAttribute("role", "group");
961
+ w.setAttribute("aria-label", label.table);
962
+ const t = h("table");
963
+ const tr = h("tr");
964
+ cells(L[u.a]).forEach((c, i) => {
965
+ const th = h("th");
966
+ if (u.al[i]) th.style.textAlign = u.al[i];
967
+ inline(th, c, !!u.pend);
968
+ tr.append(th);
969
+ });
970
+ const thead = h("thead");
971
+ const body2 = h("tbody");
972
+ thead.append(tr);
973
+ t.append(thead, body2);
974
+ w.append(t);
975
+ return { n: w, body: body2 };
976
+ }
977
+ case "tr": {
978
+ const tr = h("tr");
979
+ const cs = cells(L[u.a]);
980
+ u.al.forEach((a, i) => {
981
+ const td = h("td");
982
+ if (a) td.style.textAlign = a;
983
+ inline(td, cs[i] ?? "", spec);
984
+ tr.append(td);
985
+ });
986
+ return { n: tr };
987
+ }
988
+ }
989
+ }
990
+ function place(root, ctx, u, b) {
991
+ if (u.k === "li") {
992
+ if (ctx && ctx.k === "l" && ctx.ord === u.ord) {
993
+ ctx.el.append(b.n);
994
+ return { ctx };
995
+ }
996
+ const l = h(u.ord ? "ol" : "ul", cls("list"));
997
+ if (u.ord && u.num !== 1) l.setAttribute("start", String(u.num));
998
+ l.append(b.n);
999
+ root.append(l);
1000
+ return { ctx: { k: "l", el: l, ord: u.ord }, made: l };
1001
+ }
1002
+ if (u.k === "tr") {
1003
+ ctx.el.append(b.n);
1004
+ return { ctx };
1005
+ }
1006
+ root.append(b.n);
1007
+ return { ctx: u.k === "th" ? { k: "t", el: b.body, al: u.al } : null };
1008
+ }
1009
+ function blocks(L, parent, depth, spec, unwrap, hl, part = false) {
1010
+ if (depth > MAX_DEPTH) {
1011
+ const p = h("p");
1012
+ inline(p, joinTrim(L), spec);
1013
+ parent.append(p);
1014
+ return;
1015
+ }
1016
+ let ctx = null;
1017
+ const U = scan(L, null, part, !part, details ? spec ? 1 : 2 : 0, part);
1018
+ U.forEach((u, i) => {
1019
+ const sp = spec && i === U.length - 1;
1020
+ if (unwrap && i === 0 && u.k === "p") return inline(parent, joinTrim(L.slice(u.a, u.e)), sp);
1021
+ ctx = place(parent, ctx, u, build(u, L, sp, depth, true, hl, part && u.b === L.length)).ctx;
1022
+ });
1023
+ }
1024
+ function runNested(e) {
1025
+ for (const [c, t, l] of e.hl ?? []) highlight(c, t, l);
1026
+ e.hl = void 0;
1027
+ }
1028
+ const mkSt = () => ({ tail: [], fctx: null, fl: 0 });
1029
+ let top = mkSt();
1030
+ const hkey = (u, L) => `${L[u.a]}
1031
+ ${u.sum}
1032
+ ${u.bs - u.a}`;
1033
+ function sync(S, parent, L, partial, final, depth, q) {
1034
+ var _a;
1035
+ const tail = S.tail;
1036
+ const U = scan(L, S.fctx && S.fctx.k === "t" ? S.fctx.al : null, partial, final, details ? final ? 2 : 1 : 0, partial && !final);
1037
+ const hi = (c, t, l) => q ? q.push([c, t, l]) : highlight(c, t, l);
1038
+ const keys = U.map((u2, i) => {
1039
+ const spec = !final && i === U.length - 1;
1040
+ const t = L.slice(u2.a, u2.e).join("\n");
1041
+ const pt = partial && !final && u2.b === L.length && (u2.k === "q" || u2.k === "li" || u2.k === "d");
1042
+ return u2.k === "f" ? `f${u2.closed ? 1 : 0}${t}` : `${u2.k}${u2.k === "d" && u2.closed ? 1 : ""}${spec ? "*" : ""}${pt ? "~" : ""}${u2.k === "th" && u2.pend ? "?" : ""}${t}`;
1043
+ });
1044
+ let p = 0;
1045
+ while (p < tail.length && p < U.length && tail[p].key === keys[p]) p++;
1046
+ const e = tail[p];
1047
+ const u = U[p];
1048
+ if ((e == null ? void 0 : e.open) && e.code && p < U.length && u.k === "f" && e.u.k === "f" && u.lang === e.u.lang) {
1049
+ const text2 = fenceText(u, L);
1050
+ const node = e.code.firstChild;
1051
+ if (text2.startsWith(node.data)) node.appendData(text2.slice(node.data.length));
1052
+ else node.data = text2;
1053
+ e.key = keys[p];
1054
+ e.u = u;
1055
+ if (u.closed || final) {
1056
+ e.open = false;
1057
+ hi(e.code, text2, u.lang);
1058
+ }
1059
+ p++;
1060
+ } else if ((e == null ? void 0 : e.sub) && p === U.length - 1 && u.k === "d" && !u.closed && e.u.k === "d" && hkey(u, L) === e.hk) {
1061
+ const inner = L.slice(u.bs, u.e);
1062
+ e.sub.fl += sync(e.sub, e.node, inner.slice(e.sub.fl), partial, final, depth + 1, e.hl);
1063
+ if (final && !e.node.hasAttribute("open") && !opened.has(e.node)) {
1064
+ e.node.setAttribute("open", "");
1065
+ opened.add(e.node);
1066
+ }
1067
+ e.key = keys[p];
1068
+ e.u = u;
1069
+ p++;
1070
+ }
1071
+ let ctx = p > 0 ? tail[p - 1].ctx : S.fctx;
1072
+ const toggled = [];
1073
+ for (const old of tail.splice(p)) {
1074
+ for (const d of old.node.tagName === "DETAILS" ? [old.node] : Array.from(old.node.querySelectorAll("details")))
1075
+ toggled.push(d.open !== opened.has(d) ? d.open : null);
1076
+ old.node.remove();
1077
+ (_a = old.made) == null ? void 0 : _a.remove();
1078
+ }
1079
+ for (let i = p; i < U.length; i++) {
1080
+ const u2 = U[i];
1081
+ const hl = [];
1082
+ const last = !final && i === U.length - 1;
1083
+ let b;
1084
+ let sub;
1085
+ if (u2.k === "d" && !u2.closed && depth < MAX_DEPTH) {
1086
+ b = build({ ...u2, bs: u2.e }, L, last, depth, final, hl);
1087
+ sub = mkSt();
1088
+ sub.fl += sync(sub, b.n, L.slice(u2.bs, u2.e), partial, final, depth + 1, hl);
1089
+ } else b = build(u2, L, last, depth, final, hl, partial && !final && u2.b === L.length);
1090
+ const r = place(parent, ctx, u2, b);
1091
+ ctx = r.ctx;
1092
+ tail.push({
1093
+ key: keys[i],
1094
+ u: u2,
1095
+ node: b.n,
1096
+ made: r.made,
1097
+ ctx,
1098
+ code: b.code,
1099
+ open: u2.k === "f" && !u2.closed && !final,
1100
+ hl,
1101
+ sub,
1102
+ hk: u2.k === "d" ? hkey(u2, L) : void 0
1103
+ });
1104
+ }
1105
+ if (toggled.some((x) => x !== null)) {
1106
+ let k2 = 0;
1107
+ for (const t of tail.slice(p))
1108
+ for (const d of t.node.tagName === "DETAILS" ? [t.node] : Array.from(t.node.querySelectorAll("details"))) {
1109
+ const was = toggled[k2++];
1110
+ if (was !== null && was !== void 0) d.open = was;
1111
+ }
1112
+ }
1113
+ if (final)
1114
+ for (const t of tail) {
1115
+ if (t.open && t.code && t.u.k === "f") {
1116
+ t.open = false;
1117
+ hi(t.code, t.code.textContent ?? "", t.u.lang);
1118
+ }
1119
+ if (q) q.push(...t.hl ?? []);
1120
+ else runNested(t);
1121
+ }
1122
+ let k = Math.max(U.length - 1, 0);
1123
+ if (partial && k > 0 && U[k].a === L.length - 1) k--;
1124
+ if (!final && k > 0) {
1125
+ S.fctx = tail[k - 1].ctx;
1126
+ if (S.fctx && S.fctx.k === "t" && U[k - 1].b > U[k - 1].e) S.fctx = null;
1127
+ for (const t of tail.splice(0, k)) {
1128
+ if (q) q.push(...t.hl ?? []);
1129
+ else runNested(t);
1130
+ }
1131
+ return U[k - 1].b;
1132
+ }
1133
+ return 0;
1134
+ }
1135
+ function flush(final) {
1136
+ const partial = !src.endsWith("\n");
1137
+ const L = src.split("\n");
1138
+ if (!partial) L.pop();
1139
+ const drop = sync(top, container, L, partial, final, 0, null);
1140
+ let cut = 0;
1141
+ for (let x = 0; x < drop; x++) cut += L[x].length + 1;
1142
+ src = src.slice(cut);
1143
+ }
1144
+ function schedule() {
1145
+ if (throttle <= 0) return flush(false);
1146
+ if (timer !== void 0) return;
1147
+ const wait = lastRun + throttle - Date.now();
1148
+ if (wait <= 0) {
1149
+ lastRun = Date.now();
1150
+ flush(false);
1151
+ } else
1152
+ timer = setTimeout(() => {
1153
+ timer = void 0;
1154
+ lastRun = Date.now();
1155
+ flush(false);
1156
+ }, wait);
1157
+ }
1158
+ const stop = () => {
1159
+ clearTimeout(timer);
1160
+ timer = void 0;
1161
+ };
1162
+ const stopCopy = () => {
1163
+ timers.forEach(clearTimeout);
1164
+ timers.clear();
1165
+ };
1166
+ async function copy(text) {
1167
+ var _a;
1168
+ try {
1169
+ await navigator.clipboard.writeText(text);
1170
+ return true;
1171
+ } catch {
1172
+ const ta = h("textarea");
1173
+ ta.value = text;
1174
+ ta.style.cssText = "position:fixed;opacity:0";
1175
+ const prev = document.activeElement;
1176
+ container.append(ta);
1177
+ ta.select();
1178
+ let ok = false;
1179
+ try {
1180
+ ok = document.execCommand("copy");
1181
+ } catch {
1182
+ }
1183
+ ta.remove();
1184
+ (_a = prev == null ? void 0 : prev.focus) == null ? void 0 : _a.call(prev);
1185
+ return ok;
1186
+ }
1187
+ }
1188
+ function onClick(ev) {
1189
+ var _a, _b, _c, _d, _e;
1190
+ const t = (_b = (_a = ev.target) == null ? void 0 : _a.closest) == null ? void 0 : _b.call(_a, "a,button");
1191
+ if (!t || !container.contains(t)) return;
1192
+ if (t.tagName === "A") {
1193
+ if (((_c = o.onLinkClick) == null ? void 0 : _c.call(o, t.getAttribute("href") ?? "", ev)) === true) ev.preventDefault();
1194
+ } else if (t.classList.contains(cls("copy"))) {
1195
+ const code = (_d = t.closest("." + cls("code"))) == null ? void 0 : _d.querySelector("code");
1196
+ const status = (_e = t.parentElement) == null ? void 0 : _e.querySelector("." + cls("status"));
1197
+ const ep = epoch;
1198
+ copy((code == null ? void 0 : code.textContent) ?? "").then((ok) => {
1199
+ if (!ok || dead || ep !== epoch) return;
1200
+ t.textContent = label.copied;
1201
+ if (status) status.textContent = label.copied;
1202
+ const id = setTimeout(() => {
1203
+ timers.delete(id);
1204
+ t.textContent = label.copy;
1205
+ if (status) status.textContent = "";
1206
+ }, 2e3);
1207
+ timers.add(id);
1208
+ });
1209
+ }
1210
+ }
1211
+ container.addEventListener("click", onClick);
1212
+ return {
1213
+ write(chunk) {
1214
+ if (dead || ended || !chunk) return;
1215
+ chunk = held + chunk;
1216
+ held = "";
1217
+ const c = chunk.charCodeAt(chunk.length - 1);
1218
+ if (c === 13 || c >= 55296 && c < 56320) {
1219
+ held = chunk[chunk.length - 1];
1220
+ chunk = chunk.slice(0, -1);
1221
+ }
1222
+ chunk = chunk.replace(/\r\n?/g, "\n");
1223
+ if (!chunk) return;
1224
+ src += chunk;
1225
+ schedule();
1226
+ },
1227
+ end() {
1228
+ if (dead || ended) return;
1229
+ ended = true;
1230
+ stop();
1231
+ if (held) src += held === "\r" ? "\n" : held;
1232
+ held = "";
1233
+ flush(true);
1234
+ },
1235
+ reset() {
1236
+ if (dead) return;
1237
+ stop();
1238
+ stopCopy();
1239
+ epoch++;
1240
+ container.replaceChildren();
1241
+ src = "";
1242
+ held = "";
1243
+ top = mkSt();
1244
+ ended = false;
1245
+ lastRun = -Infinity;
1246
+ },
1247
+ destroy() {
1248
+ if (dead) return;
1249
+ stop();
1250
+ stopCopy();
1251
+ dead = true;
1252
+ container.removeEventListener("click", onClick);
1253
+ }
1254
+ };
1255
+ }
1256
+ export {
1257
+ createRenderer
1258
+ };