@pretextbook/pretext-html 0.18.1 → 0.19.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/dist/assets.d.ts CHANGED
@@ -45,3 +45,14 @@ export type AssetUrlResolver = (kind: AssetKind, relPath: string) => string | un
45
45
  * through `resolve`. Paths the resolver declines are left untouched.
46
46
  */
47
47
  export declare function rewriteAssetUrls(html: string, resolve: AssetUrlResolver): string;
48
+ /** {@link AssetUrlResolver} for a host that has to look the file up first. */
49
+ export type AsyncAssetUrlResolver = (kind: AssetKind, relPath: string) => Promise<string | undefined>;
50
+ /**
51
+ * {@link rewriteAssetUrls} for an asynchronous resolver (one that stats the
52
+ * file through an async file system, say). The page is scanned once: every
53
+ * asset URL is handed to the resolver up front, the answers are awaited
54
+ * together, and the rewritten page is assembled from them. The resolver is
55
+ * called once per occurrence; a host wanting one lookup per file caches
56
+ * inside it.
57
+ */
58
+ export declare function rewriteAssetUrlsAsync(html: string, resolve: AsyncAssetUrlResolver): Promise<string>;
package/dist/assets.js CHANGED
@@ -1,4 +1,4 @@
1
- const l = {
1
+ const h = {
2
2
  png: "image",
3
3
  jpg: "image",
4
4
  jpeg: "image",
@@ -15,62 +15,81 @@ const l = {
15
15
  ogg: "audio",
16
16
  pdf: "PDF"
17
17
  };
18
- function p(e, t) {
19
- const n = t.split("/").filter(Boolean), i = n[n.length - 1] ?? t;
18
+ function y(e, t) {
19
+ const n = t.split("/").filter(Boolean), r = n[n.length - 1] ?? t;
20
20
  if (e === "generated")
21
21
  return {
22
22
  kind: e,
23
23
  assetType: n.length > 1 ? n[0] : "generated",
24
- fileName: i
24
+ fileName: r
25
25
  };
26
- const r = i.split(".").pop()?.toLowerCase() ?? "";
27
- return { kind: e, assetType: l[r] ?? "file", fileName: i };
26
+ const i = r.split(".").pop()?.toLowerCase() ?? "";
27
+ return { kind: e, assetType: h[i] ?? "file", fileName: r };
28
28
  }
29
- function o(e) {
29
+ function a(e) {
30
30
  return e.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
31
31
  }
32
- function g(e, t = 38) {
32
+ function p(e, t = 38) {
33
33
  return e.length <= t ? e : `${e.slice(0, t - 1)}…`;
34
34
  }
35
- function f(e) {
35
+ function x(e) {
36
36
  return e.kind === "generated" ? [
37
37
  "Asset not generated yet",
38
- `${e.assetType} · ${g(e.fileName)}`,
38
+ `${e.assetType} · ${p(e.fileName)}`,
39
39
  `Run: pretext generate ${e.assetType}`
40
40
  ] : [
41
41
  "External asset not found",
42
- `${e.assetType} · ${g(e.fileName)}`,
42
+ `${e.assetType} · ${p(e.fileName)}`,
43
43
  "Check @source and the external directory"
44
44
  ];
45
45
  }
46
- const d = "#fffbeb", u = "#f59e0b", m = "#92400e", s = "#b45309", a = "ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif";
47
- function $(e) {
48
- const [t, n, i] = f(e), r = `${t}: ${e.fileName}`;
49
- return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 420 130" role="img" aria-label="${o(r)}" data-ptx-missing="${o(`${e.kind}:${e.assetType}`)}"><rect x="1" y="1" width="418" height="128" rx="8" fill="${d}" stroke="${u}" stroke-width="2" stroke-dasharray="6 4"/><path d="M40 44 L60 80 L20 80 Z" fill="none" stroke="${s}" stroke-width="3" stroke-linejoin="round"/><rect x="38.5" y="56" width="3" height="12" fill="${s}"/><rect x="38.5" y="71" width="3" height="3" fill="${s}"/><text x="78" y="54" font-family="${a}" font-size="15" font-weight="600" fill="${m}">${o(t)}</text><text x="78" y="77" font-family="${a}" font-size="13" fill="${s}">${o(n)}</text><text x="78" y="99" font-family="${a}" font-size="12" fill="${s}" opacity="0.85">${o(i)}</text></svg>`;
46
+ const v = "#fffbeb", w = "#f59e0b", b = "#92400e", c = "#b45309", l = "ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif";
47
+ function A(e) {
48
+ const [t, n, r] = x(e), i = `${t}: ${e.fileName}`;
49
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 420 130" role="img" aria-label="${a(i)}" data-ptx-missing="${a(`${e.kind}:${e.assetType}`)}"><rect x="1" y="1" width="418" height="128" rx="8" fill="${v}" stroke="${w}" stroke-width="2" stroke-dasharray="6 4"/><path d="M40 44 L60 80 L20 80 Z" fill="none" stroke="${c}" stroke-width="3" stroke-linejoin="round"/><rect x="38.5" y="56" width="3" height="12" fill="${c}"/><rect x="38.5" y="71" width="3" height="3" fill="${c}"/><text x="78" y="54" font-family="${l}" font-size="15" font-weight="600" fill="${b}">${a(t)}</text><text x="78" y="77" font-family="${l}" font-size="13" fill="${c}">${a(n)}</text><text x="78" y="99" font-family="${l}" font-size="12" fill="${c}" opacity="0.85">${a(r)}</text></svg>`;
50
50
  }
51
- function v(e, t) {
52
- return `data:image/svg+xml,${$(p(e, t)).replace(/%/g, "%25").replace(/#/g, "%23").replace(/&/g, "%26").replace(/"/g, "%22").replace(/'/g, "%27").replace(/</g, "%3C").replace(/>/g, "%3E").replace(/\s+/g, " ")}`;
51
+ function T(e, t) {
52
+ return `data:image/svg+xml,${A(y(e, t)).replace(/%/g, "%25").replace(/#/g, "%23").replace(/&/g, "%26").replace(/"/g, "%22").replace(/'/g, "%27").replace(/</g, "%3C").replace(/>/g, "%3E").replace(/\s+/g, " ")}`;
53
53
  }
54
- const h = /\bsrc="(external|generated)\/([^"]*)"/g;
55
- function x(e) {
54
+ const d = /\bsrc="(external|generated)\/([^"]*)"/g;
55
+ function u(e) {
56
56
  return e.replace(/&/g, "&amp;").replace(/"/g, "&quot;");
57
57
  }
58
- function y(e) {
58
+ function m(e) {
59
59
  try {
60
60
  return decodeURIComponent(e);
61
61
  } catch {
62
62
  return e;
63
63
  }
64
64
  }
65
- function w(e, t) {
66
- return e.replace(h, (n, i, r) => {
67
- const c = t(i, y(r));
68
- return c === void 0 ? n : `src="${x(c)}"`;
65
+ function E(e, t) {
66
+ return e.replace(d, (n, r, i) => {
67
+ const s = t(r, m(i));
68
+ return s === void 0 ? n : `src="${u(s)}"`;
69
69
  });
70
70
  }
71
+ async function N(e, t) {
72
+ const n = [...e.matchAll(d)];
73
+ if (n.length === 0)
74
+ return e;
75
+ const r = await Promise.all(
76
+ n.map(
77
+ (o) => t(o[1], m(o[2]))
78
+ )
79
+ );
80
+ let i = "", s = 0;
81
+ return n.forEach((o, $) => {
82
+ const g = r[$];
83
+ if (g === void 0)
84
+ return;
85
+ const f = o.index ?? 0;
86
+ i += e.slice(s, f) + `src="${u(g)}"`, s = f + o[0].length;
87
+ }), i + e.slice(s);
88
+ }
71
89
  export {
72
- p as describeAsset,
73
- v as missingAssetPlaceholder,
74
- $ as missingAssetSvg,
75
- w as rewriteAssetUrls
90
+ y as describeAsset,
91
+ T as missingAssetPlaceholder,
92
+ A as missingAssetSvg,
93
+ E as rewriteAssetUrls,
94
+ N as rewriteAssetUrlsAsync
76
95
  };
@@ -183,7 +183,7 @@ ${kt()}
183
183
  ${Rt}`;
184
184
  return r.replace(/<body([^>]*)>/i, `<body$1>${i}`);
185
185
  }
186
- const Dt = "https://cdn.jsdelivr.net/npm/@pretextbook/pretext-html@0.18.1/assets";
186
+ const Dt = "https://cdn.jsdelivr.net/npm/@pretextbook/pretext-html@0.19.0/assets";
187
187
  let Je;
188
188
  function $r(e) {
189
189
  Je = e.replace(/\/+$/, "");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretextbook/pretext-html",
3
- "version": "0.18.1",
3
+ "version": "0.19.0",
4
4
  "description": "Convert PreTeXt documents to HTML in pure JavaScript using the official PreTeXt XSLT stylesheets via libxslt-wasm. No Python or PreTeXt installation required.",
5
5
  "author": "Oscar Levin",
6
6
  "license": "MIT",