@pie-element/shared-math-rendering-mathjax 0.1.2-next.20261002222413 → 0.1.2-next.20261003064518
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/elementary-math.d.ts +15 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +492 -109
- package/dist/mathml.d.ts +8 -0
- package/package.json +1 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MathJax typesets the MathML elementary-math elements only through its experimental `mml3`
|
|
3
|
+
* extension, an XSLT transform that depends on the browser's `XSLTProcessor`, which Chrome is
|
|
4
|
+
* removing. Without it, `mstack` and `mlongdiv` are a "Math input error". This module rewrites both
|
|
5
|
+
* into the `mtable` they describe before MathJax reads them: one column per digit, lines as cell
|
|
6
|
+
* borders, and carries as zero-width scripts on the digit they annotate.
|
|
7
|
+
*
|
|
8
|
+
* Layout follows MathML 4 §3.6. A column is numbered from the stack's alignment point, so cell `i` of
|
|
9
|
+
* a row sits in column `i - anchor - position`, where `anchor` counts the row's cells left of that
|
|
10
|
+
* point and a positive `position` moves the row towards the tens digit.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Rewrites each `mstack` and `mlongdiv` in `root` as an `mtable`, leaving MathJax's output alone.
|
|
14
|
+
*/
|
|
15
|
+
export declare function rewriteElementaryMath(root: Element): void;
|
package/dist/index.d.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* MathJax adapter for PIE math rendering (~2.7MB)
|
|
5
5
|
* Full-featured LaTeX and MathML rendering with accessibility support.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* It also implements the legacy @pie-lib/math-rendering API (renderMath, wrapMath,
|
|
8
|
+
* unWrapMath, mmlToLatex) on MathJax 4; @pie-lib/math-rendering re-exports it.
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
11
|
* ```typescript
|
package/dist/index.js
CHANGED
|
@@ -1,89 +1,472 @@
|
|
|
1
|
+
//#region src/mathml.ts
|
|
2
|
+
var e = "http://www.w3.org/1998/Math/MathML", t = /^([a-z_][\w.-]*):math$/, n = /^[a-z][a-z0-9-]*$/;
|
|
3
|
+
function r(e) {
|
|
4
|
+
for (let n of e.querySelectorAll("*")) {
|
|
5
|
+
let r = t.exec(n.tagName.toLowerCase())?.[1];
|
|
6
|
+
!r || !e.contains(n) || n.closest("mjx-container") || n.replaceWith(i(n, `${r}:`));
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function i(t, r) {
|
|
10
|
+
let a = t.tagName.toLowerCase().slice(r.length);
|
|
11
|
+
if (!n.test(a)) return t;
|
|
12
|
+
let o = t.ownerDocument.createElementNS(e, a);
|
|
13
|
+
for (let e of t.attributes) o.setAttribute(e.name, e.value);
|
|
14
|
+
for (let e of [...t.childNodes]) {
|
|
15
|
+
let t = e instanceof Element && e.tagName.toLowerCase().startsWith(r);
|
|
16
|
+
o.appendChild(t ? i(e, r) : e);
|
|
17
|
+
}
|
|
18
|
+
return o;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/elementary-math.ts
|
|
22
|
+
var a = /* @__PURE__ */ new Set([
|
|
23
|
+
"updiagonalstrike",
|
|
24
|
+
"downdiagonalstrike",
|
|
25
|
+
"verticalstrike",
|
|
26
|
+
"horizontalstrike"
|
|
27
|
+
]), o = {
|
|
28
|
+
thin: "0.035em",
|
|
29
|
+
medium: "0.07em",
|
|
30
|
+
thick: "0.14em"
|
|
31
|
+
}, s = {
|
|
32
|
+
tight: "0em",
|
|
33
|
+
medium: "0.1em",
|
|
34
|
+
loose: "0.2em"
|
|
35
|
+
}, c = /^(\d+(\.\d*)?|\.\d+)(em|ex|px|pt|pc|in|cm|mm)$/, l = /^(#[0-9a-f]{3,8}|[a-z]+)$/i, u = /^(\d+(\.\d*)?|\.\d+)$/, d = /* @__PURE__ */ new Set([
|
|
36
|
+
"mn",
|
|
37
|
+
"mo",
|
|
38
|
+
"mi",
|
|
39
|
+
"mtext"
|
|
40
|
+
]), f = /* @__PURE__ */ new Set([
|
|
41
|
+
"stackalign",
|
|
42
|
+
"charalign",
|
|
43
|
+
"charspacing",
|
|
44
|
+
"longdivstyle"
|
|
45
|
+
]);
|
|
46
|
+
function p(t, n, r = [], i = {}) {
|
|
47
|
+
let a = t.createElementNS(e, n);
|
|
48
|
+
for (let [e, t] of Object.entries(i)) a.setAttribute(e, t);
|
|
49
|
+
for (let e of r) e && a.appendChild(e);
|
|
50
|
+
return a;
|
|
51
|
+
}
|
|
52
|
+
function m(e, t) {
|
|
53
|
+
let n = p(e.document, "mo");
|
|
54
|
+
return n.textContent = t, n;
|
|
55
|
+
}
|
|
56
|
+
function h(e) {
|
|
57
|
+
let t = p(e.document, "mn");
|
|
58
|
+
return t.textContent = "0", p(e.document, "mphantom", [t]);
|
|
59
|
+
}
|
|
60
|
+
function g(e, t) {
|
|
61
|
+
return e.getAttribute(t)?.trim().toLowerCase() || void 0;
|
|
62
|
+
}
|
|
63
|
+
function _(e, t) {
|
|
64
|
+
let n = Number.parseInt(e.getAttribute(t) ?? "", 10);
|
|
65
|
+
return Number.isFinite(n) ? n : 0;
|
|
66
|
+
}
|
|
67
|
+
function v(e) {
|
|
68
|
+
return e.localName === "none" || e.localName === "mrow" && e.children.length === 0 && !e.textContent?.trim();
|
|
69
|
+
}
|
|
70
|
+
function y(e, t, n = e) {
|
|
71
|
+
switch (t) {
|
|
72
|
+
case "left": return 0;
|
|
73
|
+
case "center": return Math.floor(e / 2);
|
|
74
|
+
case "right": return e;
|
|
75
|
+
default: return n;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function b(e, t, n) {
|
|
79
|
+
let r = [], i, a, o = (e, t) => {
|
|
80
|
+
let s = (e) => t.reduceRight((e, t) => {
|
|
81
|
+
let n = t.cloneNode(!1);
|
|
82
|
+
return n.appendChild(e), n;
|
|
83
|
+
}, e);
|
|
84
|
+
if (e.localName === "mstyle") for (let n of [...e.children]) o(n, [...t, e]);
|
|
85
|
+
else if (v(e)) r.push(null);
|
|
86
|
+
else if (e.localName === "mn" && e.children.length === 0) {
|
|
87
|
+
let t = [...(e.textContent ?? "").trim()];
|
|
88
|
+
for (let a of t) {
|
|
89
|
+
a === n.decimalPoint && (i ??= r.length);
|
|
90
|
+
let t = e.cloneNode(!1);
|
|
91
|
+
t.textContent = a, r.push(s(t));
|
|
92
|
+
}
|
|
93
|
+
t.length && (a ??= r.length);
|
|
94
|
+
} else d.has(e.localName) && e.textContent?.trim() === n.decimalPoint && (i ??= r.length), r.push(s(e));
|
|
95
|
+
};
|
|
96
|
+
for (let t of e) o(t, []);
|
|
97
|
+
return {
|
|
98
|
+
kind: "digits",
|
|
99
|
+
cells: r,
|
|
100
|
+
anchor: y(r.length, n.stackAlign, i ?? a),
|
|
101
|
+
position: t
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function x(e) {
|
|
105
|
+
return (e ?? "").toLowerCase().split(/\s+/).filter((e) => a.has(e));
|
|
106
|
+
}
|
|
107
|
+
function S(e, t) {
|
|
108
|
+
let n = [...e.children].filter((e) => !v(e));
|
|
109
|
+
return n.length <= 1 ? n[0] ?? null : p(t.document, "mrow", n);
|
|
110
|
+
}
|
|
111
|
+
function ee(e, t, n) {
|
|
112
|
+
let r = g(e, "location") ?? "n", i = x(e.getAttribute("crossout")), a = [...e.children].map((e) => v(e) ? null : e.localName === "mscarry" ? {
|
|
113
|
+
content: S(e, n),
|
|
114
|
+
location: g(e, "location") ?? r,
|
|
115
|
+
crossout: e.hasAttribute("crossout") ? x(e.getAttribute("crossout")) : i
|
|
116
|
+
} : {
|
|
117
|
+
content: e,
|
|
118
|
+
location: r,
|
|
119
|
+
crossout: i
|
|
120
|
+
}), o = e.getAttribute("scriptsizemultiplier")?.trim() ?? "";
|
|
121
|
+
return {
|
|
122
|
+
kind: "carries",
|
|
123
|
+
carries: a,
|
|
124
|
+
anchor: y(a.length, n.stackAlign),
|
|
125
|
+
position: t,
|
|
126
|
+
scale: u.test(o) ? o : "0.6"
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function C(e) {
|
|
130
|
+
let t = g(e, "mslinethickness") ?? "medium", n = o[t] ?? (c.test(t) ? t : "0.07em"), r = e.getAttribute("mathcolor")?.trim() ?? "";
|
|
131
|
+
return `${n} solid ${l.test(r) ? r : "currentColor"}`;
|
|
132
|
+
}
|
|
133
|
+
function te(e, t, n) {
|
|
134
|
+
let r = Math.max(0, _(e, "length"));
|
|
135
|
+
return {
|
|
136
|
+
kind: "line",
|
|
137
|
+
length: r,
|
|
138
|
+
anchor: y(r, n.stackAlign),
|
|
139
|
+
position: t,
|
|
140
|
+
border: C(e)
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function w(e, t, n, r, i) {
|
|
144
|
+
e.forEach((e, a) => {
|
|
145
|
+
let o = t + a * n + _(e, "position");
|
|
146
|
+
switch (e.localName) {
|
|
147
|
+
case "msgroup":
|
|
148
|
+
w([...e.children], o, _(e, "shift"), r, i);
|
|
149
|
+
break;
|
|
150
|
+
case "msline":
|
|
151
|
+
i.push(te(e, o, r));
|
|
152
|
+
break;
|
|
153
|
+
case "mscarries":
|
|
154
|
+
i.push(ee(e, o, r));
|
|
155
|
+
break;
|
|
156
|
+
case "msrow":
|
|
157
|
+
case "mrow":
|
|
158
|
+
i.push(b([...e.children], o, r));
|
|
159
|
+
break;
|
|
160
|
+
default: i.push(b([e], o, r));
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
function T(e) {
|
|
165
|
+
return -e.anchor - e.position;
|
|
166
|
+
}
|
|
167
|
+
function E(e) {
|
|
168
|
+
return e.kind === "digits" ? e.cells.length : e.kind === "carries" ? e.carries.length : e.length;
|
|
169
|
+
}
|
|
170
|
+
function D(e) {
|
|
171
|
+
return T(e) + E(e) - 1;
|
|
172
|
+
}
|
|
173
|
+
function ne(e, t, n, r) {
|
|
174
|
+
let i = (e) => p(r.document, "mpadded", [t], {
|
|
175
|
+
width: "0",
|
|
176
|
+
...e ? { lspace: e } : {}
|
|
177
|
+
}), a = (e, t) => p(r.document, e, t);
|
|
178
|
+
switch (n) {
|
|
179
|
+
case "s": return a("munder", [e, i("-0.5width")]);
|
|
180
|
+
case "ne": return a("msup", [e, i()]);
|
|
181
|
+
case "se": return a("msub", [e, i()]);
|
|
182
|
+
case "nw": return a("mmultiscripts", [
|
|
183
|
+
e,
|
|
184
|
+
a("mprescripts", []),
|
|
185
|
+
a("none", []),
|
|
186
|
+
i("-1width")
|
|
187
|
+
]);
|
|
188
|
+
case "sw": return a("mmultiscripts", [
|
|
189
|
+
e,
|
|
190
|
+
a("mprescripts", []),
|
|
191
|
+
i("-1width"),
|
|
192
|
+
a("none", [])
|
|
193
|
+
]);
|
|
194
|
+
case "w": return t.setAttribute("scriptlevel", "+1"), a("mrow", [i("-1width"), e]);
|
|
195
|
+
case "e": return t.setAttribute("scriptlevel", "+1"), a("mrow", [e, i()]);
|
|
196
|
+
default: return a("mover", [e, i("-0.5width")]);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function re(e, t, n) {
|
|
200
|
+
let r = p(n.document, "menclose", [e], {
|
|
201
|
+
notation: t.join(" "),
|
|
202
|
+
"data-padding": "0.1em",
|
|
203
|
+
"data-thickness": "0.067em"
|
|
204
|
+
}), i = t.some((e) => e.endsWith("diagonalstrike")) ? .167 : t.includes("horizontalstrike") ? .1 : 0;
|
|
205
|
+
return i ? p(n.document, "mpadded", [r], {
|
|
206
|
+
lspace: `-${i}em`,
|
|
207
|
+
width: `-${2 * i}em`
|
|
208
|
+
}) : r;
|
|
209
|
+
}
|
|
210
|
+
function ie(e, t, n, r, i) {
|
|
211
|
+
let a = e && t.crossout.length ? re(e, t.crossout, i) : e;
|
|
212
|
+
if (!t.content) return a;
|
|
213
|
+
let o = p(i.document, "mstyle", [t.content], { scriptsizemultiplier: r });
|
|
214
|
+
return ne(a ?? h(i), o, n, i);
|
|
215
|
+
}
|
|
216
|
+
function O(e, t) {
|
|
217
|
+
return e.carries[t - T(e)] ?? null;
|
|
218
|
+
}
|
|
219
|
+
function ae(e, t, n, r) {
|
|
220
|
+
let i = e.content;
|
|
221
|
+
n.forEach((e, a) => {
|
|
222
|
+
let o = n.length - 1 - a, s = n[o], c = O(s, t), l = n.slice(0, o).some((e) => O(e, t));
|
|
223
|
+
if (!c && !l) return;
|
|
224
|
+
let u = c ?? {
|
|
225
|
+
content: h(r),
|
|
226
|
+
location: "n",
|
|
227
|
+
crossout: []
|
|
228
|
+
}, d = a === 0 ? u.location : "n";
|
|
229
|
+
i = ie(i, u, d, s.scale, r);
|
|
230
|
+
}), e.content = i;
|
|
231
|
+
}
|
|
232
|
+
function k(e, t) {
|
|
233
|
+
let n = e.filter((e) => E(e) > 0), r = Math.min(0, ...n.map(T)), i = Math.max(r, ...n.map(D)) - r + 1, a = -r, o = [], s = /* @__PURE__ */ new Map(), c = () => Array.from({ length: i }, () => ({
|
|
234
|
+
content: null,
|
|
235
|
+
borders: {}
|
|
236
|
+
})), l = (e) => e.length === 0 ? Array.from({ length: i }, (e, t) => t) : Array.from({ length: e.length }, (t, n) => a + T(e) + n), u = [], d = [], f = (e) => {
|
|
237
|
+
let n = c();
|
|
238
|
+
e.cells.forEach((t, r) => {
|
|
239
|
+
n[a + T(e) + r].content = t;
|
|
240
|
+
}), u.length && n.forEach((e, n) => {
|
|
241
|
+
ae(e, n - a, u, t);
|
|
242
|
+
});
|
|
243
|
+
for (let e of d) for (let t of l(e)) n[t].borders.top = e.border;
|
|
244
|
+
u = [], d = [], s.set(e, o.length), o.push(n);
|
|
245
|
+
};
|
|
246
|
+
for (let t of e) t.kind === "digits" ? f(t) : t.kind === "carries" ? u.push(t) : d.push(t);
|
|
247
|
+
(u.length || !o.length) && f({
|
|
248
|
+
kind: "digits",
|
|
249
|
+
cells: [],
|
|
250
|
+
anchor: 0,
|
|
251
|
+
position: 0
|
|
252
|
+
});
|
|
253
|
+
let p = o[o.length - 1];
|
|
254
|
+
for (let e of d) for (let t of l(e)) p[t].borders.bottom = e.border;
|
|
255
|
+
return {
|
|
256
|
+
rows: o,
|
|
257
|
+
rowOf: s,
|
|
258
|
+
origin: a
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
function A(e, { rows: t }, n) {
|
|
262
|
+
let r = p(n.document, "mtable", t.map((e) => p(n.document, "mtr", e.map((e) => {
|
|
263
|
+
let t = Object.entries(e.borders).map(([e, t]) => `border-${e}:${t}`).join(";");
|
|
264
|
+
return p(n.document, "mtd", [e.content], {
|
|
265
|
+
...t ? { style: t } : {},
|
|
266
|
+
...e.align ? { columnalign: e.align } : {}
|
|
267
|
+
});
|
|
268
|
+
}))));
|
|
269
|
+
for (let { name: t, value: n } of [...e.attributes]) f.has(t) || r.setAttribute(t, n);
|
|
270
|
+
let i = g(e, "charspacing") ?? "medium", a = g(e, "charalign");
|
|
271
|
+
return r.setAttribute("columnspacing", s[i] ?? (c.test(i) ? i : s.medium)), r.setAttribute("columnalign", a === "left" || a === "center" ? a : "right"), r.setAttribute("rowspacing", "0"), r.setAttribute("align", e.getAttribute("align") ?? "baseline"), r;
|
|
272
|
+
}
|
|
273
|
+
function j(e) {
|
|
274
|
+
return {
|
|
275
|
+
document: e.ownerDocument,
|
|
276
|
+
stackAlign: g(e, "stackalign") ?? "decimalpoint",
|
|
277
|
+
decimalPoint: e.closest("[decimalpoint]")?.getAttribute("decimalpoint")?.trim().charAt(0) || "."
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
function oe(e) {
|
|
281
|
+
let t = j(e), n = [];
|
|
282
|
+
return w([...e.children], 0, 0, t, n), A(e, k(n, t), t);
|
|
283
|
+
}
|
|
284
|
+
function M(e, t, n, r, i, a) {
|
|
285
|
+
for (let o = n; o <= r; o++) e[t][o].borders[i] = a;
|
|
286
|
+
}
|
|
287
|
+
function se(e, t) {
|
|
288
|
+
let n = m(t, ")");
|
|
289
|
+
return n.setAttribute("minsize", "1.2em"), [p(t.document, "mrow", [e, p(t.document, "mspace", [], { width: "0.2em" })]), p(t.document, "mpadded", [n], {
|
|
290
|
+
voffset: "0.1em",
|
|
291
|
+
lspace: "-0.15em",
|
|
292
|
+
depth: "-0.2em",
|
|
293
|
+
height: "-0.2em"
|
|
294
|
+
})];
|
|
295
|
+
}
|
|
296
|
+
function ce(e) {
|
|
297
|
+
let t = j(e), [n, r, ...i] = [...e.children], a = n && !v(n) ? n : null, o = r && !v(r) ? r : null, s = g(e, "longdivstyle") ?? "lefttop", c = C(e), l = [];
|
|
298
|
+
w(i, 0, 0, t, l);
|
|
299
|
+
let u = l.find((e) => e.kind === "digits");
|
|
300
|
+
u || (u = {
|
|
301
|
+
kind: "digits",
|
|
302
|
+
cells: [],
|
|
303
|
+
anchor: 0,
|
|
304
|
+
position: 0
|
|
305
|
+
}, l.unshift(u));
|
|
306
|
+
let d = [], f = (...e) => {
|
|
307
|
+
u.cells.unshift(...e), u.anchor += e.length;
|
|
308
|
+
}, p = (...e) => {
|
|
309
|
+
let n = e.flatMap((e) => e ? e.localName === "mrow" ? [...e.children] : [e] : []);
|
|
310
|
+
u.cells.push(...b(n, 0, t).cells);
|
|
311
|
+
};
|
|
312
|
+
switch (s) {
|
|
313
|
+
case "left/\\right":
|
|
314
|
+
f(a, m(t, "/")), p(m(t, "\\"), o);
|
|
315
|
+
break;
|
|
316
|
+
case "left)(right":
|
|
317
|
+
f(a, m(t, ")")), p(m(t, "("), o);
|
|
318
|
+
break;
|
|
319
|
+
case ":right=right":
|
|
320
|
+
p(m(t, ":"), a, m(t, "="), o);
|
|
321
|
+
break;
|
|
322
|
+
case "righttop":
|
|
323
|
+
case "stackedleftlinetop":
|
|
324
|
+
case "lefttop":
|
|
325
|
+
o && w([o], 0, 0, t, d), l.splice(l.indexOf(u), 0, ...d), s === "righttop" ? p(a) : s === "stackedleftlinetop" ? f(a) : f(...se(a, t));
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
328
|
+
let h = k(l, t), _ = h.rows, y = h.rowOf.get(u) ?? 0, x = (e) => h.origin + T(u) + e, S = Math.max(x(u.cells.length - 1), ...d.map((e) => h.origin + D(e)));
|
|
329
|
+
switch (s) {
|
|
330
|
+
case "left/\\right":
|
|
331
|
+
case "left)(right":
|
|
332
|
+
case ":right=right": break;
|
|
333
|
+
case "righttop": {
|
|
334
|
+
let e = _[y][x(u.cells.length - 1)];
|
|
335
|
+
e.borders.left = c, e.borders.bottom = c, M(_, y, x(0), x(u.cells.length - 1), "top", c);
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
case "stackedleftlinetop": {
|
|
339
|
+
let e = _[y][x(0)];
|
|
340
|
+
e.borders.right = c, e.borders.bottom = c, M(_, y, x(1), S, "top", c);
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
case "stackedrightright":
|
|
344
|
+
case "mediumstackedrightright":
|
|
345
|
+
case "shortstackedrightright":
|
|
346
|
+
case "stackedleftleft":
|
|
347
|
+
le(_, y, s, a, o, c, t);
|
|
348
|
+
break;
|
|
349
|
+
default: M(_, y, x(1), S, "top", c);
|
|
350
|
+
}
|
|
351
|
+
return A(e, h, t);
|
|
352
|
+
}
|
|
353
|
+
function le(e, t, n, r, i, a, o) {
|
|
354
|
+
let s = n === "stackedleftleft", c = s ? "right" : "left", l = (e) => e && p(o.document, "mpadded", [e], {
|
|
355
|
+
lspace: "0.2em",
|
|
356
|
+
width: "+0.4em"
|
|
357
|
+
});
|
|
358
|
+
e.length < t + 2 && e.push(e[0].map(() => ({
|
|
359
|
+
content: null,
|
|
360
|
+
borders: {}
|
|
361
|
+
}))), e.forEach((e, o) => {
|
|
362
|
+
let u = {
|
|
363
|
+
content: null,
|
|
364
|
+
borders: {},
|
|
365
|
+
align: s ? "right" : "left"
|
|
366
|
+
};
|
|
367
|
+
o === t ? (u.content = l(r), u.borders = {
|
|
368
|
+
[c]: a,
|
|
369
|
+
bottom: a
|
|
370
|
+
}) : o === t + 1 ? (u.content = l(i), n !== "shortstackedrightright" && (u.borders[c] = a)) : o > t + 1 && (n === "stackedrightright" || s) && (u.borders[c] = a), s ? e.unshift(u) : e.push(u);
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
function ue(e) {
|
|
374
|
+
for (let t of [...e.querySelectorAll("mstack, mlongdiv")]) !t.closest("math") || t.closest("mjx-container") || t.replaceWith(t.localName === "mstack" ? oe(t) : ce(t));
|
|
375
|
+
}
|
|
376
|
+
//#endregion
|
|
1
377
|
//#region src/unsupported-page.ts
|
|
2
|
-
var
|
|
3
|
-
function
|
|
378
|
+
var N = "https://github.com/pie-framework/pie-players/blob/develop/docs/item-player/loading-strategies.md#one-mathjax-version-per-page", P = "pie-mathjax-version-conflict", F = Symbol.for("@pie-element/shared-math-rendering-mathjax/unsupported-page");
|
|
379
|
+
function I(e, t) {
|
|
4
380
|
if (typeof window > "u") return;
|
|
5
|
-
let
|
|
6
|
-
|
|
381
|
+
let n = globalThis;
|
|
382
|
+
n[F] ??= {
|
|
7
383
|
logged: !1,
|
|
8
384
|
conditions: []
|
|
9
385
|
};
|
|
10
|
-
let
|
|
11
|
-
if (
|
|
12
|
-
|
|
13
|
-
let
|
|
14
|
-
|
|
15
|
-
condition:
|
|
16
|
-
message:
|
|
17
|
-
docsUrl:
|
|
386
|
+
let r = n[F];
|
|
387
|
+
if (r.conditions.includes(e)) return;
|
|
388
|
+
r.conditions.push(e);
|
|
389
|
+
let i = `[math-rendering] Unsupported page (${e}): ${t}. PIE supports one MathJax major version per page; with MathJax 3 and 4 on one page, math rendering is best effort and may break. See ${N}`;
|
|
390
|
+
r.logged || (r.logged = !0, console.error(i)), window.dispatchEvent(new CustomEvent(P, { detail: {
|
|
391
|
+
condition: e,
|
|
392
|
+
message: i,
|
|
393
|
+
docsUrl: N
|
|
18
394
|
} }));
|
|
19
395
|
}
|
|
20
|
-
function
|
|
396
|
+
function de() {
|
|
21
397
|
let e = window.MathJax?.version;
|
|
22
398
|
return typeof e == "string" && e.startsWith("3.") ? e : void 0;
|
|
23
399
|
}
|
|
24
400
|
//#endregion
|
|
25
401
|
//#region src/adapter.ts
|
|
26
|
-
var
|
|
402
|
+
var fe = "https://cdn.jsdelivr.net/npm/mathjax@4.1.3/tex-mml-chtml.js", pe = {
|
|
27
403
|
parallelogram: "\\lower.2em{\\Huge\\unicode{x25B1}}",
|
|
28
404
|
overarc: "\\overparen",
|
|
29
405
|
napprox: "\\not\\approx",
|
|
30
406
|
longdiv: "\\enclose{longdiv}",
|
|
31
407
|
abs: ["\\left|#1\\right|", 1]
|
|
32
|
-
},
|
|
408
|
+
}, me = "[math-rendering] using $ is not advisable, please use $$..$$ or \\(...\\)", he = /\\\(|\\\[|\$\$|\\\$|\\begin\{|\\(?:eq)?ref\{/, ge = /\\\(|\\\[|\$|\\begin\{|\\(?:eq)?ref\{/, _e = /\\embed\{newLine\}\[\]/g, ve = [
|
|
33
409
|
["$$", "$$"],
|
|
34
410
|
["$", "$"],
|
|
35
411
|
["\\[", "\\]"],
|
|
36
412
|
["\\(", "\\)"]
|
|
37
|
-
],
|
|
38
|
-
|
|
413
|
+
], L = Symbol.for("@pie-element/shared-math-rendering-mathjax/loading"), R = "PIE-MJX-CHTML-styles", z = "MJX-CHTML-styles", ye = "PIE-MJX-SVG-styles", be = "MJX-SVG-styles", B = "PIE-MathJax-Menu-Settings", xe = [z, be], Se = { "mjx-container[overflow=\"linebreak\"][display]": {
|
|
414
|
+
overflow: "auto clip",
|
|
415
|
+
"min-width": "initial !important"
|
|
416
|
+
} }, Ce = 200, we = 201;
|
|
417
|
+
function V(e) {
|
|
39
418
|
let t = [...e.matches("mjx-container") ? [e] : [], ...e.querySelectorAll("mjx-container")];
|
|
40
419
|
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
420
|
}
|
|
42
|
-
function
|
|
421
|
+
function H() {
|
|
43
422
|
return window.MathJax;
|
|
44
423
|
}
|
|
45
|
-
function
|
|
46
|
-
for (let [t, n] of
|
|
424
|
+
function U(e) {
|
|
425
|
+
for (let [t, n] of ve) if (e.startsWith(t) && e.endsWith(n)) return e.substring(t.length, e.length - n.length);
|
|
47
426
|
return e;
|
|
48
427
|
}
|
|
49
|
-
function
|
|
50
|
-
return
|
|
428
|
+
function Te(e) {
|
|
429
|
+
return U(e.includes("\\displaystyle") ? e.replace("\\displaystyle", "").trim() : e);
|
|
51
430
|
}
|
|
52
|
-
var
|
|
53
|
-
function
|
|
54
|
-
let n = e.closest(`.${
|
|
55
|
-
return n !== null && t.contains(n) && !n.classList.contains(
|
|
431
|
+
var Ee = "mathjax_ignore", W = "mathjax_process";
|
|
432
|
+
function De(e, t) {
|
|
433
|
+
let n = e.closest(`.${Ee}, .${W}`);
|
|
434
|
+
return n !== null && t.contains(n) && !n.classList.contains(W);
|
|
56
435
|
}
|
|
57
|
-
function
|
|
436
|
+
function Oe(e) {
|
|
58
437
|
for (let t of e.querySelectorAll("[data-latex]")) {
|
|
59
|
-
if (t.dataset.mathHandled || t.closest("mjx-container") ||
|
|
438
|
+
if (t.dataset.mathHandled || t.closest("mjx-container") || De(t, e)) continue;
|
|
60
439
|
let n = t.textContent;
|
|
61
|
-
n && (t.textContent = `\\(${
|
|
440
|
+
n && (t.textContent = `\\(${Te(n)}\\)`.replace(_e, "\\newline "), t.dataset.mathHandled = "true");
|
|
62
441
|
}
|
|
63
442
|
}
|
|
64
|
-
function
|
|
443
|
+
function ke(e) {
|
|
444
|
+
for (let t of e.querySelectorAll("math")) t.closest("mjx-container") || t.setAttribute("displaystyle", "true");
|
|
445
|
+
}
|
|
446
|
+
function Ae(e, t) {
|
|
65
447
|
for (let t of e.querySelectorAll("math")) if (!t.closest("mjx-assistive-mml")) return !0;
|
|
66
|
-
return (t ?
|
|
448
|
+
return (t ? ge : he).test(e.textContent ?? "");
|
|
67
449
|
}
|
|
68
|
-
function
|
|
450
|
+
function je(e) {
|
|
69
451
|
return new Promise((t, n) => {
|
|
70
|
-
let { useSingleDollar: r = !1, accessibility: i = !0, loadFonts:
|
|
452
|
+
let { useSingleDollar: r = !1, accessibility: i = !0, loadFonts: a = !0, srcUrl: o } = e, s = {
|
|
71
453
|
loader: {
|
|
72
454
|
load: i ? ["a11y/assistive-mml"] : [],
|
|
73
455
|
failed: (e) => n(e),
|
|
74
456
|
"output/svg": { ready: (e) => {
|
|
75
|
-
let t =
|
|
76
|
-
return
|
|
457
|
+
let t = H();
|
|
458
|
+
return G(t) && K(t), e;
|
|
77
459
|
} }
|
|
78
460
|
},
|
|
79
461
|
startup: {
|
|
80
462
|
typeset: !1,
|
|
81
463
|
ready: () => {
|
|
82
|
-
let e =
|
|
83
|
-
i && (
|
|
464
|
+
let e = H(), r = e?.startup, i = G(e);
|
|
465
|
+
i && (K(e), Me(e)), r?.defaultReady?.(), i && (Ne(r?.document), r?.document?.addStyles?.(Se)), Promise.resolve(r?.promise).then(() => t(), n);
|
|
84
466
|
}
|
|
85
467
|
},
|
|
86
|
-
tex: { macros: { ...
|
|
468
|
+
tex: { macros: { ...pe } },
|
|
469
|
+
output: { displayOverflow: "linebreak" },
|
|
87
470
|
options: {
|
|
88
471
|
enableMenu: i,
|
|
89
472
|
menuOptions: { settings: {
|
|
@@ -94,150 +477,150 @@ function O(e) {
|
|
|
94
477
|
a11y: { inTabOrder: !1 }
|
|
95
478
|
}
|
|
96
479
|
};
|
|
97
|
-
r &&
|
|
98
|
-
let
|
|
99
|
-
|
|
480
|
+
r && s.tex && (console.warn(me), s.tex.inlineMath = [["$", "$"], ["\\(", "\\)"]], s.tex.processEscapes = !0), a || (s.chtml = { fontURL: "" }), window.MathJax = s;
|
|
481
|
+
let c = document.createElement("script");
|
|
482
|
+
c.src = o || fe, c.async = !0, c.onerror = () => n(/* @__PURE__ */ Error("Failed to load MathJax")), document.head.appendChild(c);
|
|
100
483
|
});
|
|
101
484
|
}
|
|
102
|
-
function
|
|
485
|
+
function G(e) {
|
|
103
486
|
return !!e?.version?.startsWith("4.");
|
|
104
487
|
}
|
|
105
|
-
function
|
|
488
|
+
function K(e) {
|
|
106
489
|
let t = e?._?.output, n = t?.chtml_ts?.CHTML;
|
|
107
|
-
n && (n.STYLESHEETID =
|
|
490
|
+
n && (n.STYLESHEETID = R);
|
|
108
491
|
let r = t?.svg_ts?.SVG;
|
|
109
|
-
r && (r.STYLESHEETID =
|
|
492
|
+
r && (r.STYLESHEETID = ye);
|
|
110
493
|
}
|
|
111
|
-
function
|
|
494
|
+
function Me(e) {
|
|
112
495
|
let t = e?._?.ui?.menu?.Menu?.Menu;
|
|
113
496
|
if (t) {
|
|
114
|
-
t.MENU_STORAGE =
|
|
497
|
+
t.MENU_STORAGE = B;
|
|
115
498
|
try {
|
|
116
|
-
let e = localStorage.getItem(
|
|
499
|
+
let e = localStorage.getItem(B);
|
|
117
500
|
if (!e) return;
|
|
118
501
|
let { assistiveMml: t, ...n } = JSON.parse(e);
|
|
119
502
|
if (t === void 0) return;
|
|
120
|
-
Object.keys(n).length ? localStorage.setItem(
|
|
503
|
+
Object.keys(n).length ? localStorage.setItem(B, JSON.stringify(n)) : localStorage.removeItem(B);
|
|
121
504
|
} catch {}
|
|
122
505
|
}
|
|
123
506
|
}
|
|
124
|
-
function
|
|
507
|
+
function Ne(e) {
|
|
125
508
|
if (!e) return;
|
|
126
509
|
let t = /* @__PURE__ */ new WeakSet(), n = (e) => {
|
|
127
510
|
let n = e.typesetRoot;
|
|
128
|
-
!n || t.has(n) || (
|
|
511
|
+
!n || t.has(n) || (V(n), t.add(n));
|
|
129
512
|
};
|
|
130
|
-
e.addRenderAction?.("pie-strip-latex",
|
|
513
|
+
e.addRenderAction?.("pie-strip-latex", we, (e) => {
|
|
131
514
|
for (let t of e.math ?? []) n(t);
|
|
132
515
|
return !1;
|
|
133
516
|
}, (e) => {
|
|
134
517
|
let t = e.typesetRoot;
|
|
135
|
-
return t &&
|
|
518
|
+
return t && V(t), !1;
|
|
136
519
|
});
|
|
137
520
|
let r = e.outputJax?.chtmlStyles;
|
|
138
|
-
r?.id ===
|
|
521
|
+
r?.id === z && (r.id = R), Pe();
|
|
139
522
|
}
|
|
140
|
-
function
|
|
141
|
-
return e instanceof HTMLStyleElement &&
|
|
523
|
+
function q(e) {
|
|
524
|
+
return e instanceof HTMLStyleElement && xe.includes(e.id);
|
|
142
525
|
}
|
|
143
|
-
function
|
|
144
|
-
|
|
526
|
+
function J(e) {
|
|
527
|
+
I("foreign-output-stylesheet", `another MathJax added <style id="${e.id}"> to <head>`);
|
|
145
528
|
}
|
|
146
|
-
function
|
|
147
|
-
let e = document.head, t = [...e.children].find(
|
|
529
|
+
function Pe() {
|
|
530
|
+
let e = document.head, t = [...e.children].find(q);
|
|
148
531
|
if (t) {
|
|
149
|
-
|
|
532
|
+
J(t);
|
|
150
533
|
return;
|
|
151
534
|
}
|
|
152
535
|
let n = new MutationObserver((e) => {
|
|
153
536
|
for (let t of e) {
|
|
154
|
-
let e = [...t.addedNodes].find(
|
|
537
|
+
let e = [...t.addedNodes].find(q);
|
|
155
538
|
if (e) {
|
|
156
|
-
|
|
539
|
+
J(e), n.disconnect();
|
|
157
540
|
return;
|
|
158
541
|
}
|
|
159
542
|
}
|
|
160
543
|
});
|
|
161
544
|
n.observe(e, { childList: !0 });
|
|
162
545
|
}
|
|
163
|
-
function
|
|
546
|
+
function Fe(e) {
|
|
164
547
|
let t = e?.math;
|
|
165
548
|
if (!t?.remove) return;
|
|
166
|
-
let n = [...t].filter((e) => (e.state?.() ?? 0) >=
|
|
549
|
+
let n = [...t].filter((e) => (e.state?.() ?? 0) >= Ce && e.typesetRoot?.isConnected === !1);
|
|
167
550
|
if (n.length !== 0) {
|
|
168
551
|
for (let e of [...n].reverse()) e.clear?.();
|
|
169
552
|
t.remove(...n);
|
|
170
553
|
}
|
|
171
554
|
}
|
|
172
|
-
function
|
|
555
|
+
function Ie(e) {
|
|
173
556
|
return new Promise((t, n) => {
|
|
174
557
|
let r = e.startup ?? {};
|
|
175
558
|
e.startup = r;
|
|
176
559
|
let i = r.ready;
|
|
177
560
|
r.ready = () => {
|
|
178
|
-
let e =
|
|
561
|
+
let e = H()?.startup;
|
|
179
562
|
i ? i.call(r) : e?.defaultReady?.(), Promise.resolve(e?.promise).then(() => t(), n);
|
|
180
563
|
};
|
|
181
564
|
});
|
|
182
565
|
}
|
|
183
|
-
function
|
|
184
|
-
let t =
|
|
185
|
-
return t ? t.version ? t.startup?.promise ? Promise.resolve(t.startup.promise).then(() => void 0) : Promise.resolve() :
|
|
566
|
+
function Le(e) {
|
|
567
|
+
let t = H();
|
|
568
|
+
return t ? t.version ? t.startup?.promise ? Promise.resolve(t.startup.promise).then(() => void 0) : Promise.resolve() : Ie(t) : je(e);
|
|
186
569
|
}
|
|
187
|
-
function
|
|
188
|
-
return
|
|
189
|
-
typeof
|
|
570
|
+
function Re(e) {
|
|
571
|
+
return Le(e).then(() => {
|
|
572
|
+
typeof H()?.typesetPromise != "function" && console.warn("[mathjax-renderer] MathJax on this page has no typesetPromise; math stays untypeset.");
|
|
190
573
|
});
|
|
191
574
|
}
|
|
192
|
-
function
|
|
193
|
-
let t = globalThis, n = t[
|
|
194
|
-
return n || (n =
|
|
575
|
+
function ze(e) {
|
|
576
|
+
let t = globalThis, n = t[L];
|
|
577
|
+
return n || (n = Re(e), t[L] = n), n;
|
|
195
578
|
}
|
|
196
|
-
function
|
|
579
|
+
function Y(e = {}) {
|
|
197
580
|
let t = e.useSingleDollar ?? !1;
|
|
198
581
|
return async (n) => {
|
|
199
582
|
if (typeof window > "u") return;
|
|
200
|
-
let
|
|
201
|
-
|
|
202
|
-
let
|
|
203
|
-
if (!
|
|
204
|
-
|
|
583
|
+
let i = de();
|
|
584
|
+
i && I("mathjax-3-global", `window.MathJax is MathJax ${i}`), Oe(n), r(n), ke(n), ue(n);
|
|
585
|
+
let a = ze(e);
|
|
586
|
+
if (!Ae(n, t)) {
|
|
587
|
+
a.catch(() => {});
|
|
205
588
|
return;
|
|
206
589
|
}
|
|
207
|
-
await
|
|
208
|
-
let
|
|
209
|
-
if (await
|
|
210
|
-
|
|
590
|
+
await a;
|
|
591
|
+
let o = H();
|
|
592
|
+
if (await o?.startup?.promise, typeof o?.typesetPromise == "function") {
|
|
593
|
+
Fe(o.startup?.document);
|
|
211
594
|
try {
|
|
212
|
-
await
|
|
595
|
+
await o.typesetPromise([n]);
|
|
213
596
|
} catch (e) {
|
|
214
|
-
throw
|
|
597
|
+
throw o.typesetClear?.([n]), e;
|
|
215
598
|
}
|
|
216
|
-
|
|
599
|
+
V(n);
|
|
217
600
|
}
|
|
218
601
|
};
|
|
219
602
|
}
|
|
220
|
-
function
|
|
603
|
+
function Be() {
|
|
221
604
|
return [];
|
|
222
605
|
}
|
|
223
606
|
//#endregion
|
|
224
607
|
//#region src/render-math.ts
|
|
225
|
-
var
|
|
226
|
-
function
|
|
227
|
-
return !!window[
|
|
608
|
+
var Ve = "@pie-lib/math-rendering", He = "@pie-lib/math-rendering@2", Ue = "pie-print", We = 50, Ge = 1e4, X = null;
|
|
609
|
+
function Ke() {
|
|
610
|
+
return !!window[He]?.opts?.useSingleDollar;
|
|
228
611
|
}
|
|
229
|
-
function
|
|
230
|
-
return
|
|
612
|
+
function qe() {
|
|
613
|
+
return X ||= Y({ useSingleDollar: Ke() }), X;
|
|
231
614
|
}
|
|
232
615
|
function Z() {
|
|
233
616
|
if (typeof window > "u") return null;
|
|
234
|
-
let e = window[
|
|
617
|
+
let e = window[Ve];
|
|
235
618
|
return typeof e?.renderMath == "function" && e.renderMath !== $ ? e : null;
|
|
236
619
|
}
|
|
237
|
-
function
|
|
620
|
+
function Je(e) {
|
|
238
621
|
let t = e;
|
|
239
622
|
for (; t;) {
|
|
240
|
-
if (t instanceof Element && t.localName ===
|
|
623
|
+
if (t instanceof Element && t.localName === Ue) return t;
|
|
241
624
|
t = t instanceof ShadowRoot ? t.host : t.parentNode;
|
|
242
625
|
}
|
|
243
626
|
return null;
|
|
@@ -246,14 +629,14 @@ function Q() {
|
|
|
246
629
|
let e = window.renderMath;
|
|
247
630
|
return typeof e == "function" ? e : null;
|
|
248
631
|
}
|
|
249
|
-
async function
|
|
250
|
-
let t =
|
|
632
|
+
async function Ye(e) {
|
|
633
|
+
let t = Je(e);
|
|
251
634
|
if (!t) return null;
|
|
252
635
|
let n = Q();
|
|
253
636
|
if (n || t.mathRenderingModuleUrlImported !== !0) return n;
|
|
254
|
-
let r = Date.now() +
|
|
637
|
+
let r = Date.now() + Ge;
|
|
255
638
|
for (; Date.now() < r;) {
|
|
256
|
-
await new Promise((e) => setTimeout(e,
|
|
639
|
+
await new Promise((e) => setTimeout(e, We));
|
|
257
640
|
let e = Q();
|
|
258
641
|
if (e) return e;
|
|
259
642
|
}
|
|
@@ -263,14 +646,14 @@ var $ = async (e) => {
|
|
|
263
646
|
if (typeof window > "u") return;
|
|
264
647
|
let t = typeof e == "string", n;
|
|
265
648
|
t ? (n = document.createElement("div"), n.innerHTML = e) : n = e || document.body;
|
|
266
|
-
let r = Z(), i = r || t ? null : await
|
|
267
|
-
return r ? await r.renderMath?.(n) : i && i !== $ ? await i(n) : await
|
|
268
|
-
},
|
|
649
|
+
let r = Z(), i = r || t ? null : await Ye(n);
|
|
650
|
+
return r ? await r.renderMath?.(n) : i && i !== $ ? await i(n) : await qe()(n), t ? n.innerHTML : void 0;
|
|
651
|
+
}, Xe = (e, t) => {
|
|
269
652
|
let n = Z()?.wrapMath?.(e, t);
|
|
270
653
|
if (n != null) return n;
|
|
271
654
|
let [r, i] = t === "dollar" || t === "double_dollar" ? ["$", "$"] : ["\\(", "\\)"];
|
|
272
|
-
return `${r}${
|
|
273
|
-
},
|
|
655
|
+
return `${r}${U(e)}${i}`;
|
|
656
|
+
}, Ze = (e) => {
|
|
274
657
|
let t = e.trim();
|
|
275
658
|
return t.startsWith("\\[") && t.endsWith("\\]") ? {
|
|
276
659
|
unwrapped: t.slice(2, -2).trim(),
|
|
@@ -297,6 +680,6 @@ var $ = async (e) => {
|
|
|
297
680
|
close: "$"
|
|
298
681
|
}
|
|
299
682
|
} : { unwrapped: t };
|
|
300
|
-
},
|
|
683
|
+
}, Qe = (e) => Z()?.mmlToLatex?.(e) ?? e;
|
|
301
684
|
//#endregion
|
|
302
|
-
export {
|
|
685
|
+
export { P as MATHJAX_CONFLICT_EVENT, N as UNSUPPORTED_PAGE_DOCS_URL, Y as createMathjaxRenderer, Be as getMathjaxCssUrls, Qe as mmlToLatex, $ as renderMath, Ze as unWrapMath, Xe as wrapMath };
|
package/dist/mathml.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const MATHML_NS = "http://www.w3.org/1998/Math/MathML";
|
|
2
|
+
/**
|
|
3
|
+
* Re-creates prefixed MathML, a `<mml:math>` and its `mml:` descendants, as MathML elements. The
|
|
4
|
+
* HTML parser reads a prefixed tag as an unknown HTML element, and MathJax finds prefixed math only
|
|
5
|
+
* when `<html>` binds the prefix, which host pages do not. Any prefix on a root named `math` counts,
|
|
6
|
+
* bound or not, since authored content declares only the default namespace.
|
|
7
|
+
*/
|
|
8
|
+
export declare function unprefixMathml(root: Element): void;
|
package/package.json
CHANGED