@pie-element/shared-math-rendering-mathjax 0.1.2 → 0.1.3-next.20261007003844

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.
@@ -0,0 +1,129 @@
1
+ //#region src/assets.ts
2
+ var e = "4.1.3", t = "https://github.com/pie-framework/pie-elements-ng/blob/develop/docs/MATH-RENDERING.md#assets", n = "@pie-lib/math-rendering@2", r = ["nemeth", "euro"], i = `mathjax@${e}/sre`, a = /^[^@]+@[^@]+$/;
3
+ function o(e) {
4
+ if (!e) return;
5
+ let t;
6
+ try {
7
+ t = new URL(e);
8
+ } catch {
9
+ return;
10
+ }
11
+ if (t.origin === "null") return;
12
+ let n = t.pathname.split("/");
13
+ for (let e = n.length - 1; e > 0; e--) {
14
+ let r = n[e];
15
+ if (r === "node_modules") return;
16
+ if (!a.test(r)) continue;
17
+ let i = n[e - 1]?.startsWith("@") ? e - 1 : e;
18
+ return t.origin + n.slice(0, i).join("/");
19
+ }
20
+ }
21
+ function s() {
22
+ let e = globalThis[n]?.opts;
23
+ return e && typeof e == "object" ? e : {};
24
+ }
25
+ function c(e) {
26
+ if (typeof e != "string" || !e) return;
27
+ let t = typeof document > "u" ? void 0 : document.baseURI;
28
+ try {
29
+ return new URL(e, t).href.replace(/\/+$/, "");
30
+ } catch {
31
+ return;
32
+ }
33
+ }
34
+ function l(e) {
35
+ if (!e || typeof e != "object") return;
36
+ let t = /* @__PURE__ */ new Map();
37
+ for (let [n, r] of Object.entries(e)) {
38
+ let e = c(r);
39
+ e && t.set(n, e);
40
+ }
41
+ return t.size ? t : void 0;
42
+ }
43
+ function u(e) {
44
+ if (Array.isArray(e)) return new Map(e.filter((e) => typeof e == "string").map((e) => [e, void 0]));
45
+ if (e && typeof e == "object") return new Map(Object.entries(e).map(([e, t]) => [e, typeof t == "string" ? t : void 0]));
46
+ }
47
+ function d(e, t) {
48
+ let n = s(), r = c(e.assetRoot ?? n.assetRoot) ?? o(t);
49
+ return {
50
+ root: r,
51
+ speechPath: c(e.speechPath ?? n.speechPath) ?? (r ? `${r}/${i}` : void 0),
52
+ speechLocales: u(e.speechLocales ?? n.speechLocales),
53
+ urls: l(e.assetUrls ?? n.assetUrls)
54
+ };
55
+ }
56
+ function f({ root: e, urls: t }, n) {
57
+ return t?.get(n) ?? (e ? `${e}/${n}` : void 0);
58
+ }
59
+ function p({ speechPath: e, urls: t }, n) {
60
+ return t?.get(`${i}/${n}`) ?? (e ? `${e}/${n}` : void 0);
61
+ }
62
+ function m(e, t) {
63
+ if (!(!e || !t)) {
64
+ for (let n of [...e.keys()]) !t.has(n) && !r.includes(n) && e.delete(n);
65
+ for (let [n, r] of t) (r || !e.has(n)) && e.set(n, r ?? n);
66
+ }
67
+ }
68
+ function h(e) {
69
+ if (!(!e || e.has("en"))) return [...e.keys()].find((e) => !r.includes(e));
70
+ }
71
+ var g = "pie-mathjax-no-asset-root", _ = {
72
+ untypeset: "no MathJax loads, so math stays untypeset",
73
+ "no-web-fonts": "math renders without web fonts",
74
+ "no-web-fonts-or-speech": "math renders without web fonts and speech"
75
+ }, v = Symbol.for("@pie-element/shared-math-rendering-mathjax/no-assets");
76
+ function y(e) {
77
+ let r = globalThis;
78
+ r[v] ??= [];
79
+ let i = r[v];
80
+ if (i.includes(e)) return;
81
+ i.push(e);
82
+ let a = `[math-rendering] No asset root for MathJax: ${_[e]}. Set window['${n}'].opts.assetRoot to an npm root. See ${t}`;
83
+ e === "untypeset" ? console.error(a) : console.warn(a), !(typeof window > "u") && window.dispatchEvent(new CustomEvent(g, { detail: {
84
+ effect: e,
85
+ message: a,
86
+ docsUrl: t
87
+ } }));
88
+ }
89
+ //#endregion
90
+ //#region src/speech-worker.ts
91
+ var b = Symbol.for("@pie-element/shared-math-rendering-mathjax/speech-worker-failed");
92
+ function x() {
93
+ let e = globalThis;
94
+ e[b] || (e[b] = !0, console.warn(`[math-rendering] MathJax's speech worker did not start, so speech, braille and the explorer are off. A content security policy must allow blob: workers, and scripts and fetches from the asset root. See ${t}`));
95
+ }
96
+ function S(e) {
97
+ let t = e.webworker;
98
+ if (!t || t.ready) return;
99
+ e.options ??= {};
100
+ let n = e.options;
101
+ n.enableSpeech = !1, n.enableBraille = !1, n.speechError = () => {}, t.Post = () => Promise.reject(/* @__PURE__ */ Error("The speech worker did not start")), t.Terminate(), x();
102
+ }
103
+ function C(e) {
104
+ let t = e?.adaptor, n = t?.createWorker;
105
+ !e || !t || !n || (t.createWorker = async (r, i) => {
106
+ let a = await n.call(t, r, i);
107
+ return a.addEventListener("error", () => S(e), { once: !0 }), a;
108
+ });
109
+ }
110
+ var w = `${i}/mathmaps/`;
111
+ function T(e) {
112
+ let t = p(e, "speech-worker.js");
113
+ if (!t) return;
114
+ let n = e.speechPath ? `${e.speechPath}/mathmaps` : "mathmaps", r = {};
115
+ for (let [t, i] of e.urls ?? []) t.startsWith(w) && (r[`${n}/${t.slice(w.length)}`] = i);
116
+ let i = [`self.maps = ${JSON.stringify(n)};`];
117
+ return Object.keys(r).length && i.push("{", ` const urls = new Map(Object.entries(${JSON.stringify(r)}));`, " const fetchFile = self.fetch.bind(self);", " self.fetch = (input, init) => fetchFile(urls.get(input) ?? input, init);", "}"), i.push(`importScripts(${JSON.stringify(t)});`), i.join("\n");
118
+ }
119
+ function E(e, t) {
120
+ let n = URL.createObjectURL(new Blob([e], { type: "text/javascript" }));
121
+ try {
122
+ let e = new Worker(n);
123
+ return e.onmessage = t, e;
124
+ } finally {
125
+ URL.revokeObjectURL(n);
126
+ }
127
+ }
128
+ //#endregion
129
+ export { g as a, m as c, e as i, y as l, T as n, f as o, E as r, h as s, C as t, d as u };
@@ -4,10 +4,17 @@
4
4
  * the same names.
5
5
  */
6
6
  import type { MathJaxGlobal } from '../adapter.js';
7
+ import { type MathjaxAssets } from '../assets.js';
7
8
  import { mathjax3Version } from '../unsupported-page.js';
8
9
  export declare function engineMathJax(): MathJaxGlobal | undefined;
9
- /** Installs `config` as `window.MathJax` and loads the MathJax script, which reads it. */
10
- export declare function loadMathJax(config: MathJaxGlobal, srcUrl: string): Promise<void>;
10
+ /**
11
+ * Installs `config` as `window.MathJax` and loads the MathJax script, which reads it: `srcUrl`, or
12
+ * the component build under the asset root. Resolves to false when there is neither, and no
13
+ * MathJax loads.
14
+ */
15
+ export declare function loadMathJax(config: MathJaxGlobal, assets: MathjaxAssets & {
16
+ srcUrl?: string;
17
+ }): Promise<boolean>;
11
18
  /**
12
19
  * Every element bundles its own copy of this module, so the load in flight is kept on the page:
13
20
  * a second MathJax startup on one page throws "State ASSISTIVEMML already exists".
package/dist/index.d.ts CHANGED
@@ -19,4 +19,6 @@ export { createMathjaxRenderer, getMathjaxCssUrls } from './adapter.js';
19
19
  export type { MathRenderer, MathRenderingAPI, MathjaxOptions } from './types.js';
20
20
  export { MATHJAX_CONFLICT_EVENT, UNSUPPORTED_PAGE_DOCS_URL } from './unsupported-page.js';
21
21
  export type { MathjaxConflictCondition, MathjaxConflictDetail } from './unsupported-page.js';
22
+ export { NO_ASSET_ROOT_EVENT } from './assets.js';
23
+ export type { NoAssetRootDetail, NoAssetRootEffect } from './assets.js';
22
24
  export { renderMath, wrapMath, unWrapMath, mmlToLatex } from './render-math.js';