@pie-element/shared-math-rendering-mathjax 0.1.0
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 +60 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +71 -0
- package/dist/render-math.d.ts +29 -0
- package/dist/render-math.d.ts.map +1 -0
- package/dist/types.d.ts +25 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +40 -0
|
@@ -0,0 +1,60 @@
|
|
|
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
|
+
import type { MathjaxOptions } from './types';
|
|
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 MathJaxInstance {
|
|
16
|
+
version?: string;
|
|
17
|
+
tex?: MathJaxTexConfig;
|
|
18
|
+
options?: {
|
|
19
|
+
enableMenu?: boolean;
|
|
20
|
+
enableExplorer?: boolean;
|
|
21
|
+
};
|
|
22
|
+
startup: {
|
|
23
|
+
ready?: () => void;
|
|
24
|
+
defaultReady: () => void;
|
|
25
|
+
};
|
|
26
|
+
typesetPromise?: (elements?: Element[]) => Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
declare global {
|
|
29
|
+
interface Window {
|
|
30
|
+
MathJax?: MathJaxInstance;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Create a MathJax-based math renderer
|
|
35
|
+
*
|
|
36
|
+
* Loads MathJax v4 from CDN (~2.7MB, cached) and returns a function
|
|
37
|
+
* that can render math expressions in DOM elements.
|
|
38
|
+
*
|
|
39
|
+
* @param options - MathJax configuration options
|
|
40
|
+
* @returns Math renderer function
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const renderer = createMathjaxRenderer({
|
|
45
|
+
* accessibility: true,
|
|
46
|
+
* useSingleDollar: false
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* renderer(document.body);
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare function createMathjaxRenderer(options?: MathjaxOptions): (element: HTMLElement) => Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Get CSS URLs required by MathJax renderer
|
|
55
|
+
*
|
|
56
|
+
* @returns Array of CSS URLs needed for MathJax rendering (empty - MathJax handles its own CSS)
|
|
57
|
+
*/
|
|
58
|
+
export declare function getMathjaxCssUrls(): string[];
|
|
59
|
+
export {};
|
|
60
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE9C,UAAU,gBAAgB;IACxB,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC;IAChC,WAAW,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC;IACjC,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACjC;AAiBD,UAAU,eAAe;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,GAAG,CAAC,EAAE,gBAAgB,CAAC;IACvB,OAAO,CAAC,EAAE;QACR,UAAU,CAAC,EAAE,OAAO,CAAC;QACrB,cAAc,CAAC,EAAE,OAAO,CAAC;KAC1B,CAAC;IACF,OAAO,EAAE;QACP,KAAK,CAAC,EAAE,MAAM,IAAI,CAAC;QACnB,YAAY,EAAE,MAAM,IAAI,CAAC;KAC1B,CAAC;IACF,cAAc,CAAC,EAAE,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1D;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,OAAO,CAAC,EAAE,eAAe,CAAC;KAC3B;CACF;AA4ED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,GAAE,cAAmB,GAC3B,CAAC,OAAO,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,CAWzC;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,IAAI,MAAM,EAAE,CAG5C"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @pie-element/math-rendering-mathjax
|
|
3
|
+
*
|
|
4
|
+
* MathJax adapter for PIE math rendering (~2.7MB)
|
|
5
|
+
* Full-featured LaTeX and MathML rendering with accessibility support.
|
|
6
|
+
*
|
|
7
|
+
* This adapter wraps the upstream @pie-lib/math-rendering package,
|
|
8
|
+
* which must be loaded separately (e.g., via a script tag or bundled).
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { createMathjaxRenderer } from '@pie-element/shared-math-rendering-mathjax';
|
|
13
|
+
*
|
|
14
|
+
* const renderer = createMathjaxRenderer();
|
|
15
|
+
* renderer(document.body);
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export { createMathjaxRenderer, getMathjaxCssUrls } from './adapter';
|
|
19
|
+
export type { MathjaxOptions } from './types';
|
|
20
|
+
export { renderMath, wrapMath, unWrapMath, mmlToLatex } from './render-math';
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AACrE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAG9C,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const c = "https://cdn.jsdelivr.net/npm/mathjax@4/tex-chtml.js";
|
|
2
|
+
let o = null;
|
|
3
|
+
function p(t) {
|
|
4
|
+
return typeof window > "u" || window.MathJax?.version ? Promise.resolve() : o || (o = new Promise((e, r) => {
|
|
5
|
+
const { useSingleDollar: d = !1, accessibility: s = !0, loadFonts: l = !0, srcUrl: u } = t, n = {
|
|
6
|
+
tex: {
|
|
7
|
+
packages: ["base", "ams", "autoload"],
|
|
8
|
+
macros: {
|
|
9
|
+
parallelogram: "\\lower.2em{\\Huge\\unicode{x25B1}}",
|
|
10
|
+
overarc: "\\overparen",
|
|
11
|
+
napprox: "\\not\\approx",
|
|
12
|
+
longdiv: "\\enclose{longdiv}"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
options: {
|
|
16
|
+
enableMenu: s,
|
|
17
|
+
enableExplorer: s
|
|
18
|
+
},
|
|
19
|
+
startup: {
|
|
20
|
+
ready: () => {
|
|
21
|
+
window.MathJax?.startup.defaultReady && (window.MathJax.startup.defaultReady(), e());
|
|
22
|
+
},
|
|
23
|
+
defaultReady: () => {
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
d && n.tex && (n.tex.inlineMath = [
|
|
28
|
+
["$", "$"],
|
|
29
|
+
["\\(", "\\)"]
|
|
30
|
+
], n.tex.processEscapes = !0), l || (n.chtml = { fontURL: "" }), window.MathJax = n;
|
|
31
|
+
const a = document.createElement("script");
|
|
32
|
+
a.src = u || c, a.async = !0, a.onload = () => {
|
|
33
|
+
setTimeout(e, 100);
|
|
34
|
+
}, a.onerror = () => {
|
|
35
|
+
r(new Error("Failed to load MathJax"));
|
|
36
|
+
}, document.head.appendChild(a);
|
|
37
|
+
}), o);
|
|
38
|
+
}
|
|
39
|
+
function m(t = {}) {
|
|
40
|
+
return async (e) => {
|
|
41
|
+
if (await p(t), !window.MathJax?.typesetPromise) {
|
|
42
|
+
console.warn("[mathjax-renderer] MathJax not loaded or missing typesetPromise.");
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
await window.MathJax.typesetPromise([e]);
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function h() {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
let i = null;
|
|
52
|
+
function w() {
|
|
53
|
+
return i || (i = m({ accessibility: !0, useSingleDollar: !0 })), i;
|
|
54
|
+
}
|
|
55
|
+
const f = async (t) => {
|
|
56
|
+
if (typeof window > "u") return;
|
|
57
|
+
const e = typeof t == "string";
|
|
58
|
+
let r;
|
|
59
|
+
return e ? (r = document.createElement("div"), r.innerHTML = t) : r = t || document.body, await w()(r), e ? r.innerHTML : void 0;
|
|
60
|
+
}, x = (t) => t, M = (t) => {
|
|
61
|
+
const e = t.trim();
|
|
62
|
+
return e.startsWith("\\[") && e.endsWith("\\]") ? { unwrapped: e.slice(2, -2).trim(), wrapper: { open: "\\[", close: "\\]" } } : e.startsWith("\\(") && e.endsWith("\\)") ? { unwrapped: e.slice(2, -2).trim(), wrapper: { open: "\\(", close: "\\)" } } : e.startsWith("$$") && e.endsWith("$$") ? { unwrapped: e.slice(2, -2).trim(), wrapper: { open: "$$", close: "$$" } } : e.startsWith("$") && e.endsWith("$") ? { unwrapped: e.slice(1, -1).trim(), wrapper: { open: "$", close: "$" } } : { unwrapped: e };
|
|
63
|
+
}, y = (t) => t;
|
|
64
|
+
export {
|
|
65
|
+
m as createMathjaxRenderer,
|
|
66
|
+
h as getMathjaxCssUrls,
|
|
67
|
+
y as mmlToLatex,
|
|
68
|
+
f as renderMath,
|
|
69
|
+
M as unWrapMath,
|
|
70
|
+
x as wrapMath
|
|
71
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Legacy @pie-lib/math-rendering API for MathJax v4
|
|
3
|
+
*
|
|
4
|
+
* Provides backward compatibility with upstream pie-elements code.
|
|
5
|
+
* MathJax handles LaTeX and MathML natively, so most functions are simple.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Render math in a DOM element or HTML string using MathJax
|
|
9
|
+
*/
|
|
10
|
+
export declare const renderMath: (el?: Element | string) => Promise<string | undefined>;
|
|
11
|
+
/**
|
|
12
|
+
* Wrap LaTeX - MathJax handles delimiters, so just pass through
|
|
13
|
+
*/
|
|
14
|
+
export declare const wrapMath: (latex: string) => string;
|
|
15
|
+
/**
|
|
16
|
+
* Unwrap LaTeX delimiters - minimal implementation for editable-html-tip-tap
|
|
17
|
+
*/
|
|
18
|
+
export declare const unWrapMath: (latex: string) => {
|
|
19
|
+
unwrapped: string;
|
|
20
|
+
wrapper?: {
|
|
21
|
+
open: string;
|
|
22
|
+
close: string;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* MathML to LaTeX - MathJax renders MathML natively, so just pass through
|
|
27
|
+
*/
|
|
28
|
+
export declare const mmlToLatex: (mathml: string) => string;
|
|
29
|
+
//# sourceMappingURL=render-math.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render-math.d.ts","sourceRoot":"","sources":["../src/render-math.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAcH;;GAEG;AACH,eAAO,MAAM,UAAU,GAAU,KAAK,OAAO,GAAG,MAAM,KAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAgBlF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,MAAM,KAAG,MAAe,CAAC;AAEzD;;GAEG;AACH,eAAO,MAAM,UAAU,GACrB,OAAO,MAAM,KACZ;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAA;CAiBhE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,UAAU,GAAI,QAAQ,MAAM,KAAG,MAAgB,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for MathJax renderer
|
|
3
|
+
*/
|
|
4
|
+
export interface MathjaxOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Use single dollar signs as delimiters
|
|
7
|
+
* @default false
|
|
8
|
+
*/
|
|
9
|
+
useSingleDollar?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Enable accessibility features (speech, aria-labels)
|
|
12
|
+
* @default true
|
|
13
|
+
*/
|
|
14
|
+
accessibility?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Load MathJax fonts automatically
|
|
17
|
+
* @default true
|
|
18
|
+
*/
|
|
19
|
+
loadFonts?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Override the MathJax script URL.
|
|
22
|
+
*/
|
|
23
|
+
srcUrl?: string;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAE1B;;;OAGG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IAExB;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pie-element/shared-math-rendering-mathjax",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "MathJax adapter for PIE math rendering",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "rm -rf dist tsconfig.tsbuildinfo && vite build && tsc --emitDeclarationOnly",
|
|
19
|
+
"dev": "vite build --watch",
|
|
20
|
+
"test": "vitest",
|
|
21
|
+
"clean": "rm -rf dist"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"typescript": "^5.9.3",
|
|
26
|
+
"vite": "^7.2.2",
|
|
27
|
+
"vitest": "^4.0.17"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"pie",
|
|
34
|
+
"math",
|
|
35
|
+
"mathjax",
|
|
36
|
+
"rendering",
|
|
37
|
+
"latex"
|
|
38
|
+
],
|
|
39
|
+
"license": "MIT"
|
|
40
|
+
}
|