@vielzeug/scribe 0.0.0-stage → 26.11.13

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/scribe.js ADDED
@@ -0,0 +1,685 @@
1
+ // src/errors.ts
2
+ var ScribeError = class extends Error {
3
+ constructor(e, t) {
4
+ super(e, t), this.name = new.target.name;
5
+ }
6
+ }, ScribeLimitError = class extends ScribeError {}, e = {
7
+ "'": "'",
8
+ "\"": """,
9
+ "&": "&",
10
+ "<": "&lt;",
11
+ ">": "&gt;"
12
+ };
13
+ function escapeHtml(t) {
14
+ return t.replace(/[&<>"']/g, (t) => e[t]);
15
+ }
16
+ function stripControls(e) {
17
+ let t = "";
18
+ for (let n of e) {
19
+ let e = n.codePointAt(0) ?? 0;
20
+ e <= 32 || e === 127 || (t += n);
21
+ }
22
+ return t;
23
+ }
24
+ function normalizeDestination(e) {
25
+ let t = stripControls(e);
26
+ for (let e = 0; e < 3; e += 1) {
27
+ let e = stripControls(t.replace(/&#(\d+|x[0-9a-fA-F]+);/g, (e, t) => {
28
+ let n = t.startsWith("x") || t.startsWith("X") ? Number.parseInt(t.slice(1), 16) : Number.parseInt(t, 10);
29
+ return Number.isFinite(n) && n >= 0 && n <= 1114111 ? String.fromCodePoint(n) : e;
30
+ }));
31
+ if (e === t) break;
32
+ t = e;
33
+ }
34
+ return t;
35
+ }
36
+ function sanitizeDestination(e, t) {
37
+ let n = normalizeDestination(e);
38
+ if (n === "") return null;
39
+ let r = n.search(/[/?#]/), i = n.indexOf(":");
40
+ if (i === -1 || r !== -1 && r < i) return n;
41
+ let a = n.slice(0, i).toLowerCase();
42
+ return a === "data" ? null : t.has(a) ? n : null;
43
+ }
44
+ function createSlugger() {
45
+ let e = /* @__PURE__ */ new Map();
46
+ return { slug(t) {
47
+ let n = t.toLowerCase().normalize("NFKC").replace(/[^\p{L}\p{N}]+/gu, "-").replace(/^-+|-+$/g, "") || "section", r = e.get(n);
48
+ return r === void 0 ? (e.set(n, 1), n) : (e.set(n, r + 1), `${n}-${r + 1}`);
49
+ } };
50
+ }
51
+ function collectOutline(e, t, n) {
52
+ let r = [], walk = (e) => {
53
+ for (let i of e) switch (i.type) {
54
+ case "heading": {
55
+ let e = inlineText(i.children), a = t.slug(e);
56
+ i.level <= n && r.push({
57
+ id: a,
58
+ level: i.level,
59
+ text: e
60
+ });
61
+ break;
62
+ }
63
+ case "blockquote":
64
+ walk(i.children);
65
+ break;
66
+ case "list": for (let e of i.items) walk(e.children);
67
+ }
68
+ };
69
+ return walk(e), r;
70
+ }
71
+ function inlineText(e) {
72
+ let t = "";
73
+ for (let n of e) switch (n.type) {
74
+ case "text":
75
+ case "codeSpan":
76
+ t += n.text;
77
+ break;
78
+ case "softBreak":
79
+ case "hardBreak":
80
+ t += " ";
81
+ break;
82
+ case "image":
83
+ t += n.alt;
84
+ break;
85
+ case "strong":
86
+ case "emphasis":
87
+ case "strikethrough":
88
+ case "link": t += inlineText(n.children);
89
+ }
90
+ return t;
91
+ }
92
+ // src/parse.ts
93
+ var t = 2e5, n = 32, r = [
94
+ "http",
95
+ "https",
96
+ "mailto"
97
+ ], i = /^[ \t]*$/, a = /^ {0,3}(`{3,}|~{3,})(.*)$/, o = /^ {0,3}(#{1,6})(?:[ \t]+(.*?))?(?:[ \t]+#+)?[ \t]*$/, s = /^(?: {0,3}(?:\*[ \t]*){3,}| {0,3}(?:-[ \t]*){3,}| {0,3}(?:_[ \t]*){3,})$/, c = /^ {0,3}>[ \t]?(.*)$/, l = /^( {0,3})([-+*])([ \t]+)(.*)$/, u = /^( {0,3})(\d{1,9})([.)])([ \t]+)(.*)$/, d = /^[ \t]*\|?[ \t]*:?-+:?[ \t]*(?:\|[ \t]*:?-+:?[ \t]*)*\|?[ \t]*$/, f = /^[\p{L}\p{N}_-]+/u;
98
+ function indentOf(e) {
99
+ let t = 0;
100
+ for (let n of e) if (n === " ") t += 1;
101
+ else if (n === " ") t += 4;
102
+ else break;
103
+ return t;
104
+ }
105
+ function isTableStart(e, t) {
106
+ return e[t].includes("|") && e[t + 1] !== void 0 && d.test(e[t + 1]);
107
+ }
108
+ function splitCells(e) {
109
+ let t = e.trim();
110
+ t.startsWith("|") && (t = t.slice(1)), t.endsWith("|") && !t.endsWith("\\|") && (t = t.slice(0, -1));
111
+ let n = [], r = "";
112
+ for (let e = 0; e < t.length; e += 1) {
113
+ let i = t[e];
114
+ i === "\\" && t[e + 1] === "|" ? (r += "|", e += 1) : i === "|" ? (n.push(r.trim()), r = "") : r += i;
115
+ }
116
+ return n.push(r.trim()), n;
117
+ }
118
+ function parseDelimAlignments(e) {
119
+ return splitCells(e).map((e) => e.startsWith(":") && e.endsWith(":") ? "center" : e.endsWith(":") ? "right" : "left");
120
+ }
121
+ function parseBlocks(e, t) {
122
+ if (t.depth += 1, t.depth > n) throw new ScribeLimitError(`block nesting exceeds the ${n}-level limit`);
123
+ let r = [], l = [], u = 0, flushParagraph = () => {
124
+ if (l.length === 0) return;
125
+ let e = {
126
+ children: parseInline(l.join("\n"), t),
127
+ type: "paragraph"
128
+ };
129
+ r.push(e), l = [];
130
+ };
131
+ for (; u < e.length;) {
132
+ let n = e[u];
133
+ if (i.test(n)) {
134
+ flushParagraph(), u += 1;
135
+ continue;
136
+ }
137
+ let d = a.exec(n);
138
+ if (d) {
139
+ flushParagraph();
140
+ let t = d[1], n = d[0].indexOf(t[0]), i = f.exec(d[2].trim())?.[0], a = RegExp(`^ {0,3}\\${t[0]}{${t.length},}[ \\t]*$`), o = [];
141
+ for (u += 1; u < e.length && !a.test(e[u]);) o.push(e[u].slice(Math.min(indentOf(e[u]), n))), u += 1;
142
+ u += 1, r.push(i === void 0 ? {
143
+ text: o.join("\n"),
144
+ type: "codeBlock"
145
+ } : {
146
+ language: i,
147
+ text: o.join("\n"),
148
+ type: "codeBlock"
149
+ });
150
+ continue;
151
+ }
152
+ let p = o.exec(n);
153
+ if (p) {
154
+ flushParagraph(), r.push({
155
+ children: parseInline(p[2] ?? "", t),
156
+ level: p[1].length,
157
+ type: "heading"
158
+ }), u += 1;
159
+ continue;
160
+ }
161
+ if (s.test(n)) {
162
+ flushParagraph(), r.push({ type: "thematicBreak" }), u += 1;
163
+ continue;
164
+ }
165
+ if (c.test(n)) {
166
+ flushParagraph();
167
+ let n = [];
168
+ for (; u < e.length;) {
169
+ let t = c.exec(e[u]);
170
+ if (t === null) break;
171
+ n.push(t[1]), u += 1;
172
+ }
173
+ r.push({
174
+ children: parseBlocks(n, t),
175
+ type: "blockquote"
176
+ });
177
+ continue;
178
+ }
179
+ let m = matchListMarker(n);
180
+ if (m !== null && (!m.ordered || m.start === 1 || l.length === 0)) {
181
+ flushParagraph();
182
+ let n = parseList(e, u, t, m);
183
+ r.push(n.block), u = n.next;
184
+ } else if (l.length === 0 && indentOf(n) >= 4) {
185
+ let t = [];
186
+ for (; u < e.length && (i.test(e[u]) || indentOf(e[u]) >= 4) && (!i.test(e[u]) || u + 1 < e.length && indentOf(e[u + 1]) >= 4);) t.push(e[u].slice(4)), u += 1;
187
+ r.push({
188
+ text: t.join("\n"),
189
+ type: "codeBlock"
190
+ });
191
+ } else if (l.length === 0 && isTableStart(e, u)) {
192
+ let n = splitCells(e[u]), a = parseDelimAlignments(e[u + 1]);
193
+ u += 2;
194
+ let o = [];
195
+ for (; u < e.length && !i.test(e[u]) && e[u].includes("|");) {
196
+ let n = splitCells(e[u]);
197
+ for (; n.length < a.length;) n.push("");
198
+ o.push(n.slice(0, a.length).map((e) => [...parseInline(e, t)])), u += 1;
199
+ }
200
+ r.push({
201
+ alignments: a,
202
+ header: n.slice(0, a.length).map((e) => [...parseInline(e, t)]),
203
+ rows: o,
204
+ type: "table"
205
+ });
206
+ } else l.push(n), u += 1;
207
+ }
208
+ return flushParagraph(), --t.depth, r;
209
+ }
210
+ function matchListMarker(e) {
211
+ let t = l.exec(e);
212
+ if (t !== null) return {
213
+ indent: t[1].length,
214
+ kind: t[2],
215
+ ordered: !1,
216
+ spaces: t[3].length,
217
+ start: 1,
218
+ width: 1
219
+ };
220
+ let n = u.exec(e);
221
+ return n === null ? null : {
222
+ indent: n[1].length,
223
+ kind: n[3],
224
+ ordered: !0,
225
+ spaces: n[4].length,
226
+ start: Number(n[2]),
227
+ width: n[2].length + 1
228
+ };
229
+ }
230
+ function sameMarker(e, t) {
231
+ return e.ordered === t.ordered && e.kind === t.kind;
232
+ }
233
+ function parseList(e, t, n, r) {
234
+ let a = r.indent + r.width + (r.spaces >= 5 ? 1 : r.spaces), o = [], s = !1, c = 0, l = null, u = t;
235
+ for (; u < e.length;) {
236
+ let d = e[u];
237
+ if (i.test(d)) {
238
+ l !== null && (c += 1), u += 1;
239
+ continue;
240
+ }
241
+ let f = indentOf(d) < a ? matchListMarker(d) : null, p = f !== null && sameMarker(f, r) && (l === null || u > t);
242
+ if (p && l !== null && (c > 0 && (s = !0), o.push({
243
+ children: parseBlocks(l, n),
244
+ type: "listItem"
245
+ }), l = null), p) l = [d.slice(a)], c = 0, u += 1;
246
+ else if (l !== null && indentOf(d) >= a) c > 0 && (s = !0), c = 0, l.push(d.slice(a)), u += 1;
247
+ else break;
248
+ }
249
+ return l !== null && o.push({
250
+ children: parseBlocks(l, n),
251
+ type: "listItem"
252
+ }), {
253
+ block: {
254
+ items: o,
255
+ ordered: r.ordered,
256
+ start: r.start,
257
+ tight: !s,
258
+ type: "list"
259
+ },
260
+ next: u
261
+ };
262
+ }
263
+ var isDelim = (e) => "kind" in e, p = /[\p{P}\p{S}]/u;
264
+ function parseInline(e, t) {
265
+ if (t.depth += 1, t.depth > n) throw new ScribeLimitError(`inline nesting exceeds the ${n}-level limit`);
266
+ let r = [], i = "", flush = () => {
267
+ i !== "" && (r.push({
268
+ text: i,
269
+ type: "text"
270
+ }), i = "");
271
+ }, linkLike = (t) => {
272
+ if (e[t] !== "[") return null;
273
+ let n = 0, r = -1;
274
+ for (let i = t; i < e.length; i += 1) if (e[i] === "\\") i += 1;
275
+ else if (e[i] === "[") n += 1;
276
+ else if (e[i] === "]" && (--n, n === 0)) {
277
+ r = i;
278
+ break;
279
+ }
280
+ if (r === -1 || e[r + 1] !== "(") return null;
281
+ let i = r + 2, a = "", o;
282
+ if (e[i] === "<") {
283
+ let t = e.indexOf(">", i + 1);
284
+ if (t === -1) return null;
285
+ a = e.slice(i + 1, t), i = t + 1;
286
+ } else {
287
+ let t = 0;
288
+ for (; i < e.length && !/\s/.test(e[i]) && (e[i] !== ")" || t !== 0);) e[i] === "(" ? t += 1 : e[i] === ")" && --t, a += e[i], i += 1;
289
+ }
290
+ let s = /^\s+(['"])(.*?)\1\s*\)/.exec(e.slice(i));
291
+ if (s !== null) o = s[2], i += s[0].length;
292
+ else {
293
+ let t = /^\s*\)/.exec(e.slice(i));
294
+ if (t === null) return null;
295
+ i += t[0].length;
296
+ }
297
+ return {
298
+ dest: a,
299
+ end: i,
300
+ label: e.slice(t + 1, r),
301
+ title: o
302
+ };
303
+ }, a = 0;
304
+ for (; a < e.length;) {
305
+ let n = e[a];
306
+ if (n === "\\" && p.test(e[a + 1] ?? "")) i += e[a + 1], a += 2;
307
+ else if (n === "\\" && e[a + 1] === "\n") flush(), r.push({ type: "hardBreak" }), a += 2;
308
+ else if (n === "`") {
309
+ let t = 0;
310
+ for (; e[a + t] === "`";) t += 1;
311
+ let n = e.slice(a + t), o = RegExp(`(?<!\`)\`{${t}}(?!\`)`).exec(n);
312
+ if (o !== null) {
313
+ let e = n.slice(0, o.index);
314
+ e.length >= 2 && e.startsWith(" ") && e.endsWith(" ") && !/^ +$/.test(e) && (e = e.slice(1, -1)), flush(), r.push({
315
+ text: e,
316
+ type: "codeSpan"
317
+ }), a += t + o.index + t;
318
+ continue;
319
+ }
320
+ i += "`".repeat(t), a += t;
321
+ } else if (n === "<") {
322
+ let n = /^<([A-Za-z][A-Za-z0-9+.-]*:[^<>\n]*)>/.exec(e.slice(a));
323
+ if (n !== null) {
324
+ let e = sanitizeDestination(n[1], t.protocols);
325
+ if (e !== null) {
326
+ flush(), r.push({
327
+ children: [{
328
+ text: n[1],
329
+ type: "text"
330
+ }],
331
+ href: e,
332
+ type: "link"
333
+ }), a += n[0].length;
334
+ continue;
335
+ }
336
+ `${n[1]}`, i += "<", a += 1;
337
+ continue;
338
+ }
339
+ i += "<", a += 1;
340
+ } else if (n === "!" && e[a + 1] === "[" || n === "[") {
341
+ let e = linkLike(n === "!" ? a + 1 : a);
342
+ if (e !== null) {
343
+ let i = sanitizeDestination(e.dest, t.protocols);
344
+ if (flush(), i !== null) {
345
+ if (n === "!") {
346
+ let n = {
347
+ alt: inlineText(parseInline(e.label, t)),
348
+ src: i,
349
+ type: "image"
350
+ };
351
+ r.push(e.title === void 0 ? n : {
352
+ ...n,
353
+ title: e.title
354
+ });
355
+ } else {
356
+ let n = {
357
+ children: parseInline(e.label, t),
358
+ href: i,
359
+ type: "link"
360
+ };
361
+ r.push(e.title === void 0 ? n : {
362
+ ...n,
363
+ title: e.title
364
+ });
365
+ }
366
+ } else `${e.dest}`, n === "!" ? r.push({
367
+ text: inlineText(parseInline(e.label, t)),
368
+ type: "text"
369
+ }) : r.push(...parseInline(e.label, t));
370
+ a = e.end;
371
+ continue;
372
+ }
373
+ i += n, a += 1;
374
+ } else if (n === "*" || n === "_" || n === "~") {
375
+ let t = 0;
376
+ for (; e[a + t] === n;) t += 1;
377
+ if (n !== "~" || t === 2) {
378
+ let i = a === 0 ? " " : e[a - 1], o = e[a + t] ?? " ", s = !/\s/.test(o) && (!p.test(o) || /\s/.test(i) || p.test(i)), c = !/\s/.test(i) && (!p.test(i) || /\s/.test(o) || p.test(o)), l = n === "_" ? s && (!c || p.test(i)) : s, u = n === "_" ? c && (!s || p.test(o)) : c;
379
+ if (l || u) {
380
+ flush(), r.push({
381
+ canClose: u,
382
+ canOpen: l,
383
+ char: n,
384
+ count: t,
385
+ kind: "delim"
386
+ }), a += t;
387
+ continue;
388
+ }
389
+ }
390
+ i += n.repeat(t), a += t;
391
+ } else {
392
+ if ((n === "h" || n === "H") && (a === 0 || /[\s_([{]/.test(e[a - 1]))) {
393
+ let n = /^https?:\/\/\S+/.exec(e.slice(a));
394
+ if (n !== null) {
395
+ let e = n[0].replace(/[.,;:!?]+$/, "");
396
+ e.endsWith(")") && !e.includes("(") && (e = e.slice(0, -1));
397
+ let i = sanitizeDestination(e, t.protocols);
398
+ if (i !== null) {
399
+ flush(), r.push({
400
+ children: [{
401
+ text: e,
402
+ type: "text"
403
+ }],
404
+ href: i,
405
+ type: "link"
406
+ }), a += e.length;
407
+ continue;
408
+ }
409
+ }
410
+ }
411
+ if (n === "\n") {
412
+ let e = /[ \t]*$/.exec(i)?.[0] ?? "";
413
+ e !== "" && (i = i.slice(0, -e.length)), flush(), r.push(e.length >= 2 ? { type: "hardBreak" } : { type: "softBreak" }), a += 1;
414
+ } else i += n, a += 1;
415
+ }
416
+ }
417
+ flush();
418
+ let o = resolveEmphasis(r);
419
+ return --t.depth, mergeText(o);
420
+ }
421
+ function resolveEmphasis(e) {
422
+ for (let t = 0; t < e.length; t += 1) {
423
+ let n = e[t];
424
+ if (isDelim(n) && n.canClose && n.count !== 0) for (let r = t - 1; r >= 0; --r) {
425
+ let i = e[r];
426
+ if (!isDelim(i) || !i.canOpen || i.count === 0 || i.char !== n.char || n.char === "~" && (i.count !== 2 || n.count !== 2)) continue;
427
+ let a = n.char !== "~" && i.count >= 2 && n.count >= 2, o = n.char === "~" || a ? 2 : 1, s = resolveRemainder(e.slice(r + 1, t)), c = a ? {
428
+ children: s,
429
+ type: "strong"
430
+ } : n.char === "~" ? {
431
+ children: s,
432
+ type: "strikethrough"
433
+ } : {
434
+ children: s,
435
+ type: "emphasis"
436
+ }, l = [];
437
+ i.count > o && l.push({
438
+ canClose: !1,
439
+ canOpen: !0,
440
+ char: i.char,
441
+ count: i.count - o,
442
+ kind: "delim"
443
+ }), l.push(c), n.count > o && l.push({
444
+ canClose: !0,
445
+ canOpen: !1,
446
+ char: n.char,
447
+ count: n.count - o,
448
+ kind: "delim"
449
+ }), e.splice(r, t - r + 1, ...l), t = r - 1;
450
+ break;
451
+ }
452
+ }
453
+ return resolveRemainder(e);
454
+ }
455
+ function resolveRemainder(e) {
456
+ let t = [];
457
+ for (let n of e) isDelim(n) ? n.count > 0 && t.push({
458
+ text: n.char.repeat(n.count),
459
+ type: "text"
460
+ }) : t.push(n);
461
+ return mergeText(t);
462
+ }
463
+ function mergeText(e) {
464
+ let t = [];
465
+ for (let n of e) {
466
+ let e = t[t.length - 1];
467
+ n.type === "text" && e !== void 0 && e.type === "text" ? t[t.length - 1] = {
468
+ text: e.text + n.text,
469
+ type: "text"
470
+ } : t.push(n);
471
+ }
472
+ return t;
473
+ }
474
+ function deepFreeze(e) {
475
+ if (typeof e == "object" && e) {
476
+ for (let t of Object.values(e)) deepFreeze(t);
477
+ Object.freeze(e);
478
+ }
479
+ return e;
480
+ }
481
+ function parseProse(e, n = {}) {
482
+ if (e.length > t) throw new ScribeLimitError(`input exceeds the ${t}-character limit`);
483
+ let i = {
484
+ depth: 0,
485
+ protocols: new Set((n.linkProtocols ?? r).map((e) => e.toLowerCase()))
486
+ };
487
+ return deepFreeze({ blocks: parseBlocks(e.replace(/\r\n?/g, "\n").replaceAll(" ", " ").split("\n"), i) });
488
+ }
489
+ // src/render.ts
490
+ function titleAttr(e) {
491
+ return e === void 0 ? "" : ` title="${escapeHtml(e)}"`;
492
+ }
493
+ function inlineHtml(e) {
494
+ let t = "";
495
+ for (let n of e) switch (n.type) {
496
+ case "text":
497
+ t += escapeHtml(n.text);
498
+ break;
499
+ case "codeSpan":
500
+ t += `<code>${escapeHtml(n.text)}</code>`;
501
+ break;
502
+ case "strong":
503
+ t += `<strong>${inlineHtml(n.children)}</strong>`;
504
+ break;
505
+ case "emphasis":
506
+ t += `<em>${inlineHtml(n.children)}</em>`;
507
+ break;
508
+ case "strikethrough":
509
+ t += `<del>${inlineHtml(n.children)}</del>`;
510
+ break;
511
+ case "link":
512
+ t += `<a href="${escapeHtml(n.href)}"${titleAttr(n.title)}>${inlineHtml(n.children)}</a>`;
513
+ break;
514
+ case "image":
515
+ t += `<img src="${escapeHtml(n.src)}" alt="${escapeHtml(n.alt)}"${titleAttr(n.title)}>`;
516
+ break;
517
+ case "softBreak":
518
+ t += "\n";
519
+ break;
520
+ case "hardBreak": t += "<br />\n";
521
+ }
522
+ return t;
523
+ }
524
+ function headingAttrs(e, t, n) {
525
+ return n ? ` id="${t.slug(inlineText(e))}"` : "";
526
+ }
527
+ function blocksHtml(e, t, n, r) {
528
+ let i = [];
529
+ for (let a of e) switch (a.type) {
530
+ case "paragraph": {
531
+ let e = inlineHtml(a.children);
532
+ i.push(r ? e : `<p>${e}</p>`);
533
+ break;
534
+ }
535
+ case "heading": {
536
+ let e = a.level;
537
+ i.push(`<h${e}${headingAttrs(a.children, t, n)}>${inlineHtml(a.children)}</h${e}>`);
538
+ break;
539
+ }
540
+ case "codeBlock": {
541
+ let e = a.language === void 0 ? "" : ` class="language-${escapeHtml(a.language)}"`;
542
+ i.push(`<pre><code${e}>${escapeHtml(a.text)}\n</code></pre>`);
543
+ break;
544
+ }
545
+ case "blockquote":
546
+ i.push(`<blockquote>\n${blocksHtml(a.children, t, n, !1)}\n</blockquote>`);
547
+ break;
548
+ case "list": {
549
+ let e = a.ordered ? "ol" : "ul", r = a.ordered && a.start !== 1 ? ` start="${a.start}"` : "", o = a.items.map((e) => `<li>${blocksHtml(e.children, t, n, a.tight)}</li>`).join("\n");
550
+ i.push(`<${e}${r}>\n${o}\n</${e}>`);
551
+ break;
552
+ }
553
+ case "thematicBreak":
554
+ i.push("<hr />");
555
+ break;
556
+ case "table": {
557
+ let cellAttrs = (e) => {
558
+ let t = a.alignments[e];
559
+ return t === void 0 || t === "left" ? "" : ` align="${t}"`;
560
+ }, e = `<tr>${a.header.map((e, t) => `<th${cellAttrs(t)}>${inlineHtml(e)}</th>`).join("")}</tr>`, t = a.rows.map((e) => `<tr>${e.map((e, t) => `<td${cellAttrs(t)}>${inlineHtml(e)}</td>`).join("")}</tr>`).join("\n");
561
+ i.push(`<table>\n<thead>\n${e}\n</thead>\n<tbody>\n${t.length === 0 ? "" : `${t}\n`}</tbody>\n</table>`);
562
+ break;
563
+ }
564
+ }
565
+ return i.join("\n");
566
+ }
567
+ function renderProseHtml(e, t = {}) {
568
+ return blocksHtml(e.blocks, createSlugger(), t.headingIds === !0, !1);
569
+ }
570
+ function inlineDom(e, t) {
571
+ let n = [];
572
+ for (let r of t) switch (r.type) {
573
+ case "text":
574
+ n.push(e.createTextNode(r.text));
575
+ break;
576
+ case "codeSpan": {
577
+ let t = e.createElement("code");
578
+ t.append(e.createTextNode(r.text)), n.push(t);
579
+ break;
580
+ }
581
+ case "strong":
582
+ case "emphasis":
583
+ case "strikethrough": {
584
+ let t = e.createElement(r.type === "strikethrough" ? "del" : r.type === "strong" ? "strong" : "em");
585
+ t.append(...inlineDom(e, r.children)), n.push(t);
586
+ break;
587
+ }
588
+ case "link": {
589
+ let t = e.createElement("a");
590
+ t.setAttribute("href", r.href), r.title !== void 0 && t.setAttribute("title", r.title), t.append(...inlineDom(e, r.children)), n.push(t);
591
+ break;
592
+ }
593
+ case "image": {
594
+ let t = e.createElement("img");
595
+ t.setAttribute("src", r.src), t.setAttribute("alt", r.alt), r.title !== void 0 && t.setAttribute("title", r.title), n.push(t);
596
+ break;
597
+ }
598
+ case "softBreak":
599
+ n.push(e.createTextNode("\n"));
600
+ break;
601
+ case "hardBreak": n.push(e.createElement("br"));
602
+ }
603
+ return n;
604
+ }
605
+ function blocksDom(e, t, n, r, i) {
606
+ let a = e.createDocumentFragment();
607
+ for (let o of t) switch (o.type) {
608
+ case "paragraph":
609
+ if (i) a.append(...inlineDom(e, o.children));
610
+ else {
611
+ let t = e.createElement("p");
612
+ t.append(...inlineDom(e, o.children)), a.append(t);
613
+ }
614
+ break;
615
+ case "heading": {
616
+ let t = e.createElement(`h${o.level}`);
617
+ r && t.setAttribute("id", n.slug(inlineText(o.children))), t.append(...inlineDom(e, o.children)), a.append(t);
618
+ break;
619
+ }
620
+ case "codeBlock": {
621
+ let t = e.createElement("pre"), n = e.createElement("code");
622
+ o.language !== void 0 && n.classList.add(`language-${o.language}`), n.append(e.createTextNode(o.text)), t.append(n), a.append(t);
623
+ break;
624
+ }
625
+ case "blockquote": {
626
+ let t = e.createElement("blockquote");
627
+ t.append(blocksDom(e, o.children, n, r, !1)), a.append(t);
628
+ break;
629
+ }
630
+ case "list": {
631
+ let t = e.createElement(o.ordered ? "ol" : "ul");
632
+ o.ordered && o.start !== 1 && t.setAttribute("start", String(o.start));
633
+ for (let i of o.items) {
634
+ let a = e.createElement("li");
635
+ a.append(blocksDom(e, i.children, n, r, o.tight)), t.append(a);
636
+ }
637
+ a.append(t);
638
+ break;
639
+ }
640
+ case "thematicBreak":
641
+ a.append(e.createElement("hr"));
642
+ break;
643
+ case "table": {
644
+ let t = e.createElement("table"), n = e.createElement("thead"), r = e.createElement("tr");
645
+ o.header.forEach((t, n) => {
646
+ let i = e.createElement("th"), a = o.alignments[n];
647
+ a !== void 0 && a !== "left" && i.setAttribute("align", a), i.append(...inlineDom(e, t)), r.append(i);
648
+ }), n.append(r);
649
+ let i = e.createElement("tbody");
650
+ for (let t of o.rows) {
651
+ let n = e.createElement("tr");
652
+ t.forEach((t, r) => {
653
+ let i = e.createElement("td"), a = o.alignments[r];
654
+ a !== void 0 && a !== "left" && i.setAttribute("align", a), i.append(...inlineDom(e, t)), n.append(i);
655
+ }), i.append(n);
656
+ }
657
+ t.append(n, i), a.append(t);
658
+ break;
659
+ }
660
+ }
661
+ return a;
662
+ }
663
+ function renderProseDom(e, t = {}) {
664
+ return blocksDom(document, e.blocks, createSlugger(), t.headingIds === !0, !1);
665
+ }
666
+ function blockText(e) {
667
+ switch (e.type) {
668
+ case "paragraph": return inlineText(e.children);
669
+ case "heading": return inlineText(e.children);
670
+ case "codeBlock": return e.text;
671
+ case "blockquote": return e.children.map(blockText).join("\n\n");
672
+ case "list": return e.items.map((t, n) => (e.ordered ? `${e.start + n}. ` : "- ") + t.children.map(blockText).join("\n\n")).join("\n");
673
+ case "thematicBreak": return "";
674
+ case "table": return [e.header, ...e.rows].map((e) => e.map((e) => inlineText(e)).join(" | ")).join("\n");
675
+ }
676
+ }
677
+ function proseToText(e) {
678
+ return e.blocks.map(blockText).filter((e) => e !== "").join("\n\n");
679
+ }
680
+ function proseOutline(e, t = {}) {
681
+ return collectOutline(e.blocks, createSlugger(), t.maxLevel ?? 6);
682
+ }
683
+ export { ScribeError, ScribeLimitError, parseProse, proseOutline, proseToText, renderProseDom, renderProseHtml };
684
+
685
+ //# sourceMappingURL=scribe.js.map