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

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 +90 -73
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -34,38 +34,38 @@ 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 = [m, "MJX-SVG-styles"], g = 200, _ = 201;
38
+ function v(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 y() {
43
43
  return window.MathJax;
44
44
  }
45
- function y(e) {
45
+ function b(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 x(e) {
50
+ return b(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 S = "mathjax_ignore", C = "mathjax_process";
53
+ function w(e, t) {
54
+ let n = e.closest(`.${S}, .${C}`);
55
+ return n !== null && t.contains(n) && !n.classList.contains(C);
56
56
  }
57
- function w(e) {
57
+ function T(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") || w(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 = `\\(${x(n)}\\)`.replace(u, "\\newline "), t.dataset.mathHandled = "true");
62
62
  }
63
63
  }
64
- function T(e, t) {
64
+ function E(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 D(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: {
@@ -75,8 +75,8 @@ function E(e) {
75
75
  startup: {
76
76
  typeset: !1,
77
77
  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);
78
+ let e = y(), r = e?.startup, i = !!e?.version?.startsWith("4.");
79
+ i && O(e), r?.defaultReady?.(), i && k(r?.document), Promise.resolve(r?.promise).then(() => t(), n);
80
80
  }
81
81
  },
82
82
  tex: { macros: { ...o } },
@@ -94,143 +94,160 @@ function E(e) {
94
94
  d.src = l || a, d.async = !0, d.onerror = () => n(/* @__PURE__ */ Error("Failed to load MathJax")), document.head.appendChild(d);
95
95
  });
96
96
  }
97
- function D(e) {
97
+ function O(e) {
98
98
  let t = e?._?.output?.chtml_ts?.CHTML;
99
99
  t && (t.STYLESHEETID = p);
100
100
  }
101
- function O(e) {
101
+ function k(e) {
102
102
  if (!e) return;
103
103
  let t = /* @__PURE__ */ new WeakSet(), n = (e) => {
104
104
  let n = e.typesetRoot;
105
- !n || t.has(n) || (_(n), t.add(n));
105
+ !n || t.has(n) || (v(n), t.add(n));
106
106
  };
107
- e.addRenderAction?.("pie-strip-latex", g, (e) => {
107
+ e.addRenderAction?.("pie-strip-latex", _, (e) => {
108
108
  for (let t of e.math ?? []) n(t);
109
109
  return !1;
110
110
  }, (e) => {
111
111
  let t = e.typesetRoot;
112
- return t && _(t), !1;
112
+ return t && v(t), !1;
113
113
  });
114
114
  let r = e.outputJax?.chtmlStyles;
115
- r?.id === m && (r.id = p), j();
115
+ r?.id === m && (r.id = p), M();
116
116
  }
117
- function k(e) {
117
+ function A(e) {
118
118
  return e instanceof HTMLStyleElement && h.includes(e.id);
119
119
  }
120
- function A(e) {
120
+ function j(e) {
121
121
  r("foreign-output-stylesheet", `another MathJax added <style id="${e.id}"> to <head>`);
122
122
  }
123
- function j() {
124
- let e = document.head, t = [...e.children].find(k);
123
+ function M() {
124
+ let e = document.head, t = [...e.children].find(A);
125
125
  if (t) {
126
- A(t);
126
+ j(t);
127
127
  return;
128
128
  }
129
129
  let n = new MutationObserver((e) => {
130
130
  for (let t of e) {
131
- let e = [...t.addedNodes].find(k);
131
+ let e = [...t.addedNodes].find(A);
132
132
  if (e) {
133
- A(e), n.disconnect();
133
+ j(e), n.disconnect();
134
134
  return;
135
135
  }
136
136
  }
137
137
  });
138
138
  n.observe(e, { childList: !0 });
139
139
  }
140
- function M(e) {
140
+ function N(e) {
141
+ let t = e?.math;
142
+ if (!t?.remove) return;
143
+ let n = [...t].filter((e) => (e.state?.() ?? 0) >= g && e.typesetRoot?.isConnected === !1);
144
+ if (n.length !== 0) {
145
+ for (let e of [...n].reverse()) e.clear?.();
146
+ t.remove(...n);
147
+ }
148
+ }
149
+ function P(e) {
141
150
  return new Promise((t, n) => {
142
151
  let r = e.startup ?? {};
143
152
  e.startup = r;
144
153
  let i = r.ready;
145
154
  r.ready = () => {
146
- let e = v()?.startup;
155
+ let e = y()?.startup;
147
156
  i ? i.call(r) : e?.defaultReady?.(), Promise.resolve(e?.promise).then(() => t(), n);
148
157
  };
149
158
  });
150
159
  }
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);
160
+ function F(e) {
161
+ let t = y();
162
+ return t ? t.version ? t.startup?.promise ? Promise.resolve(t.startup.promise).then(() => void 0) : Promise.resolve() : P(t) : D(e);
154
163
  }
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.");
164
+ function I(e) {
165
+ return F(e).then(() => {
166
+ typeof y()?.typesetPromise != "function" && console.warn("[mathjax-renderer] MathJax on this page has no typesetPromise; math stays untypeset.");
158
167
  });
159
168
  }
160
- function F(e) {
169
+ function L(e) {
161
170
  let t = globalThis, n = t[f];
162
- return n || (n = P(e), t[f] = n), n;
171
+ return n || (n = I(e), t[f] = n), n;
163
172
  }
164
- function I(e = {}) {
173
+ function R(e = {}) {
165
174
  let t = e.useSingleDollar ?? !1;
166
175
  return async (n) => {
167
176
  if (typeof window > "u") return;
168
177
  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)) {
178
+ a && r("mathjax-3-global", `window.MathJax is MathJax ${a}`), T(n);
179
+ let o = L(e);
180
+ if (!E(n, t)) {
172
181
  o.catch(() => {});
173
182
  return;
174
183
  }
175
184
  await o;
176
- let s = v();
177
- await s?.startup?.promise, typeof s?.typesetPromise == "function" && (await s.typesetPromise([n]), _(n));
185
+ let s = y();
186
+ if (await s?.startup?.promise, typeof s?.typesetPromise == "function") {
187
+ N(s.startup?.document);
188
+ try {
189
+ await s.typesetPromise([n]);
190
+ } catch (e) {
191
+ throw s.typesetClear?.([n]), e;
192
+ }
193
+ v(n);
194
+ }
178
195
  };
179
196
  }
180
- function L() {
197
+ function z() {
181
198
  return [];
182
199
  }
183
200
  //#endregion
184
201
  //#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;
202
+ var B = "@pie-lib/math-rendering", V = "@pie-lib/math-rendering@2", H = "pie-print", U = 50, W = 1e4, G = null;
203
+ function K() {
204
+ return !!window[V]?.opts?.useSingleDollar;
188
205
  }
189
- function G() {
190
- return U ||= I({ useSingleDollar: W() }), U;
206
+ function q() {
207
+ return G ||= R({ useSingleDollar: K() }), G;
191
208
  }
192
- function K() {
209
+ function J() {
193
210
  if (typeof window > "u") return null;
194
- let e = window[R];
195
- return typeof e?.renderMath == "function" && e.renderMath !== X ? e : null;
211
+ let e = window[B];
212
+ return typeof e?.renderMath == "function" && e.renderMath !== Q ? e : null;
196
213
  }
197
- function q(e) {
214
+ function Y(e) {
198
215
  let t = e;
199
216
  for (; t;) {
200
- if (t instanceof Element && t.localName === B) return t;
217
+ if (t instanceof Element && t.localName === H) return t;
201
218
  t = t instanceof ShadowRoot ? t.host : t.parentNode;
202
219
  }
203
220
  return null;
204
221
  }
205
- function J() {
222
+ function X() {
206
223
  let e = window.renderMath;
207
224
  return typeof e == "function" ? e : null;
208
225
  }
209
- async function Y(e) {
210
- let t = q(e);
226
+ async function Z(e) {
227
+ let t = Y(e);
211
228
  if (!t) return null;
212
- let n = J();
229
+ let n = X();
213
230
  if (n || t.mathRenderingModuleUrlImported !== !0) return n;
214
- let r = Date.now() + H;
231
+ let r = Date.now() + W;
215
232
  for (; Date.now() < r;) {
216
- await new Promise((e) => setTimeout(e, V));
217
- let e = J();
233
+ await new Promise((e) => setTimeout(e, U));
234
+ let e = X();
218
235
  if (e) return e;
219
236
  }
220
237
  return null;
221
238
  }
222
- var X = async (e) => {
239
+ var Q = async (e) => {
223
240
  if (typeof window > "u") return;
224
241
  let t = typeof e == "string", n;
225
242
  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);
243
+ let r = J(), i = r || t ? null : await Z(n);
244
+ return r ? await r.renderMath?.(n) : i && i !== Q ? await i(n) : await q()(n), t ? n.innerHTML : void 0;
245
+ }, $ = (e, t) => {
246
+ let n = J()?.wrapMath?.(e, t);
230
247
  if (n != null) return n;
231
248
  let [r, i] = t === "dollar" || t === "double_dollar" ? ["$", "$"] : ["\\(", "\\)"];
232
- return `${r}${y(e)}${i}`;
233
- }, Q = (e) => {
249
+ return `${r}${b(e)}${i}`;
250
+ }, ee = (e) => {
234
251
  let t = e.trim();
235
252
  return t.startsWith("\\[") && t.endsWith("\\]") ? {
236
253
  unwrapped: t.slice(2, -2).trim(),
@@ -257,6 +274,6 @@ var X = async (e) => {
257
274
  close: "$"
258
275
  }
259
276
  } : { unwrapped: t };
260
- }, $ = (e) => K()?.mmlToLatex?.(e) ?? e;
277
+ }, te = (e) => J()?.mmlToLatex?.(e) ?? e;
261
278
  //#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 };
279
+ export { t as MATHJAX_CONFLICT_EVENT, e as UNSUPPORTED_PAGE_DOCS_URL, R as createMathjaxRenderer, z as getMathjaxCssUrls, te as mmlToLatex, Q as renderMath, ee as unWrapMath, $ 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-next.8",
3
+ "version": "0.1.2-next.20261002215153",
4
4
  "description": "MathJax adapter for PIE math rendering",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",