@pie-element/shared-math-rendering-mathjax 0.1.1 → 0.1.2-next.20261002222413

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 (2) hide show
  1. package/dist/index.js +118 -78
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -34,49 +34,53 @@ var a = "https://cdn.jsdelivr.net/npm/mathjax@4.1.3/tex-mml-chtml.js", o = {
34
34
  ["$", "$"],
35
35
  ["\\[", "\\]"],
36
36
  ["\\(", "\\)"]
37
- ], f = Symbol.for("@pie-element/shared-math-rendering-mathjax/loading"), p = "PIE-MJX-CHTML-styles", m = "MJX-CHTML-styles", h = [m, "MJX-SVG-styles"], g = 201;
38
- function _(e) {
37
+ ], f = Symbol.for("@pie-element/shared-math-rendering-mathjax/loading"), p = "PIE-MJX-CHTML-styles", m = "MJX-CHTML-styles", h = "PIE-MJX-SVG-styles", g = "MJX-SVG-styles", _ = "PIE-MathJax-Menu-Settings", v = [m, g], y = 200, b = 201;
38
+ function x(e) {
39
39
  let t = [...e.matches("mjx-container") ? [e] : [], ...e.querySelectorAll("mjx-container")];
40
40
  for (let e of t) for (let t of [e, ...e.querySelectorAll("[data-latex],[data-latex-item]")]) t.removeAttribute("data-latex"), t.removeAttribute("data-latex-item");
41
41
  }
42
- function v() {
42
+ function S() {
43
43
  return window.MathJax;
44
44
  }
45
- function y(e) {
45
+ function C(e) {
46
46
  for (let [t, n] of d) if (e.startsWith(t) && e.endsWith(n)) return e.substring(t.length, e.length - n.length);
47
47
  return e;
48
48
  }
49
- function b(e) {
50
- return y(e.includes("\\displaystyle") ? e.replace("\\displaystyle", "").trim() : e);
49
+ function w(e) {
50
+ return C(e.includes("\\displaystyle") ? e.replace("\\displaystyle", "").trim() : e);
51
51
  }
52
- var x = "mathjax_ignore", S = "mathjax_process";
53
- function C(e, t) {
54
- let n = e.closest(`.${x}, .${S}`);
55
- return n !== null && t.contains(n) && !n.classList.contains(S);
52
+ var T = "mathjax_ignore", E = "mathjax_process";
53
+ function D(e, t) {
54
+ let n = e.closest(`.${T}, .${E}`);
55
+ return n !== null && t.contains(n) && !n.classList.contains(E);
56
56
  }
57
- function w(e) {
57
+ function ee(e) {
58
58
  for (let t of e.querySelectorAll("[data-latex]")) {
59
- if (t.dataset.mathHandled || t.closest("mjx-container") || C(t, e)) continue;
59
+ if (t.dataset.mathHandled || t.closest("mjx-container") || D(t, e)) continue;
60
60
  let n = t.textContent;
61
- n && (t.textContent = `\\(${b(n)}\\)`.replace(u, "\\newline "), t.dataset.mathHandled = "true");
61
+ n && (t.textContent = `\\(${w(n)}\\)`.replace(u, "\\newline "), t.dataset.mathHandled = "true");
62
62
  }
63
63
  }
64
- function T(e, t) {
64
+ function te(e, t) {
65
65
  for (let t of e.querySelectorAll("math")) if (!t.closest("mjx-assistive-mml")) return !0;
66
66
  return (t ? l : c).test(e.textContent ?? "");
67
67
  }
68
- function E(e) {
68
+ function O(e) {
69
69
  return new Promise((t, n) => {
70
70
  let { useSingleDollar: r = !1, accessibility: i = !0, loadFonts: c = !0, srcUrl: l } = e, u = {
71
71
  loader: {
72
72
  load: i ? ["a11y/assistive-mml"] : [],
73
- failed: (e) => n(e)
73
+ failed: (e) => n(e),
74
+ "output/svg": { ready: (e) => {
75
+ let t = S();
76
+ return k(t) && A(t), e;
77
+ } }
74
78
  },
75
79
  startup: {
76
80
  typeset: !1,
77
81
  ready: () => {
78
- let e = v(), r = e?.startup, i = !!e?.version?.startsWith("4.");
79
- i && D(e), r?.defaultReady?.(), i && O(r?.document), Promise.resolve(r?.promise).then(() => t(), n);
82
+ let e = S(), r = e?.startup, i = k(e);
83
+ i && (A(e), j(e)), r?.defaultReady?.(), i && M(r?.document), Promise.resolve(r?.promise).then(() => t(), n);
80
84
  }
81
85
  },
82
86
  tex: { macros: { ...o } },
@@ -86,7 +90,8 @@ function E(e) {
86
90
  assistiveMml: i,
87
91
  enrich: !1,
88
92
  inTabOrder: !1
89
- } }
93
+ } },
94
+ a11y: { inTabOrder: !1 }
90
95
  }
91
96
  };
92
97
  r && u.tex && (console.warn(s), u.tex.inlineMath = [["$", "$"], ["\\(", "\\)"]], u.tex.processEscapes = !0), c || (u.chtml = { fontURL: "" }), window.MathJax = u;
@@ -94,143 +99,178 @@ function E(e) {
94
99
  d.src = l || a, d.async = !0, d.onerror = () => n(/* @__PURE__ */ Error("Failed to load MathJax")), document.head.appendChild(d);
95
100
  });
96
101
  }
97
- function D(e) {
98
- let t = e?._?.output?.chtml_ts?.CHTML;
99
- t && (t.STYLESHEETID = p);
102
+ function k(e) {
103
+ return !!e?.version?.startsWith("4.");
104
+ }
105
+ function A(e) {
106
+ let t = e?._?.output, n = t?.chtml_ts?.CHTML;
107
+ n && (n.STYLESHEETID = p);
108
+ let r = t?.svg_ts?.SVG;
109
+ r && (r.STYLESHEETID = h);
100
110
  }
101
- function O(e) {
111
+ function j(e) {
112
+ let t = e?._?.ui?.menu?.Menu?.Menu;
113
+ if (t) {
114
+ t.MENU_STORAGE = _;
115
+ try {
116
+ let e = localStorage.getItem(_);
117
+ if (!e) return;
118
+ let { assistiveMml: t, ...n } = JSON.parse(e);
119
+ if (t === void 0) return;
120
+ Object.keys(n).length ? localStorage.setItem(_, JSON.stringify(n)) : localStorage.removeItem(_);
121
+ } catch {}
122
+ }
123
+ }
124
+ function M(e) {
102
125
  if (!e) return;
103
126
  let t = /* @__PURE__ */ new WeakSet(), n = (e) => {
104
127
  let n = e.typesetRoot;
105
- !n || t.has(n) || (_(n), t.add(n));
128
+ !n || t.has(n) || (x(n), t.add(n));
106
129
  };
107
- e.addRenderAction?.("pie-strip-latex", g, (e) => {
130
+ e.addRenderAction?.("pie-strip-latex", b, (e) => {
108
131
  for (let t of e.math ?? []) n(t);
109
132
  return !1;
110
133
  }, (e) => {
111
134
  let t = e.typesetRoot;
112
- return t && _(t), !1;
135
+ return t && x(t), !1;
113
136
  });
114
137
  let r = e.outputJax?.chtmlStyles;
115
- r?.id === m && (r.id = p), j();
138
+ r?.id === m && (r.id = p), F();
116
139
  }
117
- function k(e) {
118
- return e instanceof HTMLStyleElement && h.includes(e.id);
140
+ function N(e) {
141
+ return e instanceof HTMLStyleElement && v.includes(e.id);
119
142
  }
120
- function A(e) {
143
+ function P(e) {
121
144
  r("foreign-output-stylesheet", `another MathJax added <style id="${e.id}"> to <head>`);
122
145
  }
123
- function j() {
124
- let e = document.head, t = [...e.children].find(k);
146
+ function F() {
147
+ let e = document.head, t = [...e.children].find(N);
125
148
  if (t) {
126
- A(t);
149
+ P(t);
127
150
  return;
128
151
  }
129
152
  let n = new MutationObserver((e) => {
130
153
  for (let t of e) {
131
- let e = [...t.addedNodes].find(k);
154
+ let e = [...t.addedNodes].find(N);
132
155
  if (e) {
133
- A(e), n.disconnect();
156
+ P(e), n.disconnect();
134
157
  return;
135
158
  }
136
159
  }
137
160
  });
138
161
  n.observe(e, { childList: !0 });
139
162
  }
140
- function M(e) {
163
+ function I(e) {
164
+ let t = e?.math;
165
+ if (!t?.remove) return;
166
+ let n = [...t].filter((e) => (e.state?.() ?? 0) >= y && e.typesetRoot?.isConnected === !1);
167
+ if (n.length !== 0) {
168
+ for (let e of [...n].reverse()) e.clear?.();
169
+ t.remove(...n);
170
+ }
171
+ }
172
+ function L(e) {
141
173
  return new Promise((t, n) => {
142
174
  let r = e.startup ?? {};
143
175
  e.startup = r;
144
176
  let i = r.ready;
145
177
  r.ready = () => {
146
- let e = v()?.startup;
178
+ let e = S()?.startup;
147
179
  i ? i.call(r) : e?.defaultReady?.(), Promise.resolve(e?.promise).then(() => t(), n);
148
180
  };
149
181
  });
150
182
  }
151
- function N(e) {
152
- let t = v();
153
- return t ? t.version ? t.startup?.promise ? Promise.resolve(t.startup.promise).then(() => void 0) : Promise.resolve() : M(t) : E(e);
183
+ function R(e) {
184
+ let t = S();
185
+ return t ? t.version ? t.startup?.promise ? Promise.resolve(t.startup.promise).then(() => void 0) : Promise.resolve() : L(t) : O(e);
154
186
  }
155
- function P(e) {
156
- return N(e).then(() => {
157
- typeof v()?.typesetPromise != "function" && console.warn("[mathjax-renderer] MathJax on this page has no typesetPromise; math stays untypeset.");
187
+ function z(e) {
188
+ return R(e).then(() => {
189
+ typeof S()?.typesetPromise != "function" && console.warn("[mathjax-renderer] MathJax on this page has no typesetPromise; math stays untypeset.");
158
190
  });
159
191
  }
160
- function F(e) {
192
+ function B(e) {
161
193
  let t = globalThis, n = t[f];
162
- return n || (n = P(e), t[f] = n), n;
194
+ return n || (n = z(e), t[f] = n), n;
163
195
  }
164
- function I(e = {}) {
196
+ function V(e = {}) {
165
197
  let t = e.useSingleDollar ?? !1;
166
198
  return async (n) => {
167
199
  if (typeof window > "u") return;
168
200
  let a = i();
169
- a && r("mathjax-3-global", `window.MathJax is MathJax ${a}`), w(n);
170
- let o = F(e);
171
- if (!T(n, t)) {
201
+ a && r("mathjax-3-global", `window.MathJax is MathJax ${a}`), ee(n);
202
+ let o = B(e);
203
+ if (!te(n, t)) {
172
204
  o.catch(() => {});
173
205
  return;
174
206
  }
175
207
  await o;
176
- let s = v();
177
- await s?.startup?.promise, typeof s?.typesetPromise == "function" && (await s.typesetPromise([n]), _(n));
208
+ let s = S();
209
+ if (await s?.startup?.promise, typeof s?.typesetPromise == "function") {
210
+ I(s.startup?.document);
211
+ try {
212
+ await s.typesetPromise([n]);
213
+ } catch (e) {
214
+ throw s.typesetClear?.([n]), e;
215
+ }
216
+ x(n);
217
+ }
178
218
  };
179
219
  }
180
- function L() {
220
+ function H() {
181
221
  return [];
182
222
  }
183
223
  //#endregion
184
224
  //#region src/render-math.ts
185
- var R = "@pie-lib/math-rendering", z = "@pie-lib/math-rendering@2", B = "pie-print", V = 50, H = 1e4, U = null;
186
- function W() {
187
- return !!window[z]?.opts?.useSingleDollar;
225
+ var U = "@pie-lib/math-rendering", W = "@pie-lib/math-rendering@2", G = "pie-print", K = 50, q = 1e4, J = null;
226
+ function Y() {
227
+ return !!window[W]?.opts?.useSingleDollar;
188
228
  }
189
- function G() {
190
- return U ||= I({ useSingleDollar: W() }), U;
229
+ function X() {
230
+ return J ||= V({ useSingleDollar: Y() }), J;
191
231
  }
192
- function K() {
232
+ function Z() {
193
233
  if (typeof window > "u") return null;
194
- let e = window[R];
195
- return typeof e?.renderMath == "function" && e.renderMath !== X ? e : null;
234
+ let e = window[U];
235
+ return typeof e?.renderMath == "function" && e.renderMath !== $ ? e : null;
196
236
  }
197
- function q(e) {
237
+ function ne(e) {
198
238
  let t = e;
199
239
  for (; t;) {
200
- if (t instanceof Element && t.localName === B) return t;
240
+ if (t instanceof Element && t.localName === G) return t;
201
241
  t = t instanceof ShadowRoot ? t.host : t.parentNode;
202
242
  }
203
243
  return null;
204
244
  }
205
- function J() {
245
+ function Q() {
206
246
  let e = window.renderMath;
207
247
  return typeof e == "function" ? e : null;
208
248
  }
209
- async function Y(e) {
210
- let t = q(e);
249
+ async function re(e) {
250
+ let t = ne(e);
211
251
  if (!t) return null;
212
- let n = J();
252
+ let n = Q();
213
253
  if (n || t.mathRenderingModuleUrlImported !== !0) return n;
214
- let r = Date.now() + H;
254
+ let r = Date.now() + q;
215
255
  for (; Date.now() < r;) {
216
- await new Promise((e) => setTimeout(e, V));
217
- let e = J();
256
+ await new Promise((e) => setTimeout(e, K));
257
+ let e = Q();
218
258
  if (e) return e;
219
259
  }
220
260
  return null;
221
261
  }
222
- var X = async (e) => {
262
+ var $ = async (e) => {
223
263
  if (typeof window > "u") return;
224
264
  let t = typeof e == "string", n;
225
265
  t ? (n = document.createElement("div"), n.innerHTML = e) : n = e || document.body;
226
- let r = K(), i = r || t ? null : await Y(n);
227
- return r ? await r.renderMath?.(n) : i && i !== X ? await i(n) : await G()(n), t ? n.innerHTML : void 0;
228
- }, Z = (e, t) => {
229
- let n = K()?.wrapMath?.(e, t);
266
+ let r = Z(), i = r || t ? null : await re(n);
267
+ return r ? await r.renderMath?.(n) : i && i !== $ ? await i(n) : await X()(n), t ? n.innerHTML : void 0;
268
+ }, ie = (e, t) => {
269
+ let n = Z()?.wrapMath?.(e, t);
230
270
  if (n != null) return n;
231
271
  let [r, i] = t === "dollar" || t === "double_dollar" ? ["$", "$"] : ["\\(", "\\)"];
232
- return `${r}${y(e)}${i}`;
233
- }, Q = (e) => {
272
+ return `${r}${C(e)}${i}`;
273
+ }, ae = (e) => {
234
274
  let t = e.trim();
235
275
  return t.startsWith("\\[") && t.endsWith("\\]") ? {
236
276
  unwrapped: t.slice(2, -2).trim(),
@@ -257,6 +297,6 @@ var X = async (e) => {
257
297
  close: "$"
258
298
  }
259
299
  } : { unwrapped: t };
260
- }, $ = (e) => K()?.mmlToLatex?.(e) ?? e;
300
+ }, oe = (e) => Z()?.mmlToLatex?.(e) ?? e;
261
301
  //#endregion
262
- export { t as MATHJAX_CONFLICT_EVENT, e as UNSUPPORTED_PAGE_DOCS_URL, I as createMathjaxRenderer, L as getMathjaxCssUrls, $ as mmlToLatex, X as renderMath, Q as unWrapMath, Z as wrapMath };
302
+ export { t as MATHJAX_CONFLICT_EVENT, e as UNSUPPORTED_PAGE_DOCS_URL, V as createMathjaxRenderer, H as getMathjaxCssUrls, oe as mmlToLatex, $ as renderMath, ae as unWrapMath, ie as wrapMath };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-element/shared-math-rendering-mathjax",
3
- "version": "0.1.1",
3
+ "version": "0.1.2-next.20261002222413",
4
4
  "description": "MathJax adapter for PIE math rendering",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",