@smartdoca/markdown 0.4.3 → 0.4.5

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.
Files changed (45) hide show
  1. package/README.md +10 -1
  2. package/README.zh-CN.md +10 -1
  3. package/dist/chunks/index-Cd3grnUT.js +2815 -0
  4. package/dist/chunks/index-DQLDZhUI.cjs +86 -0
  5. package/dist/chunks/pdf-file-impl-CMlXt-sQ.cjs +8 -0
  6. package/dist/chunks/pdf-file-impl-Hh_4oE89.js +622 -0
  7. package/dist/index.cjs +1 -51
  8. package/dist/index.js +48 -2351
  9. package/dist/style.css +1 -1
  10. package/dist/types/components/ImageLightbox.d.ts +13 -0
  11. package/dist/types/components/ImageLightbox.d.ts.map +1 -0
  12. package/dist/types/components/MarkdownPreview.d.ts +4 -1
  13. package/dist/types/components/MarkdownPreview.d.ts.map +1 -1
  14. package/dist/types/components/MermaidDiagram.d.ts.map +1 -1
  15. package/dist/types/editor/i18n.d.ts +777 -3
  16. package/dist/types/editor/i18n.d.ts.map +1 -1
  17. package/dist/types/editor/image-preview.d.ts +30 -0
  18. package/dist/types/editor/image-preview.d.ts.map +1 -0
  19. package/dist/types/editor/locales/es.d.ts +130 -0
  20. package/dist/types/editor/locales/es.d.ts.map +1 -0
  21. package/dist/types/editor/locales/fr.d.ts +130 -0
  22. package/dist/types/editor/locales/fr.d.ts.map +1 -0
  23. package/dist/types/editor/locales/ja.d.ts +130 -0
  24. package/dist/types/editor/locales/ja.d.ts.map +1 -0
  25. package/dist/types/editor/locales/ko.d.ts +130 -0
  26. package/dist/types/editor/locales/ko.d.ts.map +1 -0
  27. package/dist/types/editor/locales/ru.d.ts +130 -0
  28. package/dist/types/editor/locales/ru.d.ts.map +1 -0
  29. package/dist/types/editor/locales/zh-Hant.d.ts +130 -0
  30. package/dist/types/editor/locales/zh-Hant.d.ts.map +1 -0
  31. package/dist/types/editor/locales.d.ts +771 -0
  32. package/dist/types/editor/locales.d.ts.map +1 -0
  33. package/dist/types/editor/pdf-contract.d.ts +77 -0
  34. package/dist/types/editor/pdf-contract.d.ts.map +1 -0
  35. package/dist/types/editor/pdf-file-impl.d.ts +7 -0
  36. package/dist/types/editor/pdf-file-impl.d.ts.map +1 -0
  37. package/dist/types/editor/pdf-file.d.ts +4 -78
  38. package/dist/types/editor/pdf-file.d.ts.map +1 -1
  39. package/dist/types/lib/CollaborativeMarkdownEditor.d.ts +3 -0
  40. package/dist/types/lib/CollaborativeMarkdownEditor.d.ts.map +1 -1
  41. package/dist/types/lib/index.d.ts +1 -0
  42. package/dist/types/lib/index.d.ts.map +1 -1
  43. package/docs/COLLABORATION.md +4 -0
  44. package/docs/IMAGE_PREVIEW.md +62 -0
  45. package/package.json +3 -2
@@ -0,0 +1,622 @@
1
+ import { getDocument as ht, PasswordResponses as et, OPS as Q, ImageKind as nt } from "pdfjs-dist/legacy/build/pdf.mjs";
2
+ import "pdfjs-dist/legacy/build/pdf.worker.mjs";
3
+ import { PDFDocument as ut, StandardFonts as Y, rgb as _, PDFString as it } from "pdf-lib";
4
+ import gt from "@pdf-lib/fontkit";
5
+ import { b as ct, P as U, a as ot, h as mt, D as pt, c as wt, d as xt, e as yt } from "./index-Cd3grnUT.js";
6
+ function X(i, t) {
7
+ if (i?.aborted) throw new U("ABORTED", t("pdf.aborted"));
8
+ }
9
+ function J(i, t, n, e) {
10
+ const r = i ?? t;
11
+ if (!Number.isSafeInteger(r) || r <= 0) throw new RangeError(e("validation.positiveInteger", { name: n }));
12
+ return r;
13
+ }
14
+ function D(i, t) {
15
+ i.some((n) => n.code === t.code && n.page === t.page && n.path === t.path) || i.push(t);
16
+ }
17
+ function lt(i) {
18
+ if (/^#/u.test(i)) return !0;
19
+ try {
20
+ const t = new URL(i);
21
+ return t.protocol === "http:" || t.protocol === "https:" || t.protocol === "mailto:";
22
+ } catch {
23
+ return !1;
24
+ }
25
+ }
26
+ function bt(i) {
27
+ if (typeof i == "string")
28
+ return lt(i) ? i : void 0;
29
+ }
30
+ async function At(i, t, n, e) {
31
+ X(n, e);
32
+ try {
33
+ if ((i instanceof ArrayBuffer || ArrayBuffer.isView(i) ? i.byteLength : i.size) > t) throw new U("FILE_TOO_LARGE", e("pdf.fileTooLarge", { maxBytes: t }));
34
+ const f = i instanceof ArrayBuffer ? new Uint8Array(i) : ArrayBuffer.isView(i) ? new Uint8Array(i.buffer, i.byteOffset, i.byteLength) : new Uint8Array(await i.arrayBuffer());
35
+ if (X(n, e), f.byteLength > t) throw new U("FILE_TOO_LARGE", e("pdf.fileTooLarge", { maxBytes: t }));
36
+ return f;
37
+ } catch (r) {
38
+ throw r instanceof U ? r : new U("READ_FAILED", e("pdf.readFailed"), r);
39
+ }
40
+ }
41
+ function q(i) {
42
+ const t = Array.isArray(i.transform) ? i.transform : [1, 0, 0, 1, 0, 0], n = Number(t[4]) || 0, e = Number(t[5]) || 0, r = Math.abs(Number(i.width) || 0), f = Math.abs(Number(i.height) || Math.abs(Number(t[3]) || 10));
43
+ return [n, e - f * 0.25, n + r, e + f];
44
+ }
45
+ function ft(i, t) {
46
+ const n = Math.min(t[0], t[2]), e = Math.min(t[1], t[3]), r = Math.max(t[0], t[2]), f = Math.max(t[1], t[3]);
47
+ return i[0] <= r && i[2] >= n && i[1] <= f && i[3] >= e;
48
+ }
49
+ function Tt(i) {
50
+ const t = i.filter((e) => typeof e.str == "string" && e.str.length > 0).map((e) => ({ item: e, box: q(e) })).sort((e, r) => r.box[1] - e.box[1] || e.box[0] - r.box[0]), n = [];
51
+ for (const e of t) {
52
+ const r = (e.box[1] + e.box[3]) / 2, f = Math.max(2, (e.box[3] - e.box[1]) * 0.65);
53
+ let d = n.find((o) => Math.abs(o.y - r) <= f);
54
+ d || (d = { y: r, items: [], text: "" }, n.push(d)), d.items.push(e.item);
55
+ }
56
+ for (const e of n)
57
+ e.items.sort((r, f) => q(r)[0] - q(f)[0]), e.text = e.items.map((r) => r.str).join("").replace(/[ \t]+/gu, " ").trim();
58
+ return n.sort((e, r) => r.y - e.y);
59
+ }
60
+ function Et(i, t, n) {
61
+ const e = i.items.filter((g) => ft(q(g), t)).map((g) => g.str).join("").trim();
62
+ if (!e) return;
63
+ const r = e.replace(/[\[\]]/gu, ""), f = n.replace(/[()\\]/gu, "\\$&"), d = `[${r}](${f})`, o = i.text.indexOf(e);
64
+ o >= 0 && !i.text.includes(`](${f})`) && (i.text = i.text.slice(0, o) + d + i.text.slice(o + e.length));
65
+ }
66
+ function kt(i) {
67
+ let t = 4294967295;
68
+ for (const n of i) {
69
+ t ^= n;
70
+ for (let e = 0; e < 8; e++) t = t >>> 1 ^ (t & 1 ? 3988292384 : 0);
71
+ }
72
+ return (t ^ 4294967295) >>> 0;
73
+ }
74
+ function Z(i, t) {
75
+ const n = new Uint8Array(12 + t.length), e = new DataView(n.buffer);
76
+ e.setUint32(0, t.length);
77
+ for (let r = 0; r < 4; r++) n[4 + r] = i.charCodeAt(r);
78
+ return n.set(t, 8), e.setUint32(8 + t.length, kt(n.subarray(4, 8 + t.length))), n;
79
+ }
80
+ function Lt(i) {
81
+ const t = [new Uint8Array([120, 1])];
82
+ let n = 0;
83
+ for (; n < i.length || n === 0; ) {
84
+ const o = Math.min(65535, i.length - n), g = n + o >= i.length, l = new Uint8Array(5 + o);
85
+ l[0] = g ? 1 : 0, l[1] = o & 255, l[2] = o >>> 8;
86
+ const h = ~o & 65535;
87
+ if (l[3] = h & 255, l[4] = h >>> 8, l.set(i.subarray(n, n + o), 5), t.push(l), n += o, g) break;
88
+ }
89
+ let e = 1, r = 0;
90
+ for (const o of i)
91
+ e = (e + o) % 65521, r = (r + e) % 65521;
92
+ t.push(new Uint8Array([r >>> 8 & 255, r & 255, e >>> 8 & 255, e & 255]));
93
+ const f = new Uint8Array(t.reduce((o, g) => o + g.length, 0));
94
+ let d = 0;
95
+ for (const o of t)
96
+ f.set(o, d), d += o.length;
97
+ return f;
98
+ }
99
+ function _t(i) {
100
+ const t = Number(i?.width), n = Number(i?.height), e = i?.data instanceof Uint8Array ? i.data : ArrayBuffer.isView(i?.data) ? new Uint8Array(i.data.buffer, i.data.byteOffset, i.data.byteLength) : void 0;
101
+ if (!Number.isInteger(t) || !Number.isInteger(n) || t <= 0 || n <= 0 || !e) return;
102
+ const r = i.kind === nt.GRAYSCALE_1BPP ? 1 : i.kind === nt.RGB_24BPP ? 3 : 4;
103
+ if (r === 1 && e.length < t * n || r > 1 && e.length < t * n * r) return;
104
+ const f = new Uint8Array(n * (t * 4 + 1));
105
+ for (let T = 0; T < n; T++) {
106
+ f[T * (t * 4 + 1)] = 0;
107
+ for (let I = 0; I < t; I++) {
108
+ const F = T * t * r + I * r, B = T * (t * 4 + 1) + 1 + I * 4, N = e[F];
109
+ f[B] = N, f[B + 1] = r === 1 ? N : e[F + 1], f[B + 2] = r === 1 ? N : e[F + 2], f[B + 3] = r === 4 ? e[F + 3] : 255;
110
+ }
111
+ }
112
+ const d = new Uint8Array(13);
113
+ new DataView(d.buffer).setUint32(0, t), new DataView(d.buffer).setUint32(4, n), d[8] = 8, d[9] = 6;
114
+ const o = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]), g = Lt(f), l = Z("IHDR", d), h = Z("IDAT", g), R = Z("IEND", new Uint8Array()), P = new Uint8Array(o.length + l.length + h.length + R.length);
115
+ let b = 0;
116
+ return P.set(o, b), b += o.length, P.set(l, b), b += l.length, P.set(h, b), b += h.length, P.set(R, b), P;
117
+ }
118
+ async function Pt(i, t) {
119
+ if (typeof OffscreenCanvas < "u") {
120
+ const n = new OffscreenCanvas(i, t);
121
+ return { canvas: n, context: n.getContext("2d") };
122
+ }
123
+ if (typeof document < "u") {
124
+ const n = document.createElement("canvas");
125
+ return n.width = i, n.height = t, { canvas: n, context: n.getContext("2d") };
126
+ }
127
+ try {
128
+ const r = (await import(["@napi-rs", "canvas"].join("/"))).createCanvas(i, t);
129
+ return { canvas: r, context: r.getContext("2d") };
130
+ } catch {
131
+ return;
132
+ }
133
+ }
134
+ function It(i, t, n, e) {
135
+ if (i instanceof U) return i;
136
+ const r = String(i?.message || i || "");
137
+ if (/password|encrypted|encryptedpdf/iu.test(r)) return new U("PASSWORD_REQUIRED", e("pdf.passwordRequired"), i);
138
+ const f = r.replace(/\s+/gu, " ").trim().slice(0, 180);
139
+ return new U(t, f ? e("pdf.errorDetail", { message: n, detail: f }) : n, i);
140
+ }
141
+ async function Ct(i, t = {}) {
142
+ const n = ct(t.locale, t.messages), e = J(t.maxBytes, pt, "maxBytes", n), r = J(t.maxPages, xt, "maxPages", n), f = J(t.maxImagePixels, yt, "maxImagePixels", n), d = J(t.timeoutMs, wt, "timeoutMs", n), o = await At(i, e, t.signal, n), g = [];
143
+ let l, h, R;
144
+ try {
145
+ const P = new Promise((E, O) => {
146
+ l = setTimeout(() => O(new U("TIMEOUT", n("pdf.timeout", { timeoutMs: d }))), d);
147
+ });
148
+ h = ht({ data: o, disableAutoFetch: !0, disableStream: !0, isOffscreenCanvasSupported: !1, isImageDecoderSupported: !1, useSystemFonts: !1, verbosity: 0 }), h.onPassword = (E, O) => {
149
+ (O === et.NEED_PASSWORD || O === et.INCORRECT_PASSWORD) && (R = new U("PASSWORD_REQUIRED", n("pdf.passwordRequired")), h.destroy().catch(() => {
150
+ }));
151
+ };
152
+ const b = await K(h.promise, P, t.signal, n);
153
+ if (b.numPages > r) throw new U("PAGE_LIMIT_EXCEEDED", n("pdf.pageLimit", { maxPages: r }));
154
+ const T = [], I = [];
155
+ for (let E = 1; E <= b.numPages; E++) {
156
+ X(t.signal, n);
157
+ const O = await K(b.getPage(E), P, t.signal, n), H = await K(O.getTextContent({ normalizeWhitespace: !1, disableCombineTextItems: !1 }), P, t.signal, n), M = Tt(H.items), s = await K(O.getAnnotations({ intent: "display" }), P, t.signal, n);
158
+ for (const a of s) {
159
+ const p = a.url ?? a.unsafeUrl, x = bt(p) ?? (typeof a.dest == "string" && a.dest.startsWith("#") ? a.dest : void 0);
160
+ if (!x) {
161
+ (p || a.dest) && D(g, { code: p ? "UNSAFE_URL_SKIPPED" : "EXTERNAL_URL_SKIPPED", message: n("pdf.unsafeLink"), page: E });
162
+ continue;
163
+ }
164
+ const y = M.find((L) => L.items.some((m) => ft(q(m), a.rect || [])));
165
+ y && Et(y, a.rect || [], x);
166
+ }
167
+ const u = M.map((a) => a.text).filter(Boolean), k = M.flatMap((a) => a.items.map((p) => Math.round(q(p)[0] / 12)));
168
+ new Set(k).size > 2 && M.length > 4 && D(g, { code: "MULTI_COLUMN_LAYOUT", message: n("pdf.multiColumn"), page: E });
169
+ const c = [];
170
+ try {
171
+ const a = await K(O.getOperatorList({ intent: "display" }), P, t.signal, n), p = [];
172
+ let x, y = 0;
173
+ const L = (m, w) => {
174
+ try {
175
+ const A = Number(m?.width) || 0, v = Number(m?.height) || 0;
176
+ if (A * v > f) throw new U("IMAGE_PIXEL_LIMIT", n("pdf.imagePixelLimit", { maxImagePixels: f }));
177
+ const S = _t(m);
178
+ if (!S) throw new Error(n("pdf.pngFailed"));
179
+ c.push({ key: `pdf-resource/${E}-${w}.png`, bytes: S, filename: `page-${E}-image-${w}.png`, mimeType: "image/png" });
180
+ } catch (A) {
181
+ A instanceof U && (x = A), D(g, { code: "IMAGE_EXTRACT_FAILED", message: n("pdf.imageSkipped"), page: E });
182
+ }
183
+ };
184
+ for (let m = 0; m < a.fnArray.length; m++) {
185
+ const w = a.fnArray[m], A = a.argsArray[m];
186
+ if (!(w === Q.paintImageXObject || w === Q.paintInlineImageXObject || w === Q.paintImageMaskXObject) || !A?.[0]) continue;
187
+ y++;
188
+ const S = y;
189
+ if (typeof A[0] == "string") {
190
+ const $ = A[0];
191
+ p.push(new Promise((C) => {
192
+ O.objs.get($, (W) => {
193
+ L(W, S), C();
194
+ });
195
+ }));
196
+ } else
197
+ L(A[0], S);
198
+ }
199
+ if (p.length) {
200
+ const m = O.getViewport({ scale: 1 }), w = await Pt(Math.max(1, Math.ceil(m.width)), Math.max(1, Math.ceil(m.height)));
201
+ if (w?.context) {
202
+ if (await O.render({ canvasContext: w.context, canvas: w.canvas, viewport: m, annotationMode: 0 }).promise, await Promise.all(p), x) throw x;
203
+ } else
204
+ D(g, { code: "IMAGE_EXTRACT_FAILED", message: n("pdf.canvasUnavailable"), page: E });
205
+ }
206
+ if (x) throw x;
207
+ } catch (a) {
208
+ if (a instanceof U) throw a;
209
+ D(g, { code: "IMAGE_EXTRACT_FAILED", message: n("pdf.imageParseFailed"), page: E });
210
+ }
211
+ if (c.sort((a, p) => a.key.localeCompare(p.key, void 0, { numeric: !0 })), I.push(...c), !u.length && !c.length && D(g, { code: "NO_TEXT_EXTRACTED", message: n("pdf.noTextPage"), page: E }), c.length) {
212
+ u.length && u.push("");
213
+ for (const a of c) u.push(`![${a.alt || n("pdf.imageAlt")}](${a.key})`);
214
+ }
215
+ T.push(u.join(`
216
+ `)), u.some((a) => a.includes("|")) && D(g, { code: "COMPLEX_TABLE", message: n("pdf.complexTableImport"), page: E }), O.cleanup?.();
217
+ }
218
+ const B = (await b.getMetadata().catch(() => {
219
+ }))?.info || {}, N = B.Title || B.Author ? { title: B.Title || void 0, author: B.Author || void 0 } : void 0;
220
+ return T.some(Boolean) || D(g, { code: "NO_TEXT_EXTRACTED", message: n("pdf.noText") }), await b.destroy(), { ok: !0, markdown: T.join(`
221
+
222
+ `), resources: I, metadata: N, warnings: g };
223
+ } catch (P) {
224
+ try {
225
+ await h?.destroy?.();
226
+ } catch {
227
+ }
228
+ throw R || It(P, "INVALID_PDF", n("pdf.invalid"), n);
229
+ } finally {
230
+ l && clearTimeout(l);
231
+ }
232
+ }
233
+ async function K(i, t, n, e) {
234
+ if (X(n, e), !n) return Promise.race([i, t]);
235
+ let r;
236
+ const f = new Promise((d, o) => {
237
+ r = () => o(new U("ABORTED", e("pdf.aborted"))), n.addEventListener("abort", r, { once: !0 });
238
+ });
239
+ try {
240
+ return await Promise.race([i, t, f]);
241
+ } finally {
242
+ r && n.removeEventListener("abort", r);
243
+ }
244
+ }
245
+ async function rt(i, t, n) {
246
+ if (X(t, n), !t) return i;
247
+ let e;
248
+ const r = new Promise((f, d) => {
249
+ e = () => d(new U("ABORTED", n("pdf.aborted"))), t.addEventListener("abort", e, { once: !0 });
250
+ });
251
+ try {
252
+ return await Promise.race([i, r]);
253
+ } finally {
254
+ e && t.removeEventListener("abort", e);
255
+ }
256
+ }
257
+ function G(i, t, n) {
258
+ const e = [], r = /(!?)\[([^\]]*)\]\(([^\s)]+)(?:\s+["']([^"']*)["'])?\)|(`+)([\s\S]*?)\5|(\*\*|__)([\s\S]*?)\7|(~~)([\s\S]*?)\9|(\*|_)([^\n]*?)\11|(\$\$?)([^$]+)\12/gu;
259
+ let f = 0;
260
+ for (const o of i.matchAll(r)) {
261
+ const g = o.index ?? 0;
262
+ if (g > f && e.push({ kind: "text", text: i.slice(f, g) }), o[1] === "!") e.push({ kind: "image", text: o[2], alt: o[2], path: o[3] });
263
+ else if (o[2] !== void 0) {
264
+ const l = o[3];
265
+ lt(l) ? e.push({ kind: "link", text: o[2], href: l }) : (e.push({ kind: "text", text: o[2] }), t.push({ code: "UNSAFE_URL_SKIPPED", message: n("pdf.unsafeLinkText"), path: l }));
266
+ } else o[6] !== void 0 ? e.push({ kind: "code", text: o[6] }) : o[8] !== void 0 ? e.push({ kind: "bold", text: o[8] }) : o[10] !== void 0 ? e.push({ kind: "strike", text: o[10] }) : o[12] !== void 0 ? e.push({ kind: "italic", text: o[12] }) : o[14] !== void 0 && (e.push({ kind: "math", text: o[14] }), D(t, { code: "MATH_DEGRADED", message: n("pdf.mathDegraded") }));
267
+ f = g + o[0].length;
268
+ }
269
+ f < i.length && e.push({ kind: "text", text: i.slice(f) });
270
+ const d = (o) => o.replace(/\\([\\`*{}\[\]()#+.!_>|~-])/gu, "$1");
271
+ for (const o of e) o.text = d(o.text);
272
+ return e.length ? e : [{ kind: "text", text: d(i) }];
273
+ }
274
+ function Rt(i, t, n) {
275
+ const e = i.replace(/<!--([\s\S]*?)-->/gu, "").replace(/\r\n?/gu, `
276
+ `).split(`
277
+ `), r = [], f = (g) => !!(e[g]?.includes("|") && e[g + 1] && /^\s*\|?\s*:?-{3,}:?\s*(?:\|\s*:?-{3,}:?\s*)+\|?\s*$/u.test(e[g + 1])), d = (g) => {
278
+ const l = e[g];
279
+ return !!(l && (f(g) || /^\s*(```+|~~~+)/u.test(l) || /^\s{0,3}#{1,6}\s+/u.test(l) || /^\s*(---+|\*\*\*+|___+)\s*$/u.test(l) || /^\s*!\[[^\]]*\]\(/u.test(l) || /^\s*([-+*]|\d+[.)])\s+/u.test(l) || /^\s*>\s?/u.test(l)));
280
+ };
281
+ let o = 0;
282
+ for (; o < e.length; ) {
283
+ const g = e[o];
284
+ if (!g.trim()) {
285
+ o++;
286
+ continue;
287
+ }
288
+ const l = g.match(/^\s*(```+|~~~+)[ \t]*(.*?)\s*$/u);
289
+ if (l) {
290
+ const T = [], I = l[1];
291
+ for (o++; o < e.length && !new RegExp(`^\\s*${I[0]}{${I.length},}\\s*$`, "u").test(e[o]); ) T.push(e[o++]);
292
+ o < e.length && o++;
293
+ const F = l[2]?.trim().split(/\s+|\|/u)[0] || void 0;
294
+ r.push({ kind: "code", text: T.join(`
295
+ `), language: F });
296
+ continue;
297
+ }
298
+ const h = g.match(/^\s{0,3}(#{1,6})\s+(.+?)\s*#*\s*$/u);
299
+ if (h) {
300
+ r.push({ kind: "heading", level: h[1].length, text: h[2] }), o++;
301
+ continue;
302
+ }
303
+ if (/^\s*(---+|\*\*\*+|___+)\s*$/u.test(g)) {
304
+ r.push({ kind: "rule" }), o++;
305
+ continue;
306
+ }
307
+ const R = g.match(/^\s*!\[([^\]]*)\]\(([^\s)]+)(?:\s+["']([^"']*)["'])?\)\s*$/u);
308
+ if (R) {
309
+ r.push({ kind: "image", alt: R[1], path: R[2] }), o++;
310
+ continue;
311
+ }
312
+ const P = g.match(/^\s*([-+*]|\d+[.)])\s+(.+)$/u);
313
+ if (P) {
314
+ const T = /^\d/u.test(P[1]), I = [];
315
+ for (; o < e.length; ) {
316
+ const F = e[o].match(/^\s*([-+*]|\d+[.)])\s+(.+)$/u);
317
+ if (!F || /^\d/u.test(F[1]) !== T) break;
318
+ I.push(F[2]), o++;
319
+ }
320
+ r.push({ kind: "list", ordered: T, items: I });
321
+ continue;
322
+ }
323
+ if (f(o)) {
324
+ const T = [], I = (F) => {
325
+ const B = F.trim().replace(/^\||\|$/gu, ""), N = [];
326
+ let E = "", O = !1, H = 0;
327
+ for (const M of B) {
328
+ if (O) {
329
+ E += M, O = !1;
330
+ continue;
331
+ }
332
+ if (M === "\\") {
333
+ O = !0, E += M;
334
+ continue;
335
+ }
336
+ if (M === "`") {
337
+ H = H ? 0 : 1, E += M;
338
+ continue;
339
+ }
340
+ if (M === "|" && !H) {
341
+ N.push(E.trim()), E = "";
342
+ continue;
343
+ }
344
+ E += M;
345
+ }
346
+ return O && (E += "\\"), N.push(E.trim()), N.map((M) => M.replace(/\\\|/gu, "|"));
347
+ };
348
+ for (T.push(I(g)), o += 2; o < e.length && e[o].includes("|") && e[o].trim(); )
349
+ T.push(I(e[o++]));
350
+ r.push({ kind: "table", rows: T });
351
+ continue;
352
+ }
353
+ if (/^\s*>\s?/u.test(g)) {
354
+ const T = [];
355
+ for (; o < e.length && /^\s*>\s?/u.test(e[o]); ) T.push(e[o++].replace(/^\s*>\s?/u, ""));
356
+ r.push({ kind: "quote", text: T.join(`
357
+ `) });
358
+ continue;
359
+ }
360
+ const b = [g.trim()];
361
+ for (o++; o < e.length && e[o].trim() && !d(o); ) b.push(e[o++].trim());
362
+ r.push({ kind: "paragraph", text: b.join(" ") });
363
+ }
364
+ return r.some((g) => g.kind === "table") && D(t, { code: "COMPLEX_TABLE", message: n("pdf.complexTableExport") }), r;
365
+ }
366
+ function st(i) {
367
+ return i instanceof Uint8Array ? Promise.resolve(i) : i.arrayBuffer().then((t) => new Uint8Array(t));
368
+ }
369
+ function St(i) {
370
+ const t = (i || "document.pdf").trim() || "document.pdf";
371
+ return /\.pdf$/iu.test(t) ? t : `${t}.pdf`;
372
+ }
373
+ function Ot(i) {
374
+ return typeof i == "number" ? { top: i, right: i, bottom: i, left: i } : { top: i?.top ?? 56.7, right: i?.right ?? 56.7, bottom: i?.bottom ?? 56.7, left: i?.left ?? 56.7 };
375
+ }
376
+ function V(i, t) {
377
+ return i.kind === "code" ? t.code : i.kind === "bold" ? t.bold : i.kind === "italic" ? t.italic : (i.kind === "strike", t.normal);
378
+ }
379
+ function j(i, t, n, e, r) {
380
+ try {
381
+ return t.widthOfTextAtSize(i, n), i;
382
+ } catch {
383
+ return D(e, { code: "FONT_FALLBACK", message: r("pdf.fontFallback") }), Array.from(i, (f) => {
384
+ try {
385
+ return t.widthOfTextAtSize(f, n), f;
386
+ } catch {
387
+ return "?";
388
+ }
389
+ }).join("");
390
+ }
391
+ }
392
+ async function at(i, t, n, e) {
393
+ const r = n === "image/jpeg" || n === "image/jpg" || /\.jpe?g$/iu.test(e) || t[0] === 255 && t[1] === 216;
394
+ try {
395
+ return r ? await i.embedJpg(t) : await i.embedPng(t);
396
+ } catch {
397
+ return;
398
+ }
399
+ }
400
+ async function Wt(i, t = {}) {
401
+ const n = ct(t.locale, t.messages);
402
+ if (typeof i != "string") throw new TypeError(n("validation.markdownString"));
403
+ X(t.signal, n);
404
+ const e = [], r = await ut.create(), f = t.pageSize || [595.28, 841.89], d = Ot(t.margin);
405
+ if ([...f, d.top, d.right, d.bottom, d.left].some((s) => !Number.isFinite(s) || s <= 0)) throw new RangeError(n("pdf.pageGeometry"));
406
+ const o = {};
407
+ if (t.fontBytes) {
408
+ r.registerFontkit(gt);
409
+ const s = t.fontBytes instanceof ArrayBuffer ? new Uint8Array(t.fontBytes) : t.fontBytes, u = await r.embedFont(s, { subset: t.fontSubset ?? !0 });
410
+ o.normal = o.bold = o.italic = o.code = u;
411
+ } else
412
+ o.normal = await r.embedFont(Y.Helvetica), o.bold = await r.embedFont(Y.HelveticaBold), o.italic = await r.embedFont(Y.HelveticaOblique), o.code = await r.embedFont(Y.Courier), /[^\u0000-\u00ff]/u.test(i) && D(e, { code: "FONT_FALLBACK", message: n("pdf.fontFallbackCjk") });
413
+ const g = Rt(i, e, n);
414
+ let l = r.addPage(f), h = f[1] - d.top;
415
+ const R = f[0] - d.left - d.right, P = (s) => s * 1.45, b = (s) => {
416
+ h - s < d.bottom && (l = r.addPage(f), h = f[1] - d.top);
417
+ }, T = (s, u, k = o.normal, c = 0, a = _(0, 0, 0)) => {
418
+ const p = j(s, k, u, e, n);
419
+ b(P(u)), l.drawText(p, { x: d.left + c, y: h - u, size: u, font: k, color: a }), h -= P(u);
420
+ }, I = (s, u, k = 0) => {
421
+ let c = d.left + k, a = !0;
422
+ const p = f[0] - d.right, x = (y, L) => {
423
+ const m = V(y, o), w = j(L, m, u, e, n), A = m.widthOfTextAtSize(w, u);
424
+ if (c + A > p && c > d.left + k && (h -= P(u), b(P(u)), c = d.left + k), l.drawText(w, { x: c, y: h - u, size: u, font: m, color: y.kind === "link" ? _(0.05, 0.25, 0.7) : _(0, 0, 0) }), y.kind === "link" && y.href) {
425
+ const v = l.doc.context.obj({
426
+ Type: "Annot",
427
+ Subtype: "Link",
428
+ Rect: [c, h - u * 0.15, c + A, h + u * 1.05],
429
+ Border: [0, 0, 0],
430
+ A: { Type: "Action", S: "URI", URI: it.of(y.href) }
431
+ });
432
+ l.node.addAnnot(l.doc.context.register(v));
433
+ }
434
+ y.kind === "strike" && l.drawLine({ start: { x: c, y: h - u * 0.45 }, end: { x: c + A, y: h - u * 0.45 }, thickness: 0.7, color: _(0, 0, 0) }), c += A;
435
+ };
436
+ for (const y of s) {
437
+ if (X(t.signal, n), y.kind === "image") {
438
+ x({ kind: "text", text: y.alt || "image" }, `[${y.alt || "image"}]`);
439
+ continue;
440
+ }
441
+ const L = y.text.split(/(\s+)/u);
442
+ for (const m of L)
443
+ m && x(y, m);
444
+ a = !1;
445
+ }
446
+ (!a || s.length) && (h -= P(u));
447
+ }, F = (s, u, k) => {
448
+ const c = [[]];
449
+ let a = 0;
450
+ const p = () => {
451
+ c.push([]), a = 0;
452
+ };
453
+ for (const x of s) {
454
+ const y = x.text.split(/(\s+)/u).filter(Boolean);
455
+ for (const L of y) {
456
+ if (/^\s+$/u.test(L)) {
457
+ if (c.at(-1)?.length) {
458
+ const v = V(x, o).widthOfTextAtSize(" ", u);
459
+ a + v <= k && (c.at(-1).push({ ...x, text: " " }), a += v);
460
+ }
461
+ continue;
462
+ }
463
+ let m = L;
464
+ for (; m; ) {
465
+ const w = V(x, o), A = j(m, w, u, e, n), v = w.widthOfTextAtSize(A, u);
466
+ if (a > 0 && a + v > k) {
467
+ p();
468
+ continue;
469
+ }
470
+ if (v <= k || Array.from(m).length === 1) {
471
+ c.at(-1).push({ ...x, text: m }), a += v, m = "";
472
+ continue;
473
+ }
474
+ let S = "", $ = 0;
475
+ for (const C of Array.from(m)) {
476
+ const W = w.widthOfTextAtSize(j(C, w, u, e, n), u);
477
+ if (S && $ + W > k) break;
478
+ S += C, $ += W;
479
+ }
480
+ S || (S = Array.from(m)[0]), c.at(-1).push({ ...x, text: S }), a += $ || w.widthOfTextAtSize(S, u), m = m.slice(S.length), m && p();
481
+ }
482
+ }
483
+ }
484
+ return c.filter((x) => x.length);
485
+ }, B = (s, u, k, c) => {
486
+ for (const a of s) {
487
+ const p = V(a, o), x = j(a.text, p, u, e, n), y = p.widthOfTextAtSize(x, u);
488
+ if (l.drawText(x, { x: k, y: c, size: u, font: p, color: a.kind === "link" ? _(0.05, 0.25, 0.7) : _(0, 0, 0) }), a.kind === "link" && a.href) {
489
+ const L = l.doc.context.obj({ Type: "Annot", Subtype: "Link", Rect: [k, c - u * 0.15, k + y, c + u * 1.05], Border: [0, 0, 0], A: { Type: "Action", S: "URI", URI: it.of(a.href) } });
490
+ l.node.addAnnot(l.doc.context.register(L));
491
+ }
492
+ k += y;
493
+ }
494
+ }, N = (s) => s ? s.startsWith("comment") || s.startsWith("quote") ? _(0.416, 0.451, 0.49) : s.startsWith("keyword") || s.startsWith("selector-tag") || s.startsWith("subst") ? _(0.843, 0.227, 0.286) : s.startsWith("string") || s.startsWith("doctag") || s.startsWith("regexp") ? _(0.012, 0.184, 0.384) : s.startsWith("title") || s.startsWith("section") || s.startsWith("selector-id") ? _(0.435, 0.259, 0.757) : s.startsWith("number") || s.startsWith("literal") || s.startsWith("variable") || s.startsWith("symbol") || s.startsWith("bullet") ? _(0, 0.361, 0.773) : s.startsWith("type") || s.startsWith("class") || s.startsWith("built_in") ? _(0.89, 0.384, 0.035) : s.startsWith("attr") || s.startsWith("attribute") || s.startsWith("name") ? _(0.133, 0.533, 0.227) : s.startsWith("meta") ? _(0.451, 0.361, 0.059) : _(0.15, 0.17, 0.2) : _(0.15, 0.17, 0.2), E = (s, u, k, c) => {
495
+ if (!s.length) return [[{ text: " " }]];
496
+ const a = [[]];
497
+ let p = 0;
498
+ const x = (y, L) => {
499
+ const m = a.at(-1), w = m.at(-1);
500
+ w && w.scope === L ? w.text += y : m.push({ text: y, scope: L });
501
+ };
502
+ for (const y of s) for (const L of Array.from(y.text)) {
503
+ const m = u.widthOfTextAtSize(j(L, u, k, e, n), k);
504
+ p > 0 && p + m > c && (a.push([]), p = 0), x(L, y.scope), p += m;
505
+ }
506
+ return a;
507
+ }, O = (s) => {
508
+ const a = _(0.965, 0.968, 0.972), p = _(0.78, 0.8, 0.84), x = (m) => {
509
+ b(12), l.drawRectangle({ x: d.left, y: h - 12, width: R, height: 12, color: a }), l.drawRectangle({ x: d.left, y: h - 12, width: 2, height: 12, color: p });
510
+ let w = d.left + 8;
511
+ for (const A of m) {
512
+ const v = j(A.text, o.code, 8.5, e, n);
513
+ l.drawText(v, { x: w, y: h - 9.5, size: 8.5, font: o.code, color: N(A.scope) }), w += o.code.widthOfTextAtSize(v, 8.5);
514
+ }
515
+ h -= 12;
516
+ };
517
+ b(12), l.drawRectangle({ x: d.left, y: h - 12, width: R, height: 12, color: a }), l.drawRectangle({ x: d.left, y: h - 12, width: 2, height: 12, color: p }), s.language && l.drawText(j(s.language, o.code, 7, e, n), { x: d.left + 8, y: h - 8.5, size: 7, font: o.code, color: _(0.38, 0.4, 0.45) }), h -= 12;
518
+ const y = R - 16, L = mt(s.text || "", s.language);
519
+ for (const m of L.lines) for (const w of E(m, o.code, 8.5, y)) x(w);
520
+ b(6), l.drawRectangle({ x: d.left, y: h - 6, width: R, height: 6, color: a }), l.drawRectangle({ x: d.left, y: h - 6, width: 2, height: 6, color: p }), h -= 12;
521
+ };
522
+ for (const s of g) {
523
+ if (X(t.signal, n), s.kind === "rule") {
524
+ b(12), l.drawLine({ start: { x: d.left, y: h - 6 }, end: { x: f[0] - d.right, y: h - 6 }, thickness: 1, color: _(0.65, 0.65, 0.65) }), h -= 18;
525
+ continue;
526
+ }
527
+ if (s.kind === "heading") {
528
+ const c = [0, 24, 20, 16, 14, 12, 11][s.level || 1];
529
+ I(G(s.text || "", e, n), c), h -= 5;
530
+ continue;
531
+ }
532
+ if (s.kind === "code") {
533
+ O(s);
534
+ continue;
535
+ }
536
+ if (s.kind === "list") {
537
+ for (let c = 0; c < (s.items || []).length; c++) {
538
+ const a = s.ordered ? `${c + 1}. ` : "• ";
539
+ I([{ kind: "text", text: a }, ...G(s.items[c], e, n)], 11, 12);
540
+ }
541
+ h -= 4;
542
+ continue;
543
+ }
544
+ if (s.kind === "quote") {
545
+ I(G(s.text || "", e, n), 11, 14), h -= 4;
546
+ continue;
547
+ }
548
+ if (s.kind === "table") {
549
+ const c = s.rows || [], a = Math.max(0, ...c.map((y) => y.length)), p = 9, x = 5;
550
+ if (c.length && a) {
551
+ const y = Array.from({ length: a }, (w, A) => {
552
+ const v = Math.max(...c.map((S) => {
553
+ const $ = S[A] || "";
554
+ return G($, e, n).reduce((C, W) => C + V(W, o).widthOfTextAtSize(j(W.text, V(W, o), p, e, n), p), 0);
555
+ }), 24);
556
+ return Math.min(260, v + x * 2);
557
+ }), L = y.reduce((w, A) => w + A, 0), m = L > R ? y.map((w) => w * R / L) : y.map((w) => w + (R - L) / a);
558
+ for (let w = 0; w < c.length; w++) {
559
+ const A = c[w], v = Array.from({ length: a }, (C, W) => F(G(A[W] || "", e, n), p, Math.max(20, m[W] - x * 2))), S = Math.max(1, ...v.map((C) => C.length)) * 12 + x * 2;
560
+ b(S);
561
+ let $ = d.left;
562
+ for (let C = 0; C < a; C++) {
563
+ const W = m[C], dt = w === 0 ? _(0.95, 0.95, 0.94) : _(1, 1, 1);
564
+ l.drawRectangle({ x: $, y: h - S, width: W, height: S, color: dt, borderColor: _(0.78, 0.78, 0.76), borderWidth: 0.5 });
565
+ const tt = v[C];
566
+ for (let z = 0; z < tt.length; z++) B(tt[z], p, $ + x, h - x - p - z * 12);
567
+ $ += W;
568
+ }
569
+ h -= S;
570
+ }
571
+ }
572
+ h -= 4;
573
+ continue;
574
+ }
575
+ if (s.kind === "image") {
576
+ if (!t.resolveResource) {
577
+ D(e, { code: "RESOURCE_UNRESOLVED", message: n("pdf.imageUnresolved"), path: s.path }), T(`[${s.alt || "image"}]`, 11);
578
+ continue;
579
+ }
580
+ try {
581
+ const c = await rt(t.resolveResource({ path: s.path || "", alt: s.alt }), t.signal, n);
582
+ X(t.signal, n);
583
+ const a = await st(c.bytes), p = await at(r, a, "", s.path || "");
584
+ if (!p) throw new Error("unsupported image");
585
+ const x = p.scaleToFit(R, 240);
586
+ b(x.height + 10), l.drawImage(p, { x: d.left, y: h - x.height, width: x.width, height: x.height }), h -= x.height + 10;
587
+ } catch (c) {
588
+ if (D(e, { code: "RESOURCE_RESOLUTION_FAILED", message: n("pdf.imageFailed"), path: s.path }), T(`[${s.alt || "image"}]`, 11), c instanceof U) throw c;
589
+ }
590
+ continue;
591
+ }
592
+ const u = G(s.text || "", e, n), k = [];
593
+ for (const c of u) if (c.kind === "image") {
594
+ if (!t.resolveResource) {
595
+ D(e, { code: "RESOURCE_UNRESOLVED", message: n("pdf.inlineImageUnresolved"), path: c.path });
596
+ continue;
597
+ }
598
+ try {
599
+ const a = await rt(t.resolveResource({ path: c.path || "", alt: c.alt }), t.signal, n);
600
+ X(t.signal, n);
601
+ const p = await at(r, await st(a.bytes), "", c.path || "");
602
+ p ? k.push(p) : D(e, { code: "RESOURCE_RESOLUTION_FAILED", message: n("pdf.inlineImageUnsupported"), path: c.path });
603
+ } catch (a) {
604
+ if (a instanceof U) throw a;
605
+ D(e, { code: "RESOURCE_RESOLUTION_FAILED", message: n("pdf.inlineImageFailed"), path: c.path });
606
+ }
607
+ }
608
+ I(u, 11);
609
+ for (const c of k) {
610
+ const a = c.scaleToFit(Math.min(R, 180), 100);
611
+ b(a.height + 8), l.drawImage(c, { x: d.left, y: h - a.height, width: a.width, height: a.height }), h -= a.height + 8;
612
+ }
613
+ h -= 4;
614
+ }
615
+ r.setTitle("Markdown PDF");
616
+ const H = await r.save(), M = H.buffer.slice(H.byteOffset, H.byteOffset + H.byteLength);
617
+ return { blob: new Blob([M], { type: ot }), fileName: St(t.fileName), mimeType: ot, warnings: e };
618
+ }
619
+ export {
620
+ Wt as exportPdfFile,
621
+ Ct as importPdfFile
622
+ };