@pretextbook/pretext-html 0.2.0 → 0.3.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.
@@ -1,67 +1,67 @@
1
1
  import { fromXml as x } from "xast-util-from-xml";
2
- import { toXml as y } from "xast-util-to-xml";
3
- const T = "/";
4
- function ie(e) {
2
+ import { toXml as $ } from "xast-util-to-xml";
3
+ const P = "/";
4
+ function me(e) {
5
5
  if (!e)
6
6
  return ".";
7
7
  const t = e.startsWith("/"), n = e.length > 1 && e.endsWith("/"), r = [];
8
- for (const o of e.split("/"))
9
- if (!(!o || o === ".")) {
10
- if (o === "..") {
11
- const a = r[r.length - 1];
12
- r.length > 0 && a !== ".." ? r.pop() : t || r.push("..");
8
+ for (const i of e.split("/"))
9
+ if (!(!i || i === ".")) {
10
+ if (i === "..") {
11
+ const s = r[r.length - 1];
12
+ r.length > 0 && s !== ".." ? r.pop() : t || r.push("..");
13
13
  continue;
14
14
  }
15
- r.push(o);
15
+ r.push(i);
16
16
  }
17
- let i = r.join("/");
18
- return t && (i = `/${i}`), i ? (n && !i.endsWith("/") && (i += "/"), i) : t ? "/" : ".";
17
+ let o = r.join("/");
18
+ return t && (o = `/${o}`), o ? (n && !o.endsWith("/") && (o += "/"), o) : t ? "/" : ".";
19
19
  }
20
20
  function h(...e) {
21
21
  let t = "";
22
22
  for (let r = e.length - 1; r >= 0; r--) {
23
- const i = e[r];
24
- if (i && (t = t ? `${i}/${t}` : i, i.startsWith("/")))
23
+ const o = e[r];
24
+ if (o && (t = t ? `${o}/${t}` : o, o.startsWith("/")))
25
25
  break;
26
26
  }
27
27
  t.startsWith("/") || (t = `/${t}`);
28
- const n = ie(t);
28
+ const n = me(t);
29
29
  return n.length > 1 && n.endsWith("/") ? n.slice(0, -1) : n;
30
30
  }
31
- function E(e) {
31
+ function b(e) {
32
32
  const t = e.replace(/\/+$/, "");
33
33
  if (!t)
34
34
  return e.startsWith("/") ? "/" : ".";
35
35
  const n = t.lastIndexOf("/");
36
36
  return n < 0 ? "." : n === 0 ? "/" : t.slice(0, n);
37
37
  }
38
- function F(e, t) {
38
+ function S(e, t) {
39
39
  const n = h(e).split("/").filter(Boolean), r = h(t).split("/").filter(Boolean);
40
- let i = 0;
41
- for (; i < n.length && i < r.length && n[i] === r[i]; )
42
- i += 1;
43
- return [...new Array(n.length - i).fill(".."), ...r.slice(i)].join("/");
40
+ let o = 0;
41
+ for (; o < n.length && o < r.length && n[o] === r[o]; )
42
+ o += 1;
43
+ return [...new Array(n.length - o).fill(".."), ...r.slice(o)].join("/");
44
44
  }
45
- const oe = "https://cdn.jsdelivr.net/npm/@pretextbook/pretext-html@0.2.0/assets";
46
- let O;
47
- function Ae(e) {
48
- O = e.replace(/\/+$/, "");
45
+ const pe = "https://cdn.jsdelivr.net/npm/@pretextbook/pretext-html@0.3.0/assets";
46
+ let V;
47
+ function Ge(e) {
48
+ V = e.replace(/\/+$/, "");
49
49
  }
50
- function v() {
51
- return O ?? oe;
50
+ function T() {
51
+ return V ?? pe;
52
52
  }
53
- function H(e, t) {
53
+ function Q(e, t) {
54
54
  return `${e.replace(/\/+$/, "")}/${t.replace(/^\/+/, "")}`;
55
55
  }
56
- const j = /* @__PURE__ */ new Map();
57
- function se(e) {
56
+ const C = /* @__PURE__ */ new Map();
57
+ function we(e) {
58
58
  return `${e.replace(/\/+$/, "")}-bundle.json`;
59
59
  }
60
- function ae(e) {
61
- let t = j.get(e);
60
+ function ge(e) {
61
+ let t = C.get(e);
62
62
  return t || (t = (async () => {
63
63
  try {
64
- const n = await fetch(se(e));
64
+ const n = await fetch(we(e));
65
65
  if (!n.ok)
66
66
  return /* @__PURE__ */ new Map();
67
67
  const r = await n.json();
@@ -69,16 +69,16 @@ function ae(e) {
69
69
  } catch {
70
70
  return /* @__PURE__ */ new Map();
71
71
  }
72
- })(), j.set(e, t)), t;
72
+ })(), C.set(e, t)), t;
73
73
  }
74
- function ce(e) {
75
- return e === H(v(), "xsl");
74
+ function ye(e) {
75
+ return e === Q(T(), "xsl");
76
76
  }
77
- async function N(e, t) {
78
- if (ce(e)) {
79
- const o = (await ae(e)).get(t.replace(/^\/+/, ""));
80
- if (o !== void 0)
81
- return new TextEncoder().encode(o);
77
+ async function Z(e, t) {
78
+ if (ye(e)) {
79
+ const i = (await ge(e)).get(t.replace(/^\/+/, ""));
80
+ if (i !== void 0)
81
+ return new TextEncoder().encode(i);
82
82
  }
83
83
  const n = `${e.replace(/\/+$/, "")}/`;
84
84
  let r;
@@ -94,13 +94,13 @@ async function N(e, t) {
94
94
  return;
95
95
  }
96
96
  try {
97
- const i = await fetch(r.href);
98
- return i.ok ? new Uint8Array(await i.arrayBuffer()) : void 0;
97
+ const o = await fetch(r.href);
98
+ return o.ok ? new Uint8Array(await o.arrayBuffer()) : void 0;
99
99
  } catch {
100
100
  return;
101
101
  }
102
102
  }
103
- async function S(e) {
103
+ async function L(e) {
104
104
  if (/^[a-z][a-z0-9+.-]*:\/\//i.test(e))
105
105
  try {
106
106
  const t = await fetch(e);
@@ -109,47 +109,47 @@ async function S(e) {
109
109
  return;
110
110
  }
111
111
  }
112
- const D = /* @__PURE__ */ new Map(), J = /* @__PURE__ */ new Map();
113
- let I, le = 0, Y = N;
114
- function Re(e) {
115
- Y = e;
112
+ const F = /* @__PURE__ */ new Map(), ee = /* @__PURE__ */ new Map();
113
+ let _, be = 0, te = Z;
114
+ function Ke(e) {
115
+ te = e;
116
116
  }
117
- function ue(e, t) {
118
- const n = typeof e == "string" ? e : e instanceof URL ? e.href : e.url, r = J.get(n);
117
+ function ve(e, t) {
118
+ const n = typeof e == "string" ? e : e instanceof URL ? e.href : e.url, r = ee.get(n);
119
119
  if (r !== void 0)
120
120
  return Promise.resolve(new Response(r, { status: 200 }));
121
- let i;
121
+ let o;
122
122
  try {
123
- i = new URL(n).host;
123
+ o = new URL(n).host;
124
124
  } catch {
125
- i = "";
125
+ o = "";
126
126
  }
127
- const o = D.get(i);
128
- return o !== void 0 ? fe(o, n) : I(e, t);
127
+ const i = F.get(o);
128
+ return i !== void 0 ? Ee(i, n) : _(e, t);
129
129
  }
130
- const de = '<?xml version="1.0"?><pretext-preview-missing-file/>';
131
- async function fe(e, t) {
132
- const n = decodeURIComponent(new URL(t).pathname), r = await Y(e, n);
130
+ const xe = '<?xml version="1.0"?><pretext-preview-missing-file/>';
131
+ async function Ee(e, t) {
132
+ const n = decodeURIComponent(new URL(t).pathname), r = await te(e, n);
133
133
  if (r === void 0)
134
- return new Response(de, { status: 200 });
135
- const i = typeof r == "string" ? r : new Uint8Array(r);
136
- return new Response(i, { status: 200 });
134
+ return new Response(xe, { status: 200 });
135
+ const o = typeof r == "string" ? r : new Uint8Array(r);
136
+ return new Response(o, { status: 200 });
137
137
  }
138
- function z() {
139
- I || (I = globalThis.fetch, globalThis.fetch = ue);
138
+ function ne() {
139
+ _ || (_ = globalThis.fetch, globalThis.fetch = ve);
140
140
  }
141
- function G(e) {
142
- z();
143
- const t = `mnt${++le}.ptx.invalid`;
144
- return D.set(t, e), `http://${t}`;
141
+ function re(e) {
142
+ ne();
143
+ const t = `mnt${++be}.ptx.invalid`;
144
+ return F.set(t, e), `http://${t}`;
145
145
  }
146
- function he(e) {
147
- D.delete(new URL(e).host);
146
+ function $e(e) {
147
+ F.delete(new URL(e).host);
148
148
  }
149
- function pe(e, t) {
150
- z(), J.set(e, t);
149
+ function Te(e, t) {
150
+ ne(), ee.set(e, t);
151
151
  }
152
- const me = `<?xml version="1.0" encoding="UTF-8"?>
152
+ const oe = `<?xml version="1.0" encoding="UTF-8"?>
153
153
  <publication>
154
154
  <source>
155
155
  <directories external="../assets" generated="../generated-assets" />
@@ -159,164 +159,180 @@ const me = `<?xml version="1.0" encoding="UTF-8"?>
159
159
  </html>
160
160
  </publication>
161
161
  `;
162
- function K(e, t) {
162
+ function y(e, t) {
163
163
  for (const n of e.children)
164
164
  if (n.type === "element" && n.name === t)
165
165
  return n;
166
166
  }
167
- function A(e, t) {
168
- let n = K(e, t);
167
+ function U(e, t) {
168
+ let n = y(e, t);
169
169
  return n || (n = { type: "element", name: t, attributes: {}, children: [] }, e.children.push(n)), n;
170
170
  }
171
- function R(e) {
171
+ function H(e) {
172
172
  if (!e)
173
- return me;
174
- const t = x(e), n = K(t, "publication");
173
+ return oe;
174
+ const t = x(e), n = y(t, "publication");
175
175
  if (!n)
176
176
  throw new Error(
177
177
  "Invalid publication file: no root <publication> element found"
178
178
  );
179
- const r = A(n, "html"), i = A(r, "platform");
180
- return i.attributes = { ...i.attributes, portable: "yes" }, y(t);
181
- }
182
- const q = "http://www.w3.org/2001/XInclude", B = 64;
183
- async function we(e, t, n) {
184
- return e.includes("include") ? (await V(e, t, n)).content : e;
185
- }
186
- async function V(e, t, n) {
187
- const r = x(e), i = {
179
+ const r = U(n, "html"), o = U(r, "platform");
180
+ return o.attributes = { ...o.attributes, portable: "yes" }, $(t);
181
+ }
182
+ function O(e) {
183
+ if (!(e === void 0 || e === "" || e.startsWith("/")))
184
+ return e.endsWith("/") ? e : `${e}/`;
185
+ }
186
+ function ke(e) {
187
+ const t = x(e ?? oe), n = y(t, "publication"), r = n && y(n, "source"), o = r && y(r, "directories"), i = O(
188
+ o?.attributes?.external ?? void 0
189
+ ), s = O(
190
+ o?.attributes?.generated ?? void 0
191
+ );
192
+ return {
193
+ external: i,
194
+ generated: s,
195
+ managed: i !== void 0 && s !== void 0
196
+ };
197
+ }
198
+ const ie = "http://www.w3.org/2001/XInclude", N = 64;
199
+ async function Me(e, t, n) {
200
+ return e.includes("include") ? (await se(e, t, n)).content : e;
201
+ }
202
+ async function se(e, t, n) {
203
+ const r = x(e), o = {
188
204
  projectDir: h(n),
189
205
  stack: [h(t)]
190
- }, o = await g(
206
+ }, i = await v(
191
207
  r,
192
- E(h(t)),
208
+ b(h(t)),
193
209
  { "": "" },
194
- i
210
+ o
195
211
  );
196
- return { tree: r, content: o ? y(r) : e };
212
+ return { tree: r, content: i ? $(r) : e };
197
213
  }
198
- function ge(e) {
214
+ function De(e) {
199
215
  return e.data?.ptxIncludedFile;
200
216
  }
201
- function ye(e, t) {
217
+ function Se(e, t) {
202
218
  const n = e.data ?? (e.data = {});
203
219
  n.ptxIncludedFile = t;
204
220
  }
205
- function b(e, t) {
221
+ function k(e, t) {
206
222
  let n = e;
207
- for (const [r, i] of Object.entries(t.attributes))
208
- (r === "xmlns" || r.startsWith("xmlns:")) && (n === e && (n = { ...e }), n[r === "xmlns" ? "" : r.slice(6)] = i ?? "");
223
+ for (const [r, o] of Object.entries(t.attributes))
224
+ (r === "xmlns" || r.startsWith("xmlns:")) && (n === e && (n = { ...e }), n[r === "xmlns" ? "" : r.slice(6)] = o ?? "");
209
225
  return n;
210
226
  }
211
- function Z(e, t) {
227
+ function ae(e, t) {
212
228
  const n = e.name.indexOf(":"), r = n === -1 ? "" : e.name.slice(0, n);
213
- return (n === -1 ? e.name : e.name.slice(n + 1)) === "include" && t[r] === q;
229
+ return (n === -1 ? e.name : e.name.slice(n + 1)) === "include" && t[r] === ie;
214
230
  }
215
- async function g(e, t, n, r) {
216
- let i = !1;
217
- for (let o = 0; o < e.children.length; o++) {
218
- const a = e.children[o];
219
- if (a.type !== "element")
231
+ async function v(e, t, n, r) {
232
+ let o = !1;
233
+ for (let i = 0; i < e.children.length; i++) {
234
+ const s = e.children[i];
235
+ if (s.type !== "element")
220
236
  continue;
221
- const s = b(n, a);
222
- if (Z(a, s)) {
223
- const d = await Q(
224
- a,
225
- t,
237
+ const a = k(n, s);
238
+ if (ae(s, a)) {
239
+ const d = await ce(
226
240
  s,
241
+ t,
242
+ a,
227
243
  r
228
244
  );
229
- e.children.splice(o, 1, ...d), o += d.length - 1, i = !0;
230
- } else await g(a, t, s, r) && (i = !0);
245
+ e.children.splice(i, 1, ...d), i += d.length - 1, o = !0;
246
+ } else await v(s, t, a, r) && (o = !0);
231
247
  }
232
- return i;
248
+ return o;
233
249
  }
234
- async function Q(e, t, n, r) {
235
- const i = r.stack[r.stack.length - 1];
250
+ async function ce(e, t, n, r) {
251
+ const o = r.stack[r.stack.length - 1];
236
252
  if (e.attributes.xpointer)
237
253
  throw new Error(
238
- `xi:include with xpointer is not supported by the JS preview build (in ${i})`
254
+ `xi:include with xpointer is not supported by the JS preview build (in ${o})`
239
255
  );
240
- const o = e.attributes.href;
241
- if (!o)
242
- throw new Error(`xi:include without href (in ${i})`);
243
- let a = o;
256
+ const i = e.attributes.href;
257
+ if (!i)
258
+ throw new Error(`xi:include without href (in ${o})`);
259
+ let s = i;
244
260
  try {
245
- a = decodeURIComponent(o);
261
+ s = decodeURIComponent(i);
246
262
  } catch {
247
263
  }
248
- const s = h(t, a);
249
- if (s !== r.projectDir && !s.startsWith(r.projectDir + T))
264
+ const a = h(t, s);
265
+ if (a !== r.projectDir && !a.startsWith(r.projectDir + P))
250
266
  throw new Error(
251
- `xi:include escapes the project directory: ${o} (in ${i})`
267
+ `xi:include escapes the project directory: ${i} (in ${o})`
252
268
  );
253
- if (r.stack.includes(s))
269
+ if (r.stack.includes(a))
254
270
  throw new Error(
255
- `Circular xi:include: ${[...r.stack, s].join(" -> ")}`
271
+ `Circular xi:include: ${[...r.stack, a].join(" -> ")}`
256
272
  );
257
- if (r.stack.length >= B)
258
- throw new Error(`xi:include nesting exceeds ${B} levels`);
259
- const d = await S(s);
273
+ if (r.stack.length >= N)
274
+ throw new Error(`xi:include nesting exceeds ${N} levels`);
275
+ const d = await L(a);
260
276
  if (d === void 0) {
261
- const l = be(e, n);
262
- if (l)
263
- return await g(l, t, n, r), l.children;
264
- throw new Error(`xi:include target not found: ${o} (in ${i})`);
277
+ const f = Ae(e, n);
278
+ if (f)
279
+ return await v(f, t, n, r), f.children;
280
+ throw new Error(`xi:include target not found: ${i} (in ${o})`);
265
281
  }
266
282
  if (e.attributes.parse === "text")
267
283
  return [{ type: "text", value: d }];
268
284
  let c;
269
285
  try {
270
286
  c = x(d);
271
- } catch (l) {
287
+ } catch (f) {
272
288
  throw new Error(
273
- `Could not parse xi:include target ${s} as XML: ${l instanceof Error ? l.message : l}. (Included files must be well-formed, with a single root element.)`
289
+ `Could not parse xi:include target ${a} as XML: ${f instanceof Error ? f.message : f}. (Included files must be well-formed, with a single root element.)`
274
290
  );
275
291
  }
276
- r.stack.push(s);
292
+ r.stack.push(a);
277
293
  try {
278
- await g(c, E(s), { "": "" }, r);
294
+ await v(c, b(a), { "": "" }, r);
279
295
  } finally {
280
296
  r.stack.pop();
281
297
  }
282
- const f = c.children.filter(
283
- (l) => l.type === "element"
298
+ const u = c.children.filter(
299
+ (f) => f.type === "element"
284
300
  );
285
- for (const l of f)
286
- ye(l, s);
287
- return f;
301
+ for (const f of u)
302
+ Se(f, a);
303
+ return u;
288
304
  }
289
- function X(e) {
305
+ function z(e) {
290
306
  return e.name === "docinfo";
291
307
  }
292
- async function ve(e, t, n) {
308
+ async function Ie(e, t, n) {
293
309
  try {
294
- const i = x(e).children.find(
310
+ const o = x(e).children.find(
295
311
  (c) => c.type === "element"
296
312
  );
297
- if (!i)
313
+ if (!o)
298
314
  return;
299
- const o = h(t), a = E(o), s = {
315
+ const i = h(t), s = b(i), a = {
300
316
  projectDir: h(n),
301
- stack: [o]
302
- }, d = b({ "": "" }, i);
303
- for (const c of i.children) {
317
+ stack: [i]
318
+ }, d = k({ "": "" }, o);
319
+ for (const c of o.children) {
304
320
  if (c.type !== "element")
305
321
  continue;
306
- const f = b(d, c);
307
- if (X(c))
308
- return await g(c, a, f, s), y(c);
309
- if (Z(c, f)) {
310
- const p = (await Q(
322
+ const u = k(d, c);
323
+ if (z(c))
324
+ return await v(c, s, u, a), $(c);
325
+ if (ae(c, u)) {
326
+ const m = (await ce(
311
327
  c,
312
- a,
313
- f,
314
- s
328
+ s,
329
+ u,
330
+ a
315
331
  )).find(
316
- (m) => m.type === "element" && X(m)
332
+ (w) => w.type === "element" && z(w)
317
333
  );
318
- if (p)
319
- return y(p);
334
+ if (m)
335
+ return $(m);
320
336
  continue;
321
337
  }
322
338
  break;
@@ -326,74 +342,74 @@ async function ve(e, t, n) {
326
342
  return;
327
343
  }
328
344
  }
329
- function be(e, t) {
345
+ function Ae(e, t) {
330
346
  for (const n of e.children) {
331
347
  if (n.type !== "element")
332
348
  continue;
333
- const r = b(t, n), i = n.name.indexOf(":"), o = i === -1 ? "" : n.name.slice(0, i);
334
- if ((i === -1 ? n.name : n.name.slice(i + 1)) === "fallback" && r[o] === q)
349
+ const r = k(t, n), o = n.name.indexOf(":"), i = o === -1 ? "" : n.name.slice(0, o);
350
+ if ((o === -1 ? n.name : n.name.slice(o + 1)) === "fallback" && r[i] === ie)
335
351
  return n;
336
352
  }
337
353
  }
338
- function ee(e) {
354
+ function le(e) {
339
355
  return typeof e == "object" && e !== null && e.type === "element";
340
356
  }
341
- function xe(e) {
357
+ function Pe(e) {
342
358
  return /^(pf|xhtml):/.test(e.name);
343
359
  }
344
- function Ee(e, t, n) {
345
- const r = [], i = e.children.find(ee);
346
- if (i) {
347
- const o = n?.parentId ?? "root";
348
- te(i, o, n?.position ?? 1, t, o, r);
360
+ function _e(e, t, n) {
361
+ const r = [], o = e.children.find(le);
362
+ if (o) {
363
+ const i = n?.parentId ?? "root";
364
+ de(o, i, n?.position ?? 1, t, i, r);
349
365
  }
350
366
  return r;
351
367
  }
352
- function te(e, t, n, r, i, o) {
353
- const a = ge(e) ?? r;
354
- let s, d;
355
- if (xe(e))
368
+ function de(e, t, n, r, o, i) {
369
+ const s = De(e) ?? r;
370
+ let a, d;
371
+ if (Pe(e))
356
372
  d = "root";
357
373
  else {
358
- const f = e.attributes.label ?? e.attributes["xml:id"];
359
- s = f ?? `${t}-${n}`, o.push({
360
- id: s,
361
- file: a,
374
+ const u = e.attributes.label ?? e.attributes["xml:id"];
375
+ a = u ?? `${t}-${n}`, i.push({
376
+ id: a,
377
+ file: s,
362
378
  line: e.position?.start.line ?? 1,
363
379
  column: e.position?.start.column ?? 1,
364
380
  endLine: e.position?.end.line ?? e.position?.start.line ?? 1,
365
- parent: i
366
- }), d = s;
381
+ parent: o
382
+ }), d = a;
367
383
  }
368
384
  let c = 0;
369
- for (const f of e.children)
370
- ee(f) && (c += 1, te(
371
- f,
385
+ for (const u of e.children)
386
+ le(u) && (c += 1, de(
387
+ u,
372
388
  d,
373
389
  c,
374
- a,
375
- s ?? i,
376
- o
390
+ s,
391
+ a ?? o,
392
+ i
377
393
  ));
378
394
  }
379
- function Be(e, t) {
395
+ function qe(e, t) {
380
396
  let n;
381
397
  for (const r of e)
382
398
  r.line <= t && (n = r);
383
399
  return n ?? e[0];
384
400
  }
385
- const ne = "pretext-html:set-theme";
386
- function Xe(e) {
401
+ const ue = "pretext-html:set-theme";
402
+ function Ve(e) {
387
403
  return e === "dark" || e === "light" || e === "system";
388
404
  }
389
- function Ce(e) {
390
- return { type: ne, theme: e };
405
+ function Qe(e) {
406
+ return { type: ue, theme: e };
391
407
  }
392
- function $e(e) {
408
+ function Le(e) {
393
409
  return [
394
410
  "<script>",
395
411
  "(function () {",
396
- " var CFG = " + JSON.stringify({ msg: ne, initial: e }) + ";",
412
+ " var CFG = " + JSON.stringify({ msg: ue, initial: e }) + ";",
397
413
  // Where the last requested theme is remembered so it survives an in-place
398
414
  // document rewrite (same Window) and outranks the baked initial value.
399
415
  " var KEY = '__ptxPreviewTheme';",
@@ -463,188 +479,223 @@ function $e(e) {
463
479
  ].join(`
464
480
  `);
465
481
  }
466
- function ke(e, t) {
467
- const n = $e(t);
482
+ function Fe(e, t) {
483
+ const n = Le(t);
468
484
  return /<head[^>]*>/i.test(e) ? e.replace(/<head([^>]*)>/i, `<head$1>
469
485
  ${n}`) : e;
470
486
  }
471
- function Me() {
472
- return H(v(), "xsl");
487
+ function je() {
488
+ return Q(T(), "xsl");
473
489
  }
474
- function Te() {
490
+ function Re() {
475
491
  return "Suspending" in WebAssembly;
476
492
  }
477
- function Ie() {
478
- if (!Te())
493
+ function We() {
494
+ if (!Re())
479
495
  throw new Error(
480
496
  "WebAssembly JSPI is not enabled. Run Node with --experimental-wasm-jspi (or use the pretext-html CLI, which re-launches itself with the flag)."
481
497
  );
482
498
  }
483
- function C(e) {
499
+ function J(e) {
484
500
  return e.includes("'") ? e.includes('"') ? `concat(${e.split("'").map((n) => `'${n}'`).join(`, "'", `)})` : `"${e}"` : `'${e}'`;
485
501
  }
486
- function Se(e) {
502
+ function Be(e) {
487
503
  return e.replace(
488
504
  /from '\.\/(https:\/\/[^']+)'/g,
489
505
  (t, n) => `from '${n}'`
490
506
  );
491
507
  }
492
- function De(e) {
508
+ function Xe(e) {
493
509
  return e.slice(0, 65536).replace(/<\?[\s\S]*?\?>/g, " ").replace(/<!--[\s\S]*?-->/g, " ").replace(/<!DOCTYPE[^>]*>/gi, " ").match(/<\s*([A-Za-z][\w:-]*)/)?.[1];
494
510
  }
495
- async function W(e) {
496
- const t = await S(e);
511
+ async function Y(e) {
512
+ const t = await L(e);
497
513
  if (t === void 0)
498
514
  throw new Error(
499
515
  `Could not read ${e}. (In the browser build only absolute URLs are readable — pass sourceContent instead of relying on sourcePath.)`
500
516
  );
501
517
  return t;
502
518
  }
503
- async function _e(e, t) {
504
- const n = h(e), r = await S(n);
519
+ async function Ce(e, t) {
520
+ const n = h(e), r = await L(n);
505
521
  if (r !== void 0)
506
- return ve(r, n, t);
522
+ return Ie(r, n, t);
507
523
  }
508
- function Le(e, t, n) {
509
- const r = e.replace(/^\uFEFF/, "").replace(/<\?xml[\s\S]*?\?>/, "").replace(/<!DOCTYPE[^>]*>/i, ""), i = t === "chapter" || t === "part" ? "book" : "article";
524
+ function Ue(e, t, n) {
525
+ const r = e.replace(/^\uFEFF/, "").replace(/<\?xml[\s\S]*?\?>/, "").replace(/<!DOCTYPE[^>]*>/i, ""), o = t === "chapter" || t === "part" ? "book" : "article";
510
526
  return `<pretext>
511
527
  ${n?.trim() ? `${n.trim()}
512
- ` : ""}<${i}>
528
+ ` : ""}<${o}>
513
529
  <title/>
514
530
  ${r}
515
- </${i}>
531
+ </${o}>
516
532
  </pretext>
517
533
  `;
518
534
  }
519
- function U(e) {
520
- if (e instanceof Error && /memory access|out of bounds/i.test(e.message))
535
+ let fe = !1;
536
+ function He(e) {
537
+ return e instanceof Error && /\bAborted\(/.test(e.message);
538
+ }
539
+ function Oe(e) {
540
+ return e instanceof Error && /memory access|out of bounds/i.test(e.message);
541
+ }
542
+ function Ne() {
543
+ if (fe)
544
+ throw new Error(
545
+ "The WebAssembly renderer aborted earlier in this process and cannot be reused. Restart the process (or reload the page) to render again."
546
+ );
547
+ }
548
+ function G(e) {
549
+ if (He(e))
550
+ throw fe = !0, new Error(
551
+ "The WebAssembly renderer aborted and cannot be reused. Restart the process (or reload the page) to render again.",
552
+ { cause: e }
553
+ );
554
+ if (Oe(e))
521
555
  throw new Error(
522
- "The document is too large for the current WebAssembly build (stack overflow). Whole-book builds are a known limitation; try a smaller document, or see the README about rebuilding libxslt-wasm with a larger stack.",
556
+ "The WebAssembly renderer hit a memory fault. Most often the document is too large for this build's stack — whole-book builds are a known limitation, so try a smaller document, or see the README about rebuilding libxslt-wasm with a larger stack. The same fault also results from running two renders concurrently against the shared libxslt state.",
523
557
  { cause: e }
524
558
  );
525
559
  }
526
- let k;
527
- async function re() {
528
- return k || (k = (async () => {
560
+ let I;
561
+ async function he() {
562
+ return I || (I = (async () => {
529
563
  const [e, t] = await Promise.all([
530
564
  import("@pretextbook/libxslt-wasm"),
531
565
  import("@pretextbook/libxslt-wasm/exslt")
532
566
  ]);
533
567
  return t.registerAll(), e;
534
- })()), k;
568
+ })()), I;
535
569
  }
536
- const M = /* @__PURE__ */ new Map();
537
- async function Pe(e) {
570
+ const A = /* @__PURE__ */ new Map();
571
+ async function ze(e) {
538
572
  const t = e;
539
- let n = M.get(t);
573
+ let n = A.get(t);
540
574
  return n || (n = (async () => {
541
- const { XmlDocument: r, XsltStylesheet: i } = await re(), o = G(t), a = await N(
542
- v(),
575
+ const { XmlDocument: r, XsltStylesheet: o } = await he(), i = re(t), s = await Z(
576
+ T(),
543
577
  "/preview-html.xsl"
544
578
  );
545
- if (a === void 0)
579
+ if (s === void 0)
546
580
  throw new Error(
547
- `Could not read preview-html.xsl from ${v()}. Check the assets location (PRETEXT_HTML_ASSETS under Node, setAssetsBase() in the browser).`
581
+ `Could not read preview-html.xsl from ${T()}. Check the assets location (PRETEXT_HTML_ASSETS under Node, setAssetsBase() in the browser).`
548
582
  );
549
- const s = new TextDecoder().decode(a), d = await r.fromString(s, {
550
- url: `${o}/preview-html.xsl`,
583
+ const a = new TextDecoder().decode(s), d = await r.fromString(a, {
584
+ url: `${i}/preview-html.xsl`,
551
585
  options: { noEnt: !0, dtdLoad: !0, huge: !0 }
552
586
  });
553
- return i.fromXmlDocument(d);
554
- })(), M.set(t, n), n.catch(() => M.delete(t))), n;
555
- }
556
- async function We(e) {
557
- Ie();
558
- const { XmlDocument: t } = await re(), n = await Pe(e.xslDir ?? Me()), r = h(e.sourcePath), i = h(
559
- e.projectDir ?? E(r)
560
- ), o = e.sourceContent ?? await W(r), a = F(i, r);
561
- if (a.startsWith(".."))
587
+ return o.fromXmlDocument(d);
588
+ })(), A.set(t, n), n.catch(() => A.delete(t))), n;
589
+ }
590
+ let K = Promise.resolve();
591
+ function Ze(e) {
592
+ const t = K.then(
593
+ () => q(e),
594
+ () => q(e)
595
+ );
596
+ return K = t.catch(() => {
597
+ }), t;
598
+ }
599
+ async function q(e) {
600
+ We(), Ne();
601
+ const { XmlDocument: t } = await he(), n = await ze(e.xslDir ?? je()), r = h(e.sourcePath), o = h(
602
+ e.projectDir ?? b(r)
603
+ ), i = e.sourceContent ?? await Y(r), s = S(o, r);
604
+ if (s.startsWith(".."))
562
605
  throw new Error(
563
- `sourcePath (${r}) must live inside projectDir (${i})`
606
+ `sourcePath (${r}) must live inside projectDir (${o})`
564
607
  );
565
- const s = G(i);
608
+ const a = S(
609
+ o,
610
+ h(e.mainSourcePath ?? r)
611
+ ), d = a.startsWith("..") ? s : a, c = re(o);
566
612
  try {
567
- let d, c = `${s}/__pretext-preview-publication.xml`;
613
+ let u, f = `${c}/__pretext-preview-publication.xml`;
568
614
  if (e.publicationPath) {
569
- const u = h(e.publicationPath);
570
- d = R(
571
- await W(u)
615
+ const l = h(e.publicationPath);
616
+ u = H(
617
+ await Y(l)
572
618
  );
573
- const w = F(i, u);
574
- w.startsWith("..") || (c = `${s}/${w.split(T).join("/")}`);
619
+ const g = S(o, l);
620
+ g.startsWith("..") || (f = `${c}/${g.split(P).join("/")}`);
575
621
  } else
576
- d = R();
577
- pe(c, d);
578
- let f, l;
622
+ u = H();
623
+ Te(f, u);
624
+ const m = ke(u), w = b(h(o, d)), j = m.external !== void 0 && m.generated !== void 0 ? {
625
+ external: h(w, m.external),
626
+ generated: h(w, m.generated)
627
+ } : void 0;
628
+ let M, p;
579
629
  if (e.sourceMap) {
580
- const u = await V(
581
- o,
630
+ const l = await se(
631
+ i,
582
632
  r,
583
- i
633
+ o
584
634
  );
585
- f = u.tree, l = u.content;
635
+ M = l.tree, p = l.content;
586
636
  } else
587
- l = await we(
588
- o,
637
+ p = await Me(
638
+ i,
589
639
  r,
590
- i
640
+ o
591
641
  );
592
- const p = De(l);
593
- let m = { parentId: "root", position: 1 };
594
- if (p !== "pretext" && p !== "mathbook")
642
+ const E = Xe(p);
643
+ let R = { parentId: "root", position: 1 };
644
+ if (E !== "pretext" && E !== "mathbook")
595
645
  if (e.fragment) {
596
- const u = e.docinfo ?? (e.docinfoSourcePath ? await _e(e.docinfoSourcePath, i) : void 0);
597
- l = Le(l, p, u), m = { parentId: u?.trim() ? "root-1-2" : "root-1-1", position: 2 };
646
+ const l = e.docinfo ?? (e.docinfoSourcePath ? await Ce(e.docinfoSourcePath, o) : void 0);
647
+ p = Ue(p, E, l), R = { parentId: l?.trim() ? "root-1-2" : "root-1-1", position: 2 };
598
648
  } else
599
649
  throw new Error(
600
- `The document root is <${p ?? "?"}>, not <pretext> — this looks like an xi:included fragment. Render the project's main source file instead, or pass the fragment option.`
650
+ `The document root is <${E ?? "?"}>, not <pretext> — this looks like an xi:included fragment. Render the project's main source file instead, or pass the fragment option.`
601
651
  );
602
- const _ = f ? Ee(f, r, m) : void 0, L = await t.fromString(l, {
603
- url: `${s}/${a.split(T).join("/")}`,
652
+ const W = M ? _e(M, r, R) : void 0, B = await t.fromString(p, {
653
+ url: `${c}/${d.split(P).join("/")}`,
604
654
  options: { noEnt: !0, dtdLoad: !0, huge: !0 }
605
- }), P = {
606
- publisher: C(c)
655
+ }), X = {
656
+ publisher: J(f)
607
657
  };
608
- for (const [u, w] of Object.entries(e.stringParams ?? {}))
609
- P[u] = C(w);
610
- let $;
658
+ for (const [l, g] of Object.entries(e.stringParams ?? {}))
659
+ X[l] = J(g);
660
+ let D;
611
661
  try {
612
- $ = await n.apply(L, P);
613
- } catch (u) {
614
- throw U(u), new Error(
662
+ D = await n.apply(B, X);
663
+ } catch (l) {
664
+ throw G(l), new Error(
615
665
  "PreTeXt XSLT transform failed; see the PTX:ERROR/PTX:FATAL messages on stderr for details.",
616
- { cause: u }
666
+ { cause: l }
617
667
  );
618
668
  } finally {
619
- L.delete();
669
+ B.delete();
620
670
  }
621
671
  try {
622
- let u = Se($.toHtmlString());
623
- return e.theme && (u = ke(u, e.theme)), {
624
- html: u,
625
- ..._ ? { sourceMap: _ } : {}
672
+ let l = Be(D.toHtmlString());
673
+ return e.theme && (l = Fe(l, e.theme)), {
674
+ html: l,
675
+ ...W ? { sourceMap: W } : {},
676
+ ...j ? { assetDirs: j } : {}
626
677
  };
627
- } catch (u) {
628
- throw U(u), u;
678
+ } catch (l) {
679
+ throw G(l), l;
629
680
  } finally {
630
- $.delete();
681
+ D.delete();
631
682
  }
632
683
  } finally {
633
- he(s);
684
+ $e(c);
634
685
  }
635
686
  }
636
687
  export {
637
- ne as PREVIEW_THEME_MESSAGE,
638
- Me as defaultXslDir,
639
- Be as findSourceMapEntry,
640
- R as forcePortablePublication,
641
- ke as injectThemeBridge,
642
- Te as isJspiAvailable,
643
- Xe as isPreviewTheme,
644
- Ce as previewThemeMessage,
645
- We as renderHtml,
646
- Ae as setAssetsBase,
647
- Re as setMountReader,
648
- $e as themeBridgeScript,
649
- C as xpathStringLiteral
688
+ ue as PREVIEW_THEME_MESSAGE,
689
+ je as defaultXslDir,
690
+ qe as findSourceMapEntry,
691
+ H as forcePortablePublication,
692
+ Fe as injectThemeBridge,
693
+ Re as isJspiAvailable,
694
+ Ve as isPreviewTheme,
695
+ Qe as previewThemeMessage,
696
+ Ze as renderHtml,
697
+ Ge as setAssetsBase,
698
+ Ke as setMountReader,
699
+ Le as themeBridgeScript,
700
+ J as xpathStringLiteral
650
701
  };