@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.
- package/README.md +20 -0
- package/dist/assets.d.ts +43 -0
- package/dist/assets.js +76 -0
- package/dist/{cli-B2LiKunq.js → cli-CzoinNle.js} +264 -213
- package/dist/cli.js +1 -1
- package/dist/index.browser.js +343 -292
- package/dist/index.d.ts +5 -0
- package/dist/index.js +16 -11
- package/dist/missing-asset.d.ts +31 -0
- package/dist/publication.d.ts +27 -0
- package/dist/renderer.d.ts +29 -0
- package/package.json +9 -1
package/dist/index.browser.js
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
1
|
import { fromXml as x } from "xast-util-from-xml";
|
|
2
|
-
import { toXml as
|
|
3
|
-
const
|
|
4
|
-
function
|
|
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
|
|
9
|
-
if (!(!
|
|
10
|
-
if (
|
|
11
|
-
const
|
|
12
|
-
r.length > 0 &&
|
|
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(
|
|
15
|
+
r.push(i);
|
|
16
16
|
}
|
|
17
|
-
let
|
|
18
|
-
return 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
|
|
24
|
-
if (
|
|
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 =
|
|
28
|
+
const n = me(t);
|
|
29
29
|
return n.length > 1 && n.endsWith("/") ? n.slice(0, -1) : n;
|
|
30
30
|
}
|
|
31
|
-
function
|
|
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
|
|
38
|
+
function S(e, t) {
|
|
39
39
|
const n = h(e).split("/").filter(Boolean), r = h(t).split("/").filter(Boolean);
|
|
40
|
-
let
|
|
41
|
-
for (;
|
|
42
|
-
|
|
43
|
-
return [...new Array(n.length -
|
|
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
|
|
46
|
-
let
|
|
47
|
-
function
|
|
48
|
-
|
|
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
|
|
51
|
-
return
|
|
50
|
+
function T() {
|
|
51
|
+
return V ?? pe;
|
|
52
52
|
}
|
|
53
|
-
function
|
|
53
|
+
function Q(e, t) {
|
|
54
54
|
return `${e.replace(/\/+$/, "")}/${t.replace(/^\/+/, "")}`;
|
|
55
55
|
}
|
|
56
|
-
const
|
|
57
|
-
function
|
|
56
|
+
const C = /* @__PURE__ */ new Map();
|
|
57
|
+
function we(e) {
|
|
58
58
|
return `${e.replace(/\/+$/, "")}-bundle.json`;
|
|
59
59
|
}
|
|
60
|
-
function
|
|
61
|
-
let t =
|
|
60
|
+
function ge(e) {
|
|
61
|
+
let t = C.get(e);
|
|
62
62
|
return t || (t = (async () => {
|
|
63
63
|
try {
|
|
64
|
-
const n = await fetch(
|
|
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
|
-
})(),
|
|
72
|
+
})(), C.set(e, t)), t;
|
|
73
73
|
}
|
|
74
|
-
function
|
|
75
|
-
return e ===
|
|
74
|
+
function ye(e) {
|
|
75
|
+
return e === Q(T(), "xsl");
|
|
76
76
|
}
|
|
77
|
-
async function
|
|
78
|
-
if (
|
|
79
|
-
const
|
|
80
|
-
if (
|
|
81
|
-
return new TextEncoder().encode(
|
|
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
|
|
98
|
-
return
|
|
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
|
|
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
|
|
113
|
-
let
|
|
114
|
-
function
|
|
115
|
-
|
|
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
|
|
118
|
-
const n = typeof e == "string" ? e : e instanceof URL ? e.href : e.url, r =
|
|
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
|
|
121
|
+
let o;
|
|
122
122
|
try {
|
|
123
|
-
|
|
123
|
+
o = new URL(n).host;
|
|
124
124
|
} catch {
|
|
125
|
-
|
|
125
|
+
o = "";
|
|
126
126
|
}
|
|
127
|
-
const
|
|
128
|
-
return
|
|
127
|
+
const i = F.get(o);
|
|
128
|
+
return i !== void 0 ? Ee(i, n) : _(e, t);
|
|
129
129
|
}
|
|
130
|
-
const
|
|
131
|
-
async function
|
|
132
|
-
const n = decodeURIComponent(new URL(t).pathname), r = await
|
|
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(
|
|
135
|
-
const
|
|
136
|
-
return new Response(
|
|
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
|
|
139
|
-
|
|
138
|
+
function ne() {
|
|
139
|
+
_ || (_ = globalThis.fetch, globalThis.fetch = ve);
|
|
140
140
|
}
|
|
141
|
-
function
|
|
142
|
-
|
|
143
|
-
const t = `mnt${++
|
|
144
|
-
return
|
|
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
|
|
147
|
-
|
|
146
|
+
function $e(e) {
|
|
147
|
+
F.delete(new URL(e).host);
|
|
148
148
|
}
|
|
149
|
-
function
|
|
150
|
-
|
|
149
|
+
function Te(e, t) {
|
|
150
|
+
ne(), ee.set(e, t);
|
|
151
151
|
}
|
|
152
|
-
const
|
|
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
|
|
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
|
|
168
|
-
let n =
|
|
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
|
|
171
|
+
function H(e) {
|
|
172
172
|
if (!e)
|
|
173
|
-
return
|
|
174
|
-
const t = x(e), n =
|
|
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 =
|
|
180
|
-
return
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const
|
|
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
|
-
},
|
|
206
|
+
}, i = await v(
|
|
191
207
|
r,
|
|
192
|
-
|
|
208
|
+
b(h(t)),
|
|
193
209
|
{ "": "" },
|
|
194
|
-
|
|
210
|
+
o
|
|
195
211
|
);
|
|
196
|
-
return { tree: r, content:
|
|
212
|
+
return { tree: r, content: i ? $(r) : e };
|
|
197
213
|
}
|
|
198
|
-
function
|
|
214
|
+
function De(e) {
|
|
199
215
|
return e.data?.ptxIncludedFile;
|
|
200
216
|
}
|
|
201
|
-
function
|
|
217
|
+
function Se(e, t) {
|
|
202
218
|
const n = e.data ?? (e.data = {});
|
|
203
219
|
n.ptxIncludedFile = t;
|
|
204
220
|
}
|
|
205
|
-
function
|
|
221
|
+
function k(e, t) {
|
|
206
222
|
let n = e;
|
|
207
|
-
for (const [r,
|
|
208
|
-
(r === "xmlns" || r.startsWith("xmlns:")) && (n === e && (n = { ...e }), n[r === "xmlns" ? "" : r.slice(6)] =
|
|
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
|
|
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] ===
|
|
229
|
+
return (n === -1 ? e.name : e.name.slice(n + 1)) === "include" && t[r] === ie;
|
|
214
230
|
}
|
|
215
|
-
async function
|
|
216
|
-
let
|
|
217
|
-
for (let
|
|
218
|
-
const
|
|
219
|
-
if (
|
|
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
|
|
222
|
-
if (
|
|
223
|
-
const d = await
|
|
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(
|
|
230
|
-
} else await
|
|
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
|
|
248
|
+
return o;
|
|
233
249
|
}
|
|
234
|
-
async function
|
|
235
|
-
const
|
|
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 ${
|
|
254
|
+
`xi:include with xpointer is not supported by the JS preview build (in ${o})`
|
|
239
255
|
);
|
|
240
|
-
const
|
|
241
|
-
if (!
|
|
242
|
-
throw new Error(`xi:include without href (in ${
|
|
243
|
-
let
|
|
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
|
-
|
|
261
|
+
s = decodeURIComponent(i);
|
|
246
262
|
} catch {
|
|
247
263
|
}
|
|
248
|
-
const
|
|
249
|
-
if (
|
|
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: ${
|
|
267
|
+
`xi:include escapes the project directory: ${i} (in ${o})`
|
|
252
268
|
);
|
|
253
|
-
if (r.stack.includes(
|
|
269
|
+
if (r.stack.includes(a))
|
|
254
270
|
throw new Error(
|
|
255
|
-
`Circular xi:include: ${[...r.stack,
|
|
271
|
+
`Circular xi:include: ${[...r.stack, a].join(" -> ")}`
|
|
256
272
|
);
|
|
257
|
-
if (r.stack.length >=
|
|
258
|
-
throw new Error(`xi:include nesting exceeds ${
|
|
259
|
-
const d = await
|
|
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
|
|
262
|
-
if (
|
|
263
|
-
return await
|
|
264
|
-
throw new Error(`xi:include target not found: ${
|
|
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 (
|
|
287
|
+
} catch (f) {
|
|
272
288
|
throw new Error(
|
|
273
|
-
`Could not parse xi:include target ${
|
|
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(
|
|
292
|
+
r.stack.push(a);
|
|
277
293
|
try {
|
|
278
|
-
await
|
|
294
|
+
await v(c, b(a), { "": "" }, r);
|
|
279
295
|
} finally {
|
|
280
296
|
r.stack.pop();
|
|
281
297
|
}
|
|
282
|
-
const
|
|
283
|
-
(
|
|
298
|
+
const u = c.children.filter(
|
|
299
|
+
(f) => f.type === "element"
|
|
284
300
|
);
|
|
285
|
-
for (const
|
|
286
|
-
|
|
287
|
-
return
|
|
301
|
+
for (const f of u)
|
|
302
|
+
Se(f, a);
|
|
303
|
+
return u;
|
|
288
304
|
}
|
|
289
|
-
function
|
|
305
|
+
function z(e) {
|
|
290
306
|
return e.name === "docinfo";
|
|
291
307
|
}
|
|
292
|
-
async function
|
|
308
|
+
async function Ie(e, t, n) {
|
|
293
309
|
try {
|
|
294
|
-
const
|
|
310
|
+
const o = x(e).children.find(
|
|
295
311
|
(c) => c.type === "element"
|
|
296
312
|
);
|
|
297
|
-
if (!
|
|
313
|
+
if (!o)
|
|
298
314
|
return;
|
|
299
|
-
const
|
|
315
|
+
const i = h(t), s = b(i), a = {
|
|
300
316
|
projectDir: h(n),
|
|
301
|
-
stack: [
|
|
302
|
-
}, d =
|
|
303
|
-
for (const c of
|
|
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
|
|
307
|
-
if (
|
|
308
|
-
return await
|
|
309
|
-
if (
|
|
310
|
-
const
|
|
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
|
-
|
|
313
|
-
|
|
314
|
-
|
|
328
|
+
s,
|
|
329
|
+
u,
|
|
330
|
+
a
|
|
315
331
|
)).find(
|
|
316
|
-
(
|
|
332
|
+
(w) => w.type === "element" && z(w)
|
|
317
333
|
);
|
|
318
|
-
if (
|
|
319
|
-
return
|
|
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
|
|
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 =
|
|
334
|
-
if ((
|
|
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
|
|
354
|
+
function le(e) {
|
|
339
355
|
return typeof e == "object" && e !== null && e.type === "element";
|
|
340
356
|
}
|
|
341
|
-
function
|
|
357
|
+
function Pe(e) {
|
|
342
358
|
return /^(pf|xhtml):/.test(e.name);
|
|
343
359
|
}
|
|
344
|
-
function
|
|
345
|
-
const r = [],
|
|
346
|
-
if (
|
|
347
|
-
const
|
|
348
|
-
|
|
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
|
|
353
|
-
const
|
|
354
|
-
let
|
|
355
|
-
if (
|
|
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
|
|
359
|
-
|
|
360
|
-
id:
|
|
361
|
-
file:
|
|
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:
|
|
366
|
-
}), d =
|
|
381
|
+
parent: o
|
|
382
|
+
}), d = a;
|
|
367
383
|
}
|
|
368
384
|
let c = 0;
|
|
369
|
-
for (const
|
|
370
|
-
|
|
371
|
-
|
|
385
|
+
for (const u of e.children)
|
|
386
|
+
le(u) && (c += 1, de(
|
|
387
|
+
u,
|
|
372
388
|
d,
|
|
373
389
|
c,
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
390
|
+
s,
|
|
391
|
+
a ?? o,
|
|
392
|
+
i
|
|
377
393
|
));
|
|
378
394
|
}
|
|
379
|
-
function
|
|
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
|
|
386
|
-
function
|
|
401
|
+
const ue = "pretext-html:set-theme";
|
|
402
|
+
function Ve(e) {
|
|
387
403
|
return e === "dark" || e === "light" || e === "system";
|
|
388
404
|
}
|
|
389
|
-
function
|
|
390
|
-
return { type:
|
|
405
|
+
function Qe(e) {
|
|
406
|
+
return { type: ue, theme: e };
|
|
391
407
|
}
|
|
392
|
-
function
|
|
408
|
+
function Le(e) {
|
|
393
409
|
return [
|
|
394
410
|
"<script>",
|
|
395
411
|
"(function () {",
|
|
396
|
-
" var CFG = " + JSON.stringify({ msg:
|
|
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
|
|
467
|
-
const n =
|
|
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
|
|
472
|
-
return
|
|
487
|
+
function je() {
|
|
488
|
+
return Q(T(), "xsl");
|
|
473
489
|
}
|
|
474
|
-
function
|
|
490
|
+
function Re() {
|
|
475
491
|
return "Suspending" in WebAssembly;
|
|
476
492
|
}
|
|
477
|
-
function
|
|
478
|
-
if (!
|
|
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
|
|
499
|
+
function J(e) {
|
|
484
500
|
return e.includes("'") ? e.includes('"') ? `concat(${e.split("'").map((n) => `'${n}'`).join(`, "'", `)})` : `"${e}"` : `'${e}'`;
|
|
485
501
|
}
|
|
486
|
-
function
|
|
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
|
|
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
|
|
496
|
-
const t = await
|
|
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
|
|
504
|
-
const n = h(e), r = await
|
|
519
|
+
async function Ce(e, t) {
|
|
520
|
+
const n = h(e), r = await L(n);
|
|
505
521
|
if (r !== void 0)
|
|
506
|
-
return
|
|
522
|
+
return Ie(r, n, t);
|
|
507
523
|
}
|
|
508
|
-
function
|
|
509
|
-
const r = e.replace(/^\uFEFF/, "").replace(/<\?xml[\s\S]*?\?>/, "").replace(/<!DOCTYPE[^>]*>/i, ""),
|
|
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
|
-
` : ""}<${
|
|
528
|
+
` : ""}<${o}>
|
|
513
529
|
<title/>
|
|
514
530
|
${r}
|
|
515
|
-
</${
|
|
531
|
+
</${o}>
|
|
516
532
|
</pretext>
|
|
517
533
|
`;
|
|
518
534
|
}
|
|
519
|
-
|
|
520
|
-
|
|
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
|
|
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
|
|
527
|
-
async function
|
|
528
|
-
return
|
|
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
|
-
})()),
|
|
568
|
+
})()), I;
|
|
535
569
|
}
|
|
536
|
-
const
|
|
537
|
-
async function
|
|
570
|
+
const A = /* @__PURE__ */ new Map();
|
|
571
|
+
async function ze(e) {
|
|
538
572
|
const t = e;
|
|
539
|
-
let n =
|
|
573
|
+
let n = A.get(t);
|
|
540
574
|
return n || (n = (async () => {
|
|
541
|
-
const { XmlDocument: r, XsltStylesheet:
|
|
542
|
-
|
|
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 (
|
|
579
|
+
if (s === void 0)
|
|
546
580
|
throw new Error(
|
|
547
|
-
`Could not read preview-html.xsl from ${
|
|
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
|
|
550
|
-
url: `${
|
|
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
|
|
554
|
-
})(),
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
const
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
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 (${
|
|
606
|
+
`sourcePath (${r}) must live inside projectDir (${o})`
|
|
564
607
|
);
|
|
565
|
-
const
|
|
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
|
|
613
|
+
let u, f = `${c}/__pretext-preview-publication.xml`;
|
|
568
614
|
if (e.publicationPath) {
|
|
569
|
-
const
|
|
570
|
-
|
|
571
|
-
await
|
|
615
|
+
const l = h(e.publicationPath);
|
|
616
|
+
u = H(
|
|
617
|
+
await Y(l)
|
|
572
618
|
);
|
|
573
|
-
const
|
|
574
|
-
|
|
619
|
+
const g = S(o, l);
|
|
620
|
+
g.startsWith("..") || (f = `${c}/${g.split(P).join("/")}`);
|
|
575
621
|
} else
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
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
|
|
581
|
-
|
|
630
|
+
const l = await se(
|
|
631
|
+
i,
|
|
582
632
|
r,
|
|
583
|
-
|
|
633
|
+
o
|
|
584
634
|
);
|
|
585
|
-
|
|
635
|
+
M = l.tree, p = l.content;
|
|
586
636
|
} else
|
|
587
|
-
|
|
588
|
-
|
|
637
|
+
p = await Me(
|
|
638
|
+
i,
|
|
589
639
|
r,
|
|
590
|
-
|
|
640
|
+
o
|
|
591
641
|
);
|
|
592
|
-
const
|
|
593
|
-
let
|
|
594
|
-
if (
|
|
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
|
|
597
|
-
|
|
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 <${
|
|
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
|
|
603
|
-
url: `${
|
|
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
|
-
}),
|
|
606
|
-
publisher:
|
|
655
|
+
}), X = {
|
|
656
|
+
publisher: J(f)
|
|
607
657
|
};
|
|
608
|
-
for (const [
|
|
609
|
-
|
|
610
|
-
let
|
|
658
|
+
for (const [l, g] of Object.entries(e.stringParams ?? {}))
|
|
659
|
+
X[l] = J(g);
|
|
660
|
+
let D;
|
|
611
661
|
try {
|
|
612
|
-
|
|
613
|
-
} catch (
|
|
614
|
-
throw
|
|
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:
|
|
666
|
+
{ cause: l }
|
|
617
667
|
);
|
|
618
668
|
} finally {
|
|
619
|
-
|
|
669
|
+
B.delete();
|
|
620
670
|
}
|
|
621
671
|
try {
|
|
622
|
-
let
|
|
623
|
-
return e.theme && (
|
|
624
|
-
html:
|
|
625
|
-
...
|
|
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 (
|
|
628
|
-
throw
|
|
678
|
+
} catch (l) {
|
|
679
|
+
throw G(l), l;
|
|
629
680
|
} finally {
|
|
630
|
-
|
|
681
|
+
D.delete();
|
|
631
682
|
}
|
|
632
683
|
} finally {
|
|
633
|
-
|
|
684
|
+
$e(c);
|
|
634
685
|
}
|
|
635
686
|
}
|
|
636
687
|
export {
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
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
|
};
|