@pie-element/shared-math-rendering-mathjax 0.1.1-next.2 → 0.1.1-next.3
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 +7 -57
- package/dist/index.js +98 -65
- package/dist/types.d.ts +4 -3
- package/package.json +1 -1
package/dist/adapter.d.ts
CHANGED
|
@@ -1,66 +1,16 @@
|
|
|
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, the first time an element holds math, from `srcUrl` or MathJax
|
|
6
|
+
* 4.1.3 on jsDelivr. A MathJax the page already has, or is loading, is used instead.
|
|
45
7
|
*
|
|
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
|
-
* ```
|
|
8
|
+
* @param options - Renderer options
|
|
9
|
+
* @returns Renderer function that typesets math in an element
|
|
58
10
|
*/
|
|
59
11
|
export declare function createMathjaxRenderer(options?: MathjaxOptions): (element: HTMLElement) => Promise<void>;
|
|
60
12
|
/**
|
|
61
|
-
* Get CSS URLs
|
|
62
|
-
*
|
|
63
|
-
* @returns Array of CSS URLs needed for MathJax rendering (empty - MathJax handles its own CSS)
|
|
13
|
+
* Get CSS URLs for MathJax
|
|
14
|
+
* MathJax v4 injects its own styles, so no external CSS needed
|
|
64
15
|
*/
|
|
65
16
|
export declare function getMathjaxCssUrls(): string[];
|
|
66
|
-
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,88 +1,121 @@
|
|
|
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
|
+
});
|
|
52
72
|
}
|
|
53
|
-
function
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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);
|
|
76
|
+
}
|
|
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" || (l(n), !u(n, t))) return;
|
|
90
|
+
await h(e);
|
|
91
|
+
let r = s();
|
|
92
|
+
await r?.startup?.promise, typeof r?.typesetPromise == "function" && await r.typesetPromise([n]);
|
|
60
93
|
};
|
|
61
94
|
}
|
|
62
|
-
function
|
|
95
|
+
function _() {
|
|
63
96
|
return [];
|
|
64
97
|
}
|
|
65
98
|
//#endregion
|
|
66
99
|
//#region src/render-math.ts
|
|
67
|
-
var
|
|
68
|
-
function
|
|
69
|
-
return
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}),
|
|
100
|
+
var v = "@pie-lib/math-rendering", y = "@pie-lib/math-rendering@2", b = null;
|
|
101
|
+
function x() {
|
|
102
|
+
return !!window[y]?.opts?.useSingleDollar;
|
|
103
|
+
}
|
|
104
|
+
function S() {
|
|
105
|
+
return b ||= g({ useSingleDollar: x() }), b;
|
|
73
106
|
}
|
|
74
|
-
function
|
|
107
|
+
function C() {
|
|
75
108
|
if (typeof window > "u") return null;
|
|
76
|
-
let e = window[
|
|
109
|
+
let e = window[v];
|
|
77
110
|
return typeof e?.renderMath == "function" ? e : null;
|
|
78
111
|
}
|
|
79
|
-
var
|
|
112
|
+
var w = async (e) => {
|
|
80
113
|
if (typeof window > "u") return;
|
|
81
114
|
let t = typeof e == "string", n;
|
|
82
115
|
t ? (n = document.createElement("div"), n.innerHTML = e) : n = e || document.body;
|
|
83
|
-
let r =
|
|
84
|
-
return r ? await r.renderMath?.(n) : await
|
|
85
|
-
},
|
|
116
|
+
let r = C();
|
|
117
|
+
return r ? await r.renderMath?.(n) : await S()(n), t ? n.innerHTML : void 0;
|
|
118
|
+
}, T = (e) => C()?.wrapMath?.(e) ?? e, E = (e) => {
|
|
86
119
|
let t = e.trim();
|
|
87
120
|
return t.startsWith("\\[") && t.endsWith("\\]") ? {
|
|
88
121
|
unwrapped: t.slice(2, -2).trim(),
|
|
@@ -109,6 +142,6 @@ var l = async (e) => {
|
|
|
109
142
|
close: "$"
|
|
110
143
|
}
|
|
111
144
|
} : { unwrapped: t };
|
|
112
|
-
},
|
|
145
|
+
}, D = (e) => C()?.mmlToLatex?.(e) ?? e;
|
|
113
146
|
//#endregion
|
|
114
|
-
export {
|
|
147
|
+
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
|
}
|