@pie-element/shared-math-rendering-mathjax 0.1.1-next.2 → 0.1.1-next.4
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 +8 -57
- package/dist/index.js +102 -63
- package/dist/types.d.ts +4 -3
- package/package.json +1 -1
package/dist/adapter.d.ts
CHANGED
|
@@ -1,66 +1,17 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MathJax math renderer
|
|
3
|
-
*
|
|
4
|
-
* Loads MathJax v4 from CDN and provides math rendering functionality.
|
|
5
|
-
* Supports LaTeX and MathML with full accessibility features.
|
|
6
|
-
*/
|
|
7
1
|
import type { MathjaxOptions } from './types.js';
|
|
8
|
-
interface MathJaxTexConfig {
|
|
9
|
-
inlineMath?: [string, string][];
|
|
10
|
-
displayMath?: [string, string][];
|
|
11
|
-
processEscapes?: boolean;
|
|
12
|
-
packages?: string[];
|
|
13
|
-
macros?: Record<string, string>;
|
|
14
|
-
}
|
|
15
|
-
interface MathJaxDocument {
|
|
16
|
-
assistiveMml?: () => {
|
|
17
|
-
updateDocument?: () => unknown;
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
interface MathJaxInstance {
|
|
21
|
-
version?: string;
|
|
22
|
-
tex?: MathJaxTexConfig;
|
|
23
|
-
options?: {
|
|
24
|
-
enableMenu?: boolean;
|
|
25
|
-
enableExplorer?: boolean;
|
|
26
|
-
enableAssistiveMml?: boolean;
|
|
27
|
-
};
|
|
28
|
-
startup: {
|
|
29
|
-
ready?: () => void;
|
|
30
|
-
defaultReady: () => void;
|
|
31
|
-
document?: MathJaxDocument;
|
|
32
|
-
};
|
|
33
|
-
typesetPromise?: (elements?: Element[]) => Promise<void>;
|
|
34
|
-
}
|
|
35
|
-
declare global {
|
|
36
|
-
interface Window {
|
|
37
|
-
MathJax?: MathJaxInstance;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
2
|
/**
|
|
41
|
-
* Create a MathJax
|
|
3
|
+
* Create a MathJax renderer function.
|
|
42
4
|
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
5
|
+
* MathJax loads once per page, on the first render, from `srcUrl` or MathJax 4.1.3 on jsDelivr.
|
|
6
|
+
* A MathJax the page already has, or is loading, is used instead. A render waits on the load only
|
|
7
|
+
* when its element holds math.
|
|
45
8
|
*
|
|
46
|
-
* @param options -
|
|
47
|
-
* @returns
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```typescript
|
|
51
|
-
* const renderer = createMathjaxRenderer({
|
|
52
|
-
* accessibility: true,
|
|
53
|
-
* useSingleDollar: false
|
|
54
|
-
* });
|
|
55
|
-
*
|
|
56
|
-
* renderer(document.body);
|
|
57
|
-
* ```
|
|
9
|
+
* @param options - Renderer options
|
|
10
|
+
* @returns Renderer function that typesets math in an element
|
|
58
11
|
*/
|
|
59
12
|
export declare function createMathjaxRenderer(options?: MathjaxOptions): (element: HTMLElement) => Promise<void>;
|
|
60
13
|
/**
|
|
61
|
-
* Get CSS URLs
|
|
62
|
-
*
|
|
63
|
-
* @returns Array of CSS URLs needed for MathJax rendering (empty - MathJax handles its own CSS)
|
|
14
|
+
* Get CSS URLs for MathJax
|
|
15
|
+
* MathJax v4 injects its own styles, so no external CSS needed
|
|
64
16
|
*/
|
|
65
17
|
export declare function getMathjaxCssUrls(): string[];
|
|
66
|
-
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,88 +1,127 @@
|
|
|
1
1
|
//#region src/adapter.ts
|
|
2
|
-
var e =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
2
|
+
var e = {
|
|
3
|
+
parallelogram: "\\lower.2em{\\Huge\\unicode{x25B1}}",
|
|
4
|
+
overarc: "\\overparen",
|
|
5
|
+
napprox: "\\not\\approx",
|
|
6
|
+
longdiv: "\\enclose{longdiv}",
|
|
7
|
+
abs: ["\\left|#1\\right|", 1]
|
|
8
|
+
}, t = "[math-rendering] using $ is not advisable, please use $$..$$ or \\(...\\)", n = /\\\(|\\\[|\$\$|\\\$|\\begin\{|\\(?:eq)?ref\{/, r = /\\\(|\\\[|\$|\\begin\{|\\(?:eq)?ref\{/, i = /\\embed\{newLine\}\[\]/g, a = [
|
|
9
|
+
["$$", "$$"],
|
|
10
|
+
["$", "$"],
|
|
11
|
+
["\\[", "\\]"],
|
|
12
|
+
["\\(", "\\)"]
|
|
13
|
+
], o = Symbol.for("@pie-element/shared-math-rendering-mathjax/loading");
|
|
14
|
+
function s() {
|
|
15
|
+
return window.MathJax;
|
|
16
|
+
}
|
|
17
|
+
function c(e) {
|
|
18
|
+
let t = e.includes("\\displaystyle") ? e.replace("\\displaystyle", "").trim() : e;
|
|
19
|
+
for (let [e, n] of a) if (t.startsWith(e) && t.endsWith(n)) return t.substring(e.length, t.length - n.length);
|
|
20
|
+
return t;
|
|
21
|
+
}
|
|
22
|
+
function l(e) {
|
|
23
|
+
for (let t of e.querySelectorAll("[data-latex]")) {
|
|
24
|
+
if (t.dataset.mathHandled || t.closest("mjx-container")) continue;
|
|
25
|
+
let e = t.textContent;
|
|
26
|
+
e && (t.textContent = `\\(${c(e)}\\)`.replace(i, "\\newline "), t.dataset.mathHandled = "true");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function u(e, t) {
|
|
30
|
+
for (let t of e.querySelectorAll("math")) if (!t.closest("mjx-assistive-mml")) return !0;
|
|
31
|
+
return (t ? r : n).test(e.textContent ?? "");
|
|
32
|
+
}
|
|
33
|
+
function d(n) {
|
|
34
|
+
return new Promise((r, i) => {
|
|
35
|
+
let { useSingleDollar: a = !1, accessibility: o = !0, loadFonts: c = !0, srcUrl: l } = n, u = {
|
|
36
|
+
loader: {
|
|
37
|
+
load: o ? ["a11y/assistive-mml"] : [],
|
|
38
|
+
failed: (e) => i(e)
|
|
27
39
|
},
|
|
28
40
|
startup: {
|
|
41
|
+
typeset: !1,
|
|
29
42
|
ready: () => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
43
|
+
let e = s()?.startup;
|
|
44
|
+
e?.defaultReady?.(), Promise.resolve(e?.promise).then(() => r(), i);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
tex: { macros: { ...e } },
|
|
48
|
+
options: {
|
|
49
|
+
enableMenu: o,
|
|
50
|
+
menuOptions: { settings: {
|
|
51
|
+
assistiveMml: o,
|
|
52
|
+
enrich: !1,
|
|
53
|
+
inTabOrder: !1
|
|
54
|
+
} }
|
|
33
55
|
}
|
|
34
56
|
};
|
|
35
|
-
|
|
36
|
-
let
|
|
37
|
-
|
|
38
|
-
setTimeout(e, 100);
|
|
39
|
-
}, c.onerror = () => {
|
|
40
|
-
n(/* @__PURE__ */ Error("Failed to load MathJax"));
|
|
41
|
-
}, document.head.appendChild(c);
|
|
57
|
+
a && u.tex && (console.warn(t), u.tex.inlineMath = [["$", "$"], ["\\(", "\\)"]], u.tex.processEscapes = !0), c || (u.chtml = { fontURL: "" }), window.MathJax = u;
|
|
58
|
+
let d = document.createElement("script");
|
|
59
|
+
d.src = l || "https://cdn.jsdelivr.net/npm/mathjax@4.1.3/tex-mml-chtml.js", d.async = !0, d.onerror = () => i(/* @__PURE__ */ Error("Failed to load MathJax")), document.head.appendChild(d);
|
|
42
60
|
});
|
|
43
|
-
return n[e] = i, i;
|
|
44
61
|
}
|
|
45
|
-
function
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
e.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
62
|
+
function f(e) {
|
|
63
|
+
return new Promise((t, n) => {
|
|
64
|
+
let r = e.startup ?? {};
|
|
65
|
+
e.startup = r;
|
|
66
|
+
let i = r.ready;
|
|
67
|
+
r.ready = () => {
|
|
68
|
+
let e = s()?.startup;
|
|
69
|
+
i ? i.call(r) : e?.defaultReady?.(), Promise.resolve(e?.promise).then(() => t(), n);
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function p(e) {
|
|
74
|
+
let t = s();
|
|
75
|
+
return t ? t.version ? t.startup?.promise ? Promise.resolve(t.startup.promise).then(() => void 0) : Promise.resolve() : f(t) : d(e);
|
|
52
76
|
}
|
|
53
|
-
function
|
|
54
|
-
return
|
|
55
|
-
|
|
56
|
-
|
|
77
|
+
function m(e) {
|
|
78
|
+
return p(e).then(() => {
|
|
79
|
+
typeof s()?.typesetPromise != "function" && console.warn("[mathjax-renderer] MathJax on this page has no typesetPromise; math stays untypeset.");
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function h(e) {
|
|
83
|
+
let t = globalThis, n = t[o];
|
|
84
|
+
return n || (n = m(e), t[o] = n), n;
|
|
85
|
+
}
|
|
86
|
+
function g(e = {}) {
|
|
87
|
+
let t = e.useSingleDollar ?? !1;
|
|
88
|
+
return async (n) => {
|
|
89
|
+
if (typeof window > "u") return;
|
|
90
|
+
l(n);
|
|
91
|
+
let r = h(e);
|
|
92
|
+
if (!u(n, t)) {
|
|
93
|
+
r.catch(() => {});
|
|
57
94
|
return;
|
|
58
95
|
}
|
|
59
|
-
await
|
|
96
|
+
await r;
|
|
97
|
+
let i = s();
|
|
98
|
+
await i?.startup?.promise, typeof i?.typesetPromise == "function" && await i.typesetPromise([n]);
|
|
60
99
|
};
|
|
61
100
|
}
|
|
62
|
-
function
|
|
101
|
+
function _() {
|
|
63
102
|
return [];
|
|
64
103
|
}
|
|
65
104
|
//#endregion
|
|
66
105
|
//#region src/render-math.ts
|
|
67
|
-
var
|
|
68
|
-
function
|
|
69
|
-
return
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}),
|
|
106
|
+
var v = "@pie-lib/math-rendering", y = "@pie-lib/math-rendering@2", b = null;
|
|
107
|
+
function x() {
|
|
108
|
+
return !!window[y]?.opts?.useSingleDollar;
|
|
109
|
+
}
|
|
110
|
+
function S() {
|
|
111
|
+
return b ||= g({ useSingleDollar: x() }), b;
|
|
73
112
|
}
|
|
74
|
-
function
|
|
113
|
+
function C() {
|
|
75
114
|
if (typeof window > "u") return null;
|
|
76
|
-
let e = window[
|
|
115
|
+
let e = window[v];
|
|
77
116
|
return typeof e?.renderMath == "function" ? e : null;
|
|
78
117
|
}
|
|
79
|
-
var
|
|
118
|
+
var w = async (e) => {
|
|
80
119
|
if (typeof window > "u") return;
|
|
81
120
|
let t = typeof e == "string", n;
|
|
82
121
|
t ? (n = document.createElement("div"), n.innerHTML = e) : n = e || document.body;
|
|
83
|
-
let r =
|
|
84
|
-
return r ? await r.renderMath?.(n) : await
|
|
85
|
-
},
|
|
122
|
+
let r = C();
|
|
123
|
+
return r ? await r.renderMath?.(n) : await S()(n), t ? n.innerHTML : void 0;
|
|
124
|
+
}, T = (e) => C()?.wrapMath?.(e) ?? e, E = (e) => {
|
|
86
125
|
let t = e.trim();
|
|
87
126
|
return t.startsWith("\\[") && t.endsWith("\\]") ? {
|
|
88
127
|
unwrapped: t.slice(2, -2).trim(),
|
|
@@ -109,6 +148,6 @@ var l = async (e) => {
|
|
|
109
148
|
close: "$"
|
|
110
149
|
}
|
|
111
150
|
} : { unwrapped: t };
|
|
112
|
-
},
|
|
151
|
+
}, D = (e) => C()?.mmlToLatex?.(e) ?? e;
|
|
113
152
|
//#endregion
|
|
114
|
-
export {
|
|
153
|
+
export { g as createMathjaxRenderer, _ as getMathjaxCssUrls, D as mmlToLatex, w as renderMath, E as unWrapMath, T as wrapMath };
|
package/dist/types.d.ts
CHANGED
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export interface MathjaxOptions {
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Treat `$...$` as inline math. The renderer behind `renderMath` enables it when the page sets
|
|
7
|
+
* the legacy opt-in, `window['@pie-lib/math-rendering@2'] = { opts: { useSingleDollar: true } }`.
|
|
7
8
|
* @default false
|
|
8
9
|
*/
|
|
9
10
|
useSingleDollar?: boolean;
|
|
10
11
|
/**
|
|
11
|
-
*
|
|
12
|
+
* Add hidden MathML for screen readers and the MathJax context menu
|
|
12
13
|
* @default true
|
|
13
14
|
*/
|
|
14
15
|
accessibility?: boolean;
|
|
@@ -18,7 +19,7 @@ export interface MathjaxOptions {
|
|
|
18
19
|
*/
|
|
19
20
|
loadFonts?: boolean;
|
|
20
21
|
/**
|
|
21
|
-
* Override the MathJax script URL.
|
|
22
|
+
* Override the MathJax script URL. Defaults to MathJax 4.1.3 `tex-mml-chtml.js` on jsDelivr.
|
|
22
23
|
*/
|
|
23
24
|
srcUrl?: string;
|
|
24
25
|
}
|