@pretextbook/import 0.8.0 → 0.10.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.
Files changed (48) hide show
  1. package/README.md +37 -0
  2. package/dist/convert-worker.cjs +1 -1
  3. package/dist/convert-worker.js +1 -1
  4. package/dist/import-mode-CV7gCDzF.cjs +3 -0
  5. package/dist/import-mode-DQ8g5P8P.js +122 -0
  6. package/dist/import.css +1 -1
  7. package/dist/index.cjs +3 -3
  8. package/dist/index.d.ts +10 -6
  9. package/dist/index.js +524 -214
  10. package/dist/lib/assets/images.d.ts +49 -0
  11. package/dist/lib/detect-snippet-format.d.ts +32 -0
  12. package/dist/lib/import-mode.d.ts +22 -0
  13. package/dist/lib/insert/dedupe-ids.d.ts +36 -0
  14. package/dist/lib/insert/destination.d.ts +21 -0
  15. package/dist/lib/insert/index.d.ts +4 -0
  16. package/dist/lib/insert/prepare.d.ts +44 -0
  17. package/dist/lib/insert/retarget.d.ts +35 -0
  18. package/dist/lib/layout/xml-scan.d.ts +28 -0
  19. package/dist/lib/pandoc/formats.d.ts +40 -0
  20. package/dist/lib/pandoc/index.d.ts +3 -0
  21. package/dist/lib/pandoc/pandoc-engine.d.ts +24 -0
  22. package/dist/lib/pandoc/remote-pandoc.d.ts +74 -0
  23. package/dist/lib/pool/division-pool.d.ts +5 -0
  24. package/dist/lib/pool/index.d.ts +5 -1
  25. package/dist/lib/pool/place-divisions.d.ts +25 -0
  26. package/dist/lib/pool/placeholders.d.ts +4 -0
  27. package/dist/lib/pool/refs.d.ts +8 -0
  28. package/dist/lib/pool/serialize-destination.d.ts +24 -0
  29. package/dist/lib/pool/serialize-files.d.ts +1 -2
  30. package/dist/lib/pool/serialize-insert-records.d.ts +22 -0
  31. package/dist/lib/pool/serialize-insert.d.ts +44 -0
  32. package/dist/lib/pool/serialize-plus.d.ts +4 -4
  33. package/dist/lib/pool/serialize-records.d.ts +11 -0
  34. package/dist/lib/pretext-divisions.d.ts +25 -0
  35. package/dist/lib/select/divisions.d.ts +44 -0
  36. package/dist/lib/types.d.ts +82 -2
  37. package/dist/lib/upload.d.ts +56 -4
  38. package/dist/react/import-wizard.d.ts +45 -1
  39. package/dist/react/index.d.ts +1 -1
  40. package/dist/react.cjs +2 -2
  41. package/dist/react.js +655 -498
  42. package/dist/upload-BnRj1gU1.cjs +135 -0
  43. package/dist/upload-H_OCZBBn.js +7648 -0
  44. package/package.json +1 -1
  45. package/dist/import-mode-BCGuSwkM.js +0 -112
  46. package/dist/import-mode-D1gOJw3N.cjs +0 -3
  47. package/dist/upload-BmErkg8I.js +0 -7140
  48. package/dist/upload-BtS22hIH.cjs +0 -135
package/dist/index.js CHANGED
@@ -1,7 +1,65 @@
1
- import { d as w, r as I, a as _, f as E, b as T, w as x, s as v, e as C, c as k, p as y, g as b, h as A } from "./upload-BmErkg8I.js";
2
- import { C as q, L as V, i as Y, M as J, j as Q, P as Z, k as ee, l as te, m as se, n as ne, o as ae, q as oe, t as re, u as ie, v as ce, x as le, y as ue, z as pe, A as fe, B as me, D as de, E as ge, F as Pe, G as he, H as xe, I as be, J as Ee, K as Te, N as ve, O as Se, Q as Le, R as Fe, S as we, T as Ie, U as _e, V as Ce, W as ke, X as ye, Y as Ae, Z as Me, _ as je, $ as Re, a0 as $e, a1 as De, a2 as Ne, a3 as Oe, a4 as Be, a5 as We } from "./upload-BmErkg8I.js";
3
- import { a as Ke, d as ze, b as Ue, c as Ge, f as He, e as qe, g as Ve, p as Ye } from "./import-mode-BCGuSwkM.js";
4
- const M = {
1
+ import { i as K, d as W, r as H, a as U, f as y, b as C, w as S, s as R, e as G, c as q, p as V, g as $, h as Z, j as Y } from "./upload-H_OCZBBn.js";
2
+ import { C as Re, D as ke, L as De, k as Me, l as Oe, M as Ne, m as je, P as Xe, n as ze, o as Be, q as Ke, t as We, u as He, v as Ue, x as Ge, y as qe, z as Ve, A as Ze, B as Ye, E as Je, F as Qe, G as et, H as tt, I as nt, J as rt, K as ot, N as st, O as at, Q as it, R as ct, S as lt, T as ut, U as pt, V as dt, W as mt, X as ft, Y as ht, Z as gt, _ as wt, $ as xt, a0 as Tt, a1 as Pt, a2 as Et, a3 as St, a4 as bt, a5 as vt, a6 as It, a7 as _t, a8 as $t, a9 as At, aa as Ft, ab as Lt, ac as yt, ad as Ct, ae as Rt, af as kt, ag as Dt, ah as Mt, ai as Ot, aj as Nt, ak as jt, al as Xt, am as zt } from "./upload-H_OCZBBn.js";
3
+ import { D as Kt, a as Wt, d as Ht, b as Ut, c as Gt, f as qt, e as Vt, g as Zt, h as Yt, p as Jt, r as Qt } from "./import-mode-DQ8g5P8P.js";
4
+ const J = /\$(?![\s$])([^$\n]{1,200})\$/;
5
+ function Q(e) {
6
+ const t = J.exec(e);
7
+ if (!t)
8
+ return !1;
9
+ const r = t[1];
10
+ return /[\\^_{}]/.test(r) || /^[A-Za-z]'?$/.test(r.trim());
11
+ }
12
+ const ee = [
13
+ // \begin{...} / \end{...} — unambiguous.
14
+ { pattern: /\\(?:begin|end)\{[a-zA-Z*]+\}/, weight: 3 },
15
+ // Display math, either delimiter style.
16
+ { pattern: /\\\[[\s\S]*?\\\]|\$\$[\s\S]*?\$\$/, weight: 3 },
17
+ // A command taking an argument: \emph{...}, \frac{...}{...}.
18
+ { pattern: /\\[a-zA-Z]+\s*\{/, weight: 2 },
19
+ // Inline math delimiters \( ... \).
20
+ { pattern: /\\\([\s\S]*?\\\)/, weight: 2 },
21
+ // A bare command: \alpha, \ldots, \\.
22
+ { pattern: /\\[a-zA-Z]+\b/, weight: 1 },
23
+ // Label/ref machinery is a strong hint even without braces nearby.
24
+ { pattern: /\\(?:label|ref|eqref|cite)\b/, weight: 2 }
25
+ ], te = [
26
+ // ATX heading.
27
+ { pattern: /^#{1,6}\s+\S/m, weight: 3 },
28
+ // Fenced code block.
29
+ { pattern: /^\s*```/m, weight: 3 },
30
+ // Bullet list.
31
+ { pattern: /^\s*[-*+]\s+\S/m, weight: 2 },
32
+ // Ordered list.
33
+ { pattern: /^\s*\d+[.)]\s+\S/m, weight: 2 },
34
+ // Strong emphasis.
35
+ { pattern: /\*\*[^\s*][^*]*\*\*/, weight: 2 },
36
+ // Inline link.
37
+ { pattern: /\[[^\]\n]+\]\([^)\s]+\)/, weight: 2 },
38
+ // Blockquote.
39
+ { pattern: /^\s*>\s+\S/m, weight: 2 },
40
+ // Underscore emphasis, bounded so snake_case_names do not count.
41
+ { pattern: /(?:^|\s)_[^_\s][^_\n]*_(?:\s|[.,;:!?]|$)/, weight: 1 },
42
+ // Inline code span.
43
+ { pattern: /`[^`\n]+`/, weight: 1 }
44
+ ], ne = 2;
45
+ function A(e, t) {
46
+ return t.reduce(
47
+ (r, { pattern: n, weight: o }) => r + (n.test(e) ? o : 0),
48
+ 0
49
+ );
50
+ }
51
+ function re(e) {
52
+ return { latex: A(e, ee) + (Q(e) ? 2 : 0), markdown: A(e, te) };
53
+ }
54
+ function ve(e) {
55
+ const t = e.trim();
56
+ if (!t || t.startsWith("<"))
57
+ return;
58
+ const { latex: r, markdown: n } = re(t);
59
+ if (!(Math.max(r, n) < ne))
60
+ return r >= n ? "latex" : "markdown";
61
+ }
62
+ const oe = {
5
63
  png: "image/png",
6
64
  jpg: "image/jpeg",
7
65
  jpeg: "image/jpeg",
@@ -15,20 +73,45 @@ const M = {
15
73
  webp: "image/webp",
16
74
  ico: "image/vnd.microsoft.icon"
17
75
  };
18
- function j(e) {
19
- const s = e.split(".").pop()?.toLowerCase() ?? "";
20
- return M[s] ?? "application/octet-stream";
76
+ function se(e) {
77
+ const t = e.split(".").pop()?.toLowerCase() ?? "";
78
+ return oe[t] ?? "application/octet-stream";
21
79
  }
22
80
  const P = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
23
- function R(e) {
24
- const s = e.length, o = [];
25
- for (let t = 0; t < s; t += 3) {
26
- const n = e[t], c = t + 1 < s, r = t + 2 < s, l = c ? e[t + 1] : 0, i = r ? e[t + 2] : 0;
27
- o.push(P[n >> 2]), o.push(P[(n & 3) << 4 | l >> 4]), o.push(c ? P[(l & 15) << 2 | i >> 6] : "="), o.push(r ? P[i & 63] : "=");
81
+ function ae(e) {
82
+ const t = e.length, r = [];
83
+ for (let n = 0; n < t; n += 3) {
84
+ const o = e[n], i = n + 1 < t, s = n + 2 < t, a = i ? e[n + 1] : 0, c = s ? e[n + 2] : 0;
85
+ r.push(P[o >> 2]), r.push(P[(o & 3) << 4 | a >> 4]), r.push(i ? P[(a & 15) << 2 | c >> 6] : "="), r.push(s ? P[c & 63] : "=");
28
86
  }
29
- return o.join("");
87
+ return r.join("");
88
+ }
89
+ function k(e) {
90
+ return {
91
+ ref: e.ref,
92
+ fileName: e.fileName,
93
+ contentType: se(e.fileName),
94
+ data: ae(e.data)
95
+ };
96
+ }
97
+ function D(e) {
98
+ return {
99
+ ref: e.xmlId,
100
+ source: e.content,
101
+ sourceFormat: e.sourceFormat,
102
+ isRoot: e.isRoot
103
+ };
104
+ }
105
+ function ie(e) {
106
+ return {
107
+ title: e.title,
108
+ docinfo: e.docinfo,
109
+ documentKind: e.documentKind,
110
+ divisions: e.divisions.map(D),
111
+ assets: e.assets.map(k)
112
+ };
30
113
  }
31
- function $(e) {
114
+ function ce(e) {
32
115
  return {
33
116
  ref: e.ref,
34
117
  kind: "file",
@@ -36,82 +119,97 @@ function $(e) {
36
119
  short_description: e.fileName,
37
120
  file: {
38
121
  filename: e.fileName,
39
- content_type: j(e.fileName),
40
- data: R(e.data)
122
+ content_type: e.contentType,
123
+ data: e.data
41
124
  }
42
125
  };
43
126
  }
44
- function K(e) {
127
+ function le(e) {
45
128
  return {
46
129
  title: e.title,
47
130
  docinfo: e.docinfo,
48
131
  document_type: e.documentKind,
49
- divisions_attributes: e.divisions.map((s) => ({
50
- ref: s.xmlId,
51
- source: s.content,
52
- source_format: s.sourceFormat,
53
- is_root: s.isRoot
132
+ divisions_attributes: e.divisions.map((t) => ({
133
+ ref: t.ref,
134
+ source: t.source,
135
+ source_format: t.sourceFormat,
136
+ is_root: t.isRoot
54
137
  })),
55
- assets_attributes: e.assets.map($)
138
+ assets_attributes: e.assets.map(ce)
56
139
  };
57
140
  }
58
- const h = {
141
+ function Ie(e) {
142
+ return le(ie(e));
143
+ }
144
+ function _e(e, t) {
145
+ const { root: r, entries: n } = K(e, t), o = new Set(n.map((s) => s.xmlId)), i = e.divisions.filter(
146
+ (s) => !o.has(s.xmlId) && !(t.unwrapRoot && s.xmlId === r.xmlId)
147
+ );
148
+ return {
149
+ divisions: [...n, ...i].map(D),
150
+ assets: e.assets.map(k),
151
+ placeholders: n.map(
152
+ (s) => `<plus:${s.type} ref="${s.xmlId}"/>`
153
+ )
154
+ };
155
+ }
156
+ const E = {
59
157
  mainSourcePath: "source/main.ptx",
60
158
  publicationPath: "publication/publication.ptx",
61
159
  projectFilePath: "project.ptx",
62
160
  docinfoPath: "source/docinfo.ptx"
63
161
  };
64
- function S(e) {
65
- return E(e, "pretext") ? e : `<pretext>
162
+ function M(e) {
163
+ return y(e, "pretext") ? e : `<pretext>
66
164
  ${e}
67
165
  </pretext>`;
68
166
  }
69
- function L(e, s, o) {
70
- const t = /* @__PURE__ */ new Map(), n = [], c = `${s}-`;
71
- return e.forEach((r, l) => {
72
- const i = r.attributes["xml:id"];
73
- let u;
74
- if (i) {
75
- const m = k(i);
76
- u = m.startsWith(c) ? m : `${c}${m}`;
167
+ function O(e, t, r) {
168
+ const n = /* @__PURE__ */ new Map(), o = [], i = `${t}-`;
169
+ return e.forEach((s, a) => {
170
+ const c = s.attributes["xml:id"];
171
+ let l;
172
+ if (c) {
173
+ const m = q(c);
174
+ l = m.startsWith(i) ? m : `${i}${m}`;
77
175
  } else
78
- u = `${c}${y(l + 1, e.length)}`, o.push({
176
+ l = `${i}${V(a + 1, e.length)}`, r.push({
79
177
  action: "anomaly",
80
178
  severity: "info",
81
179
  kind: "structure",
82
180
  category: "missing_xml_id",
83
- macro: r.name,
181
+ macro: s.name,
84
182
  occurrences: 1,
85
- message: `<${r.name}> at position ${l + 1} has no xml:id; using \`${u}\` as filename.`
183
+ message: `<${s.name}> at position ${a + 1} has no xml:id; using \`${l}\` as filename.`
86
184
  });
87
- const p = t.get(u) ?? 0;
88
- t.set(u, p + 1);
89
- const a = p === 0 ? u : `${u}-${p + 1}`;
90
- n.push({
91
- span: r,
92
- slug: a,
93
- filename: `${a}.ptx`
185
+ const p = n.get(l) ?? 0;
186
+ n.set(l, p + 1);
187
+ const u = p === 0 ? l : `${l}-${p + 1}`;
188
+ o.push({
189
+ span: s,
190
+ slug: u,
191
+ filename: `${u}.ptx`
94
192
  });
95
- }), n;
193
+ }), o;
96
194
  }
97
- function D(e, s, o, t) {
98
- const n = T(e, "section");
99
- if (n.length === 0)
195
+ function ue(e, t, r, n) {
196
+ const o = C(e, "section");
197
+ if (o.length === 0)
100
198
  return e;
101
- const r = L(n, "sec", t).map((l) => {
102
- const i = `source/${s}/${l.filename}`;
103
- return o[i] = x(l.span.outer), {
104
- start: l.span.start,
105
- end: l.span.end,
106
- replacement: `<xi:include href="${s}/${l.filename}"/>`
199
+ const s = O(o, "sec", n).map((a) => {
200
+ const c = `source/${t}/${a.filename}`;
201
+ return r[c] = S(a.span.outer), {
202
+ start: a.span.start,
203
+ end: a.span.end,
204
+ replacement: `<xi:include href="${t}/${a.filename}"/>`
107
205
  };
108
206
  });
109
- return v(e, r);
207
+ return R(e, s);
110
208
  }
111
- function N(e, s, o) {
112
- const t = E(e, "book");
113
- if (!t)
114
- return o.push({
209
+ function pe(e, t, r) {
210
+ const n = y(e, "book");
211
+ if (!n)
212
+ return r.push({
115
213
  action: "anomaly",
116
214
  severity: "warning",
117
215
  kind: "structure",
@@ -119,200 +217,412 @@ function N(e, s, o) {
119
217
  macro: "book",
120
218
  occurrences: 1,
121
219
  message: "Document was treated as a book but no <book> element found; emitting as a single file."
122
- }), F(e, s);
123
- const n = {}, c = T(t.inner, "chapter");
124
- let r = t.inner;
125
- if (s.splitChapters && c.length > 0) {
126
- const p = L(c, "ch", o).map((a) => {
127
- let m = a.span.outer;
128
- if (s.splitSections) {
129
- const f = a.span.inner, d = D(
130
- f,
131
- a.slug,
132
- n,
133
- o
220
+ }), N(e, t);
221
+ const o = {}, i = C(n.inner, "chapter");
222
+ let s = n.inner;
223
+ if (t.splitChapters && i.length > 0) {
224
+ const p = O(i, "ch", r).map((u) => {
225
+ let m = u.span.outer;
226
+ if (t.splitSections) {
227
+ const d = u.span.inner, f = ue(
228
+ d,
229
+ u.slug,
230
+ o,
231
+ r
134
232
  );
135
- m = a.span.outer.slice(0, a.span.startTagEnd - a.span.start) + d + a.span.outer.slice(a.span.contentEnd - a.span.start);
233
+ m = u.span.outer.slice(0, u.span.startTagEnd - u.span.start) + f + u.span.outer.slice(u.span.contentEnd - u.span.start);
136
234
  }
137
- const g = `source/${a.filename}`;
138
- return n[g] = x(m), {
139
- start: a.span.start,
140
- end: a.span.end,
141
- replacement: `<xi:include href="${a.filename}"/>`
235
+ const h = `source/${u.filename}`;
236
+ return o[h] = S(m), {
237
+ start: u.span.start,
238
+ end: u.span.end,
239
+ replacement: `<xi:include href="${u.filename}"/>`
142
240
  };
143
241
  });
144
- r = v(t.inner, p);
242
+ s = R(n.inner, p);
145
243
  }
146
- const l = e.slice(t.start, t.startTagEnd) + r + e.slice(t.contentEnd, t.end), i = e.slice(0, t.start) + l + e.slice(t.end);
147
- return n[s.mainSourcePath] = x(
148
- C(S(i.trim()))
149
- ), n;
244
+ const a = e.slice(n.start, n.startTagEnd) + s + e.slice(n.contentEnd, n.end), c = e.slice(0, n.start) + a + e.slice(n.end);
245
+ return o[t.mainSourcePath] = S(
246
+ G(M(c.trim()))
247
+ ), o;
150
248
  }
151
- function F(e, s, o) {
152
- const t = {};
153
- return t[s.mainSourcePath] = x(
154
- S(e.trim())
155
- ), t;
249
+ function N(e, t, r) {
250
+ const n = {};
251
+ return n[t.mainSourcePath] = S(
252
+ M(e.trim())
253
+ ), n;
156
254
  }
157
- function z(e, s = {}) {
158
- const o = [], t = s.documentKind ?? w(e), n = {
159
- splitChapters: s.splitChapters ?? t === "book",
160
- splitSections: s.splitSections ?? !1,
161
- mainSourcePath: s.mainSourcePath ?? h.mainSourcePath,
162
- publicationPath: s.publicationPath ?? h.publicationPath,
163
- projectFilePath: s.projectFilePath ?? h.projectFilePath,
164
- docinfoPath: s.docinfoPath ?? h.docinfoPath
165
- }, c = t === "book" ? N(e, n, o) : F(e, n);
166
- return c[n.projectFilePath] = I({
167
- mainSource: n.mainSourcePath,
168
- publication: n.publicationPath
169
- }), c[n.publicationPath] = _(), { files: c, documentKind: t, warnings: o };
255
+ function $e(e, t = {}) {
256
+ const r = [], n = t.documentKind ?? W(e), o = {
257
+ splitChapters: t.splitChapters ?? n === "book",
258
+ splitSections: t.splitSections ?? !1,
259
+ mainSourcePath: t.mainSourcePath ?? E.mainSourcePath,
260
+ publicationPath: t.publicationPath ?? E.publicationPath,
261
+ projectFilePath: t.projectFilePath ?? E.projectFilePath,
262
+ docinfoPath: t.docinfoPath ?? E.docinfoPath
263
+ }, i = n === "book" ? pe(e, o, r) : N(e, o);
264
+ return i[o.projectFilePath] = H({
265
+ mainSource: o.mainSourcePath,
266
+ publication: o.publicationPath
267
+ }), i[o.publicationPath] = U(), { files: i, documentKind: n, warnings: r };
170
268
  }
171
- class O extends Error {
269
+ class de extends Error {
172
270
  constructor() {
173
271
  super("Import cancelled."), this.name = "ConversionCancelledError";
174
272
  }
175
273
  }
176
- let B = 0;
177
- function W(e, s, o = {}) {
178
- const t = `convert-${B++}`, n = e(), { assets: c = {}, ...r } = o;
179
- let l;
180
- const i = new Promise((p, a) => {
274
+ let me = 0;
275
+ function fe(e, t, r = {}) {
276
+ const n = `convert-${me++}`, o = e(), { assets: i = {}, ...s } = r;
277
+ let a;
278
+ const c = new Promise((p, u) => {
181
279
  let m = !1;
182
- const g = (f) => {
183
- m || (m = !0, n.terminate(), f());
280
+ const h = (d) => {
281
+ m || (m = !0, o.terminate(), d());
184
282
  };
185
- l = () => g(() => a(new O())), n.addEventListener("message", (f) => {
186
- const d = f.data;
187
- if (!(!d || d.requestId !== t)) {
188
- if (d.type === "result") {
189
- g(() => p(d.result));
283
+ a = () => h(() => u(new de())), o.addEventListener("message", (d) => {
284
+ const f = d.data;
285
+ if (!(!f || f.requestId !== n)) {
286
+ if (f.type === "result") {
287
+ h(() => p(f.result));
190
288
  return;
191
289
  }
192
- g(() => a(new Error(d.message)));
290
+ h(() => u(new Error(f.message)));
193
291
  }
194
- }), n.addEventListener("error", (f) => {
195
- const d = typeof f == "object" && f && "message" in f ? String(f.message) : "The import worker failed to start.";
196
- g(() => a(new Error(d)));
292
+ }), o.addEventListener("error", (d) => {
293
+ const f = typeof d == "object" && d && "message" in d ? String(d.message) : "The import worker failed to start.";
294
+ h(() => u(new Error(f)));
197
295
  });
198
- }), u = [
296
+ }), l = [
199
297
  ...new Set(
200
- Object.values(c).map((p) => p.buffer).filter(
298
+ Object.values(i).map((p) => p.buffer).filter(
201
299
  (p) => p instanceof ArrayBuffer
202
300
  )
203
301
  )
204
302
  ];
205
- return n.postMessage(
206
- { type: "convert", requestId: t, files: s, assets: c, options: r },
207
- u
208
- ), { result: i, cancel: () => l?.() };
303
+ return o.postMessage(
304
+ { type: "convert", requestId: n, files: t, assets: i, options: s },
305
+ l
306
+ ), { result: c, cancel: () => a?.() };
209
307
  }
210
- function U(e) {
308
+ function Ae(e) {
211
309
  const {
212
- createWorker: s,
213
- id: o = "worker",
214
- label: t = "Built-in converter",
215
- description: n = "Create a new project starting with LaTeX, Markdown, or PreTeXt files.",
216
- acceptExtensions: c
310
+ createWorker: t,
311
+ id: r = "worker",
312
+ label: n = "Built-in converter",
313
+ description: o = "Create a new project starting with LaTeX, Markdown, or PreTeXt files.",
314
+ acceptExtensions: i
217
315
  } = e;
218
- let r = null;
219
- const l = async (i, u) => {
220
- r?.cancel(), r = W(s, i, u);
316
+ let s = null;
317
+ const a = async (c, l) => {
318
+ s?.cancel(), s = fe(t, c, l);
221
319
  try {
222
- return await r.result;
320
+ return await s.result;
223
321
  } finally {
224
- r = null;
322
+ s = null;
225
323
  }
226
324
  };
227
325
  return {
228
- id: o,
229
- label: t,
230
- description: n,
231
- acceptExtensions: c,
232
- prepare: async (i) => {
233
- const { files: u, assets: p } = await b(i);
326
+ id: r,
327
+ label: n,
328
+ description: o,
329
+ acceptExtensions: i,
330
+ prepare: async (c) => {
331
+ const { files: l, assets: p } = await $(c);
234
332
  return {
235
- fileName: i.name,
236
- files: u,
333
+ fileName: c.name,
334
+ files: l,
237
335
  assets: p,
238
- analysis: A(u)
336
+ analysis: Z(l)
239
337
  };
240
338
  },
241
- convertPrepared: (i, u) => l(i.files, { ...u, assets: i.assets }),
242
- convertFile: async (i, u) => {
243
- const { files: p, assets: a } = await b(i);
244
- return l(p, { ...u, assets: a });
339
+ convertPrepared: (c, l) => a(c.files, { ...l, assets: c.assets }),
340
+ convertFile: async (c, l) => {
341
+ const { files: p, assets: u } = await $(c);
342
+ return a(p, { ...l, assets: u });
245
343
  },
246
344
  cancel: () => {
247
- r?.cancel(), r = null;
345
+ s?.cancel(), s = null;
248
346
  }
249
347
  };
250
348
  }
349
+ const he = {
350
+ ".docx": "docx",
351
+ ".odt": "odt",
352
+ ".epub": "epub",
353
+ ".tex": "latex",
354
+ ".ltx": "latex",
355
+ ".md": "markdown",
356
+ ".markdown": "markdown",
357
+ ".html": "html",
358
+ ".htm": "html",
359
+ ".rst": "rst",
360
+ ".org": "org",
361
+ ".ipynb": "ipynb",
362
+ ".typ": "typst",
363
+ ".xml": "docbook",
364
+ ".textile": "textile"
365
+ }, Fe = /* @__PURE__ */ new Set(["docx", "odt", "epub"]), j = [
366
+ ".docx",
367
+ ".odt",
368
+ ".epub",
369
+ ".html",
370
+ ".htm",
371
+ ".rst",
372
+ ".org",
373
+ ".ipynb",
374
+ ".typ",
375
+ ".textile",
376
+ ".tex",
377
+ ".ltx",
378
+ ".md",
379
+ ".markdown"
380
+ ];
381
+ function X(e) {
382
+ const t = e.lastIndexOf(".");
383
+ return t <= 0 ? "" : e.slice(t).toLowerCase();
384
+ }
385
+ function ge(e) {
386
+ return he[X(e)];
387
+ }
388
+ function we(e) {
389
+ const {
390
+ convertToPretext: t,
391
+ id: r = "pandoc",
392
+ label: n = "Pandoc",
393
+ description: o = "Convert Word, OpenOffice, EPUB, HTML, reStructuredText, and more.",
394
+ acceptExtensions: i = j
395
+ } = e;
396
+ return {
397
+ id: r,
398
+ label: n,
399
+ description: o,
400
+ acceptExtensions: i,
401
+ convertFile: async (s, a) => {
402
+ const c = await t(s, a);
403
+ if (!c.trim())
404
+ throw new Error(
405
+ `Pandoc produced no output for ${s.name}. The file may be empty or in an unexpected format.`
406
+ );
407
+ const l = Y(
408
+ { "source.ptx": c },
409
+ {
410
+ documentKind: a.documentKind,
411
+ splitChapters: a.splitChapters,
412
+ splitSections: a.splitSections,
413
+ splitLevel: a.splitLevel
414
+ }
415
+ );
416
+ return "pretextError" in l || (l.sourceName = s.name, l.sourcePath = s.name), l;
417
+ }
418
+ };
419
+ }
420
+ const xe = 3e4;
421
+ function Te(e) {
422
+ const t = /<pre>([\s\S]*?)<\/pre>/i.exec(e);
423
+ return (t ? t[1] : e).replace(/<[^>]+>/g, "").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&amp;/g, "&").trim();
424
+ }
425
+ function Pe(e, t, r) {
426
+ const n = Ee(Te(t)), o = n ? ` (${L(n)})` : "";
427
+ switch (e) {
428
+ case 400:
429
+ return `The pandoc server will not convert ${r}${o}.${x(r)}`;
430
+ case 401:
431
+ case 403:
432
+ return `The pandoc server rejected this site's credentials, so ${r} was never converted. Nothing is wrong with your file — this is a configuration problem with the service, and worth reporting.${x(r)}`;
433
+ case 404:
434
+ return `The pandoc conversion service could not be found at the address this site is configured to use. Nothing is wrong with your file — this is a configuration problem worth reporting.${x(r)}`;
435
+ case 413:
436
+ return `${r} is too large for the pandoc server to accept. Splitting it into chapters and importing them one at a time should get through.`;
437
+ case 422:
438
+ return `Pandoc could not read ${r}${o}. The file may be damaged, or may use features pandoc does not support.`;
439
+ case 503:
440
+ return `The pandoc server is running without its PreTeXt writer, so it cannot convert anything right now. Nothing is wrong with your file — this is a problem with the service, and worth reporting.${x(r)}`;
441
+ case 504:
442
+ return `Converting ${r} took longer than the pandoc server allows. Splitting it into chapters and importing them one at a time usually works.`;
443
+ default: {
444
+ const i = n ? `(HTTP ${e}: ${L(n)})` : `(HTTP ${e})`;
445
+ return `The pandoc server could not convert ${r} ${i}.${x(r)}`;
446
+ }
447
+ }
448
+ }
449
+ const F = 300;
450
+ function Ee(e) {
451
+ return e.replace(/\s+/g, " ").trim();
452
+ }
453
+ function L(e) {
454
+ return e.length <= F ? e : `${e.slice(0, F).trimEnd()}…`;
455
+ }
456
+ const Se = /* @__PURE__ */ new Set([
457
+ ".tex",
458
+ ".ltx",
459
+ ".md",
460
+ ".markdown"
461
+ ]);
462
+ function x(e) {
463
+ return Se.has(X(e)) ? " You can import this file with the built-in converter instead." : "";
464
+ }
465
+ function Le(e) {
466
+ const {
467
+ url: t,
468
+ token: r,
469
+ headers: n,
470
+ credentials: o,
471
+ standalone: i = !0,
472
+ from: s,
473
+ timeoutMs: a = xe,
474
+ fetchImpl: c,
475
+ id: l = "pandoc-remote",
476
+ label: p = "Pandoc (server)",
477
+ description: u = "Convert Word, OpenOffice, EPUB, HTML, reStructuredText, and more — no local install needed.",
478
+ acceptExtensions: m = j
479
+ } = e;
480
+ return we({
481
+ convertToPretext: async (d, f) => {
482
+ const b = c ?? globalThis.fetch;
483
+ if (typeof b != "function")
484
+ throw new Error(
485
+ "This environment has no fetch; pass fetchImpl to createRemotePandocEngine."
486
+ );
487
+ const v = s ?? ge(d.name);
488
+ if (!v)
489
+ throw new Error(
490
+ `Pandoc cannot determine a format for ${d.name}. Supported extensions: ${m.join(", ")}.`
491
+ );
492
+ const g = new FormData();
493
+ r && g.set("token", r), g.set("file", d, d.name), g.set("from", v), i && g.set("standalone", "yes");
494
+ const I = new AbortController(), z = setTimeout(() => I.abort(), a);
495
+ let T;
496
+ try {
497
+ T = await b(t, {
498
+ method: "POST",
499
+ body: g,
500
+ headers: n,
501
+ credentials: o,
502
+ signal: I.signal
503
+ });
504
+ } catch (w) {
505
+ if (w instanceof Error && w.name === "AbortError")
506
+ throw new Error(
507
+ `Pandoc conversion of ${d.name} timed out after ${Math.round(a / 1e3)}s.`
508
+ );
509
+ const B = w instanceof Error ? w.message : String(w);
510
+ throw new Error(`Could not reach the pandoc server: ${B}`);
511
+ } finally {
512
+ clearTimeout(z);
513
+ }
514
+ const _ = await T.text();
515
+ if (!T.ok)
516
+ throw new Error(
517
+ Pe(T.status, _, d.name)
518
+ );
519
+ return _;
520
+ },
521
+ id: l,
522
+ label: p,
523
+ description: u,
524
+ acceptExtensions: m
525
+ });
526
+ }
251
527
  export {
252
- q as CLEAN_RULES,
253
- O as ConversionCancelledError,
254
- V as LATEX_DIVISION_COMMANDS,
255
- Y as LATEX_FORMAT_MARKERS,
256
- J as MARKDOWN_FORMAT_MARKERS,
257
- Q as MAX_SUGGESTED_SPLIT_LEVEL,
258
- Z as PRETEXT_DIVISION_TAGS,
259
- ee as PRETEXT_ROOT_TAGS,
260
- A as analyzeImportSources,
261
- te as applyLatexFixes,
262
- Ke as assetsForImportMode,
263
- se as attachLatexRoots,
264
- ne as attachMarkdownRoots,
265
- ae as buildDivisionPool,
266
- oe as buildNativeDivisionPool,
267
- z as buildPretextProjectFiles,
268
- re as carryOverProjectFiles,
269
- ie as cleanLatex,
270
- ce as cleanLatexInChunks,
271
- le as cleanLatexText,
272
- ue as convertLatexToPretext,
273
- pe as convertMarkdownToPretext,
274
- fe as convertSourceToPretext,
275
- U as createWorkerEngine,
276
- me as defaultAttachLevel,
277
- de as defaultManifestTarget,
278
- w as detectDocumentKind,
279
- ge as detectSourceFormat,
280
- ze as diffHunks,
281
- Ue as diffLines,
282
- Ge as diffStats,
283
- Pe as divisionChildRefs,
284
- he as divisionsAtLevel,
285
- xe as expandPretextIncludes,
286
- b as extractUpload,
287
- He as fileChangesForImport,
288
- be as filePrefixForDivision,
289
- qe as filesForImportMode,
290
- Ee as findLatexFixes,
291
- Te as findLatexHeaders,
292
- ve as findProjectManifest,
293
- Se as fixesToWarnings,
294
- Ve as formatWarningLine,
295
- Le as getConversionErrorMessage,
296
- Fe as getLatexCleanDiagnostics,
297
- we as handleImportUploadFile,
298
- Ie as importProjectFromFiles,
299
- _e as isDivisionTag,
300
- Ce as latexDivisionHierarchy,
301
- ke as latexFixesToCodeActions,
302
- ye as mergeChunksAtLevel,
303
- Ae as normalizePretextSource,
304
- Me as parseLatexDivisions,
305
- je as parseProjectManifest,
306
- Ye as projectForImportMode,
307
- Re as relayoutImport,
308
- I as renderProjectPtx,
309
- $e as renderProjectPtxFromManifest,
310
- _ as renderPublicationPtx,
311
- De as resolveImportSplitLevel,
312
- Ne as resolveSplitLevel,
313
- W as runConversionInWorker,
314
- Oe as sanitizeRef,
315
- Be as serializeProjectToFiles,
316
- K as serializeProjectToPlusPayload,
317
- We as suggestSplitLevel
528
+ Re as CLEAN_RULES,
529
+ de as ConversionCancelledError,
530
+ Kt as DEFAULT_IMPORT_MODE,
531
+ xe as DEFAULT_REMOTE_PANDOC_TIMEOUT_MS,
532
+ ke as DIVISION_LADDER,
533
+ De as LADDER_OVERFLOW_TAG,
534
+ Me as LATEX_DIVISION_COMMANDS,
535
+ Oe as LATEX_FORMAT_MARKERS,
536
+ Ne as MARKDOWN_FORMAT_MARKERS,
537
+ je as MAX_SUGGESTED_SPLIT_LEVEL,
538
+ j as PANDOC_ACCEPT_EXTENSIONS,
539
+ Fe as PANDOC_BINARY_FORMATS,
540
+ he as PANDOC_EXTENSION_FORMATS,
541
+ Xe as PRETEXT_DIVISION_TAGS,
542
+ ze as PRETEXT_ROOT_TAGS,
543
+ Be as PROJECT_DESTINATION,
544
+ Z as analyzeImportSources,
545
+ Ke as applyLatexFixes,
546
+ Wt as assetsForImportMode,
547
+ We as attachLatexRoots,
548
+ He as attachMarkdownRoots,
549
+ Ue as buildDivisionPool,
550
+ Ge as buildNativeDivisionPool,
551
+ $e as buildPretextProjectFiles,
552
+ qe as carryOverProjectFiles,
553
+ Ve as cleanLatex,
554
+ Ze as cleanLatexInChunks,
555
+ Ye as cleanLatexText,
556
+ Je as convertLatexToPretext,
557
+ Qe as convertMarkdownToPretext,
558
+ et as convertSourceToPretext,
559
+ we as createPandocEngine,
560
+ Le as createRemotePandocEngine,
561
+ Ae as createWorkerEngine,
562
+ tt as dedupeXmlIds,
563
+ nt as defaultAttachLevel,
564
+ rt as defaultManifestTarget,
565
+ Pe as describeRemotePandocFailure,
566
+ W as detectDocumentKind,
567
+ ve as detectSnippetFormat,
568
+ ot as detectSourceFormat,
569
+ Ht as diffHunks,
570
+ Ut as diffLines,
571
+ Gt as diffStats,
572
+ st as divisionChildRefs,
573
+ at as divisionsAtLevel,
574
+ it as expandPretextIncludes,
575
+ Te as extractPandocErrorDetail,
576
+ $ as extractUpload,
577
+ qt as fileChangesForImport,
578
+ X as fileExtension,
579
+ ct as filePrefixForDivision,
580
+ Vt as filesForImportMode,
581
+ lt as findLatexFixes,
582
+ ut as findLatexHeaders,
583
+ pt as findProjectManifest,
584
+ dt as fixesToWarnings,
585
+ Zt as formatWarningLine,
586
+ mt as getConversionErrorMessage,
587
+ ft as getLatexCleanDiagnostics,
588
+ ht as handleImportUploadFile,
589
+ Yt as hasNativeImportMode,
590
+ Y as importProjectFromFiles,
591
+ gt as isDivisionTag,
592
+ wt as ladderDepth,
593
+ xt as latexDivisionHierarchy,
594
+ Tt as latexFixesToCodeActions,
595
+ Pt as mergeChunksAtLevel,
596
+ Et as normalizePretextSource,
597
+ St as outlineDivisions,
598
+ ge as pandocFormatForFileName,
599
+ bt as parseLatexDivisions,
600
+ vt as parseProjectManifest,
601
+ It as prepareInsertSource,
602
+ Jt as projectForImportMode,
603
+ _t as pruneDivisions,
604
+ $t as rebuildImport,
605
+ le as recordsToPlusPayload,
606
+ At as relayoutImport,
607
+ H as renderProjectPtx,
608
+ Ft as renderProjectPtxFromManifest,
609
+ U as renderPublicationPtx,
610
+ Lt as reselectImport,
611
+ Qt as resolveImportMode,
612
+ yt as resolveImportSplitLevel,
613
+ Ct as resolveSplitLevel,
614
+ Rt as retargetFragment,
615
+ kt as retargetFragmentToDepth,
616
+ Dt as retargetImport,
617
+ fe as runConversionInWorker,
618
+ Mt as sanitizeRef,
619
+ re as scoreSnippetFormats,
620
+ Ot as serializeForDestination,
621
+ Nt as serializeInsertFiles,
622
+ _e as serializeInsertToRecords,
623
+ jt as serializeProjectToFiles,
624
+ Ie as serializeProjectToPlusPayload,
625
+ ie as serializeProjectToRecords,
626
+ Xt as shiftLadderTag,
627
+ zt as suggestSplitLevel
318
628
  };