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

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/adapter.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import type { MathjaxOptions } from './types.js';
2
+ /** `latex` without the pair of legacy delimiters around all of it, if it has one. */
3
+ export declare function stripLegacyDelimiters(latex: string): string;
2
4
  /**
3
5
  * Create a MathJax renderer function.
4
6
  *
package/dist/index.js CHANGED
@@ -43,22 +43,29 @@ function v() {
43
43
  return window.MathJax;
44
44
  }
45
45
  function y(e) {
46
- let t = e.includes("\\displaystyle") ? e.replace("\\displaystyle", "").trim() : e;
47
- for (let [e, n] of d) if (t.startsWith(e) && t.endsWith(n)) return t.substring(e.length, t.length - n.length);
48
- return t;
46
+ for (let [t, n] of d) if (e.startsWith(t) && e.endsWith(n)) return e.substring(t.length, e.length - n.length);
47
+ return e;
49
48
  }
50
49
  function b(e) {
50
+ return y(e.includes("\\displaystyle") ? e.replace("\\displaystyle", "").trim() : e);
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);
56
+ }
57
+ function w(e) {
51
58
  for (let t of e.querySelectorAll("[data-latex]")) {
52
- if (t.dataset.mathHandled || t.closest("mjx-container")) continue;
53
- let e = t.textContent;
54
- e && (t.textContent = `\\(${y(e)}\\)`.replace(u, "\\newline "), t.dataset.mathHandled = "true");
59
+ if (t.dataset.mathHandled || t.closest("mjx-container") || C(t, e)) continue;
60
+ let n = t.textContent;
61
+ n && (t.textContent = `\\(${b(n)}\\)`.replace(u, "\\newline "), t.dataset.mathHandled = "true");
55
62
  }
56
63
  }
57
- function x(e, t) {
64
+ function T(e, t) {
58
65
  for (let t of e.querySelectorAll("math")) if (!t.closest("mjx-assistive-mml")) return !0;
59
66
  return (t ? l : c).test(e.textContent ?? "");
60
67
  }
61
- function S(e) {
68
+ function E(e) {
62
69
  return new Promise((t, n) => {
63
70
  let { useSingleDollar: r = !1, accessibility: i = !0, loadFonts: c = !0, srcUrl: l } = e, u = {
64
71
  loader: {
@@ -69,7 +76,7 @@ function S(e) {
69
76
  typeset: !1,
70
77
  ready: () => {
71
78
  let e = v(), r = e?.startup, i = !!e?.version?.startsWith("4.");
72
- i && C(e), r?.defaultReady?.(), i && w(r?.document), Promise.resolve(r?.promise).then(() => t(), n);
79
+ i && D(e), r?.defaultReady?.(), i && O(r?.document), Promise.resolve(r?.promise).then(() => t(), n);
73
80
  }
74
81
  },
75
82
  tex: { macros: { ...o } },
@@ -87,11 +94,11 @@ function S(e) {
87
94
  d.src = l || a, d.async = !0, d.onerror = () => n(/* @__PURE__ */ Error("Failed to load MathJax")), document.head.appendChild(d);
88
95
  });
89
96
  }
90
- function C(e) {
97
+ function D(e) {
91
98
  let t = e?._?.output?.chtml_ts?.CHTML;
92
99
  t && (t.STYLESHEETID = p);
93
100
  }
94
- function w(e) {
101
+ function O(e) {
95
102
  if (!e) return;
96
103
  let t = /* @__PURE__ */ new WeakSet(), n = (e) => {
97
104
  let n = e.typesetRoot;
@@ -105,32 +112,32 @@ function w(e) {
105
112
  return t && _(t), !1;
106
113
  });
107
114
  let r = e.outputJax?.chtmlStyles;
108
- r?.id === m && (r.id = p), D();
115
+ r?.id === m && (r.id = p), j();
109
116
  }
110
- function T(e) {
117
+ function k(e) {
111
118
  return e instanceof HTMLStyleElement && h.includes(e.id);
112
119
  }
113
- function E(e) {
120
+ function A(e) {
114
121
  r("foreign-output-stylesheet", `another MathJax added <style id="${e.id}"> to <head>`);
115
122
  }
116
- function D() {
117
- let e = document.head, t = [...e.children].find(T);
123
+ function j() {
124
+ let e = document.head, t = [...e.children].find(k);
118
125
  if (t) {
119
- E(t);
126
+ A(t);
120
127
  return;
121
128
  }
122
129
  let n = new MutationObserver((e) => {
123
130
  for (let t of e) {
124
- let e = [...t.addedNodes].find(T);
131
+ let e = [...t.addedNodes].find(k);
125
132
  if (e) {
126
- E(e), n.disconnect();
133
+ A(e), n.disconnect();
127
134
  return;
128
135
  }
129
136
  }
130
137
  });
131
138
  n.observe(e, { childList: !0 });
132
139
  }
133
- function O(e) {
140
+ function M(e) {
134
141
  return new Promise((t, n) => {
135
142
  let r = e.startup ?? {};
136
143
  e.startup = r;
@@ -141,27 +148,27 @@ function O(e) {
141
148
  };
142
149
  });
143
150
  }
144
- function k(e) {
151
+ function N(e) {
145
152
  let t = v();
146
- return t ? t.version ? t.startup?.promise ? Promise.resolve(t.startup.promise).then(() => void 0) : Promise.resolve() : O(t) : S(e);
153
+ return t ? t.version ? t.startup?.promise ? Promise.resolve(t.startup.promise).then(() => void 0) : Promise.resolve() : M(t) : E(e);
147
154
  }
148
- function A(e) {
149
- return k(e).then(() => {
155
+ function P(e) {
156
+ return N(e).then(() => {
150
157
  typeof v()?.typesetPromise != "function" && console.warn("[mathjax-renderer] MathJax on this page has no typesetPromise; math stays untypeset.");
151
158
  });
152
159
  }
153
- function j(e) {
160
+ function F(e) {
154
161
  let t = globalThis, n = t[f];
155
- return n || (n = A(e), t[f] = n), n;
162
+ return n || (n = P(e), t[f] = n), n;
156
163
  }
157
- function M(e = {}) {
164
+ function I(e = {}) {
158
165
  let t = e.useSingleDollar ?? !1;
159
166
  return async (n) => {
160
167
  if (typeof window > "u") return;
161
168
  let a = i();
162
- a && r("mathjax-3-global", `window.MathJax is MathJax ${a}`), b(n);
163
- let o = j(e);
164
- if (!x(n, t)) {
169
+ a && r("mathjax-3-global", `window.MathJax is MathJax ${a}`), w(n);
170
+ let o = F(e);
171
+ if (!T(n, t)) {
165
172
  o.catch(() => {});
166
173
  return;
167
174
  }
@@ -170,55 +177,60 @@ function M(e = {}) {
170
177
  await s?.startup?.promise, typeof s?.typesetPromise == "function" && (await s.typesetPromise([n]), _(n));
171
178
  };
172
179
  }
173
- function N() {
180
+ function L() {
174
181
  return [];
175
182
  }
176
183
  //#endregion
177
184
  //#region src/render-math.ts
178
- var P = "@pie-lib/math-rendering", F = "@pie-lib/math-rendering@2", I = "pie-print", L = 50, R = 1e4, z = null;
179
- function B() {
180
- return !!window[F]?.opts?.useSingleDollar;
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;
181
188
  }
182
- function V() {
183
- return z ||= M({ useSingleDollar: B() }), z;
189
+ function G() {
190
+ return U ||= I({ useSingleDollar: W() }), U;
184
191
  }
185
- function H() {
192
+ function K() {
186
193
  if (typeof window > "u") return null;
187
- let e = window[P];
188
- return typeof e?.renderMath == "function" ? e : null;
194
+ let e = window[R];
195
+ return typeof e?.renderMath == "function" && e.renderMath !== X ? e : null;
189
196
  }
190
- function U(e) {
197
+ function q(e) {
191
198
  let t = e;
192
199
  for (; t;) {
193
- if (t instanceof Element && t.localName === I) return t;
200
+ if (t instanceof Element && t.localName === B) return t;
194
201
  t = t instanceof ShadowRoot ? t.host : t.parentNode;
195
202
  }
196
203
  return null;
197
204
  }
198
- function W() {
205
+ function J() {
199
206
  let e = window.renderMath;
200
207
  return typeof e == "function" ? e : null;
201
208
  }
202
- async function G(e) {
203
- let t = U(e);
209
+ async function Y(e) {
210
+ let t = q(e);
204
211
  if (!t) return null;
205
- let n = W();
212
+ let n = J();
206
213
  if (n || t.mathRenderingModuleUrlImported !== !0) return n;
207
- let r = Date.now() + R;
214
+ let r = Date.now() + H;
208
215
  for (; Date.now() < r;) {
209
- await new Promise((e) => setTimeout(e, L));
210
- let e = W();
216
+ await new Promise((e) => setTimeout(e, V));
217
+ let e = J();
211
218
  if (e) return e;
212
219
  }
213
220
  return null;
214
221
  }
215
- var K = async (e) => {
222
+ var X = async (e) => {
216
223
  if (typeof window > "u") return;
217
224
  let t = typeof e == "string", n;
218
225
  t ? (n = document.createElement("div"), n.innerHTML = e) : n = e || document.body;
219
- let r = H(), i = r || t ? null : await G(n);
220
- return r ? await r.renderMath?.(n) : i ? await i(n) : await V()(n), t ? n.innerHTML : void 0;
221
- }, q = (e) => H()?.wrapMath?.(e) ?? e, J = (e) => {
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);
230
+ if (n != null) return n;
231
+ let [r, i] = t === "dollar" || t === "double_dollar" ? ["$", "$"] : ["\\(", "\\)"];
232
+ return `${r}${y(e)}${i}`;
233
+ }, Q = (e) => {
222
234
  let t = e.trim();
223
235
  return t.startsWith("\\[") && t.endsWith("\\]") ? {
224
236
  unwrapped: t.slice(2, -2).trim(),
@@ -245,6 +257,6 @@ var K = async (e) => {
245
257
  close: "$"
246
258
  }
247
259
  } : { unwrapped: t };
248
- }, Y = (e) => H()?.mmlToLatex?.(e) ?? e;
260
+ }, $ = (e) => K()?.mmlToLatex?.(e) ?? e;
249
261
  //#endregion
250
- export { t as MATHJAX_CONFLICT_EVENT, e as UNSUPPORTED_PAGE_DOCS_URL, M as createMathjaxRenderer, N as getMathjaxCssUrls, Y as mmlToLatex, K as renderMath, J as unWrapMath, q as wrapMath };
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 };
@@ -9,9 +9,14 @@
9
9
  */
10
10
  export declare const renderMath: (el?: Element | string) => Promise<string | undefined>;
11
11
  /**
12
- * Wrap LaTeX - MathJax handles delimiters, so just pass through
12
+ * Wraps LaTeX in inline delimiters, through the page renderer's `wrapMath` when it has one. The
13
+ * fallback wraps as `@pie-lib/math-rendering` does, `$…$` for its `dollar` and `double_dollar`
14
+ * wrap types and `\(…\)` for any other, so a math node saves the same markup whichever renderer
15
+ * the authoring page installed. LaTeX that already has delimiters keeps one pair, where the legacy
16
+ * function adds a second: a math span saved without `data-raw` reaches the editor with its
17
+ * delimiters.
13
18
  */
14
- export declare const wrapMath: (latex: string) => string;
19
+ export declare const wrapMath: (latex: string, wrapType?: string | null) => string;
15
20
  /**
16
21
  * Unwrap LaTeX delimiters - minimal implementation for editable-html-tip-tap
17
22
  */
package/dist/types.d.ts CHANGED
@@ -29,7 +29,7 @@ export interface MathjaxOptions {
29
29
  export type MathRenderer = (element: HTMLElement) => void | Promise<void>;
30
30
  export interface MathRenderingAPI {
31
31
  renderMath: MathRenderer;
32
- wrapMath?: (latex: string) => string;
32
+ wrapMath?: (latex: string, wrapType?: string | null) => string;
33
33
  unWrapMath?: (wrapped: string) => string;
34
34
  mmlToLatex?: (mathml: string) => string;
35
35
  }
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.7",
3
+ "version": "0.1.1",
4
4
  "description": "MathJax adapter for PIE math rendering",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",