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