-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.jsx
145 lines (130 loc) · 3.82 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React, { useEffect, useState, useRef } from "react";
import PropTypes from "prop-types";
import DOMPurify from "dompurify";
const baseConfig = {
showMathMenu: true,
tex2jax: {
inlineMath: [
["$", "$"],
["\\(", "\\)"],
],
},
skipStartupTypeset: true,
};
const defaultSanitizeOptions = {
USE_PROFILES: {mathMl: true},
ADD_ATTR: ['columnalign'],
}
const MathJaxPreview = React.forwardRef(({
script,
config,
className,
math,
id,
style,
wrapperTag,
msDelayDisplay, //milliseconds to delay display of div, 300 is default
onDisplay,
onLoad,
onError,
sanitizeOptions,
}, ref) => {
const sanitizedMath = DOMPurify.sanitize(math, {...defaultSanitizeOptions, ...sanitizeOptions});
const previewRef = useRef();
const [display, setDisplay] = useState("none"); //prevent display during processing
const [loadingState, setLoadingState] = useState(
window.MathJax ? "loaded" : "loading"
);
useEffect(() => {
let mathjaxScriptTag = document.querySelector(`script[src="${script}"]`);
if (!mathjaxScriptTag) {
mathjaxScriptTag = document.createElement("script");
mathjaxScriptTag.async = true;
mathjaxScriptTag.src = script;
for (const [k, v] of Object.entries(config || {})) {
mathjaxScriptTag.setAttribute(k, v);
}
const node = document.head || document.getElementsByTagName("head")[0];
node.appendChild(mathjaxScriptTag);
}
const onloadHandler = () => {
setLoadingState("loaded");
window.MathJax.Hub.Config({ ...baseConfig, ...config });
onLoad && onLoad();
};
const onerrorHandler = () => {
setLoadingState("failed");
onError && onError();
};
mathjaxScriptTag.addEventListener("load", onloadHandler);
mathjaxScriptTag.addEventListener("error", onerrorHandler);
return () => {
mathjaxScriptTag.removeEventListener("load", onloadHandler);
mathjaxScriptTag.removeEventListener("error", onloadHandler);
};
}, [setLoadingState, config, baseConfig]);
useEffect(() => {
if (loadingState !== "loaded") {
return;
}
previewRef.current.innerHTML = sanitizedMath;
window.MathJax.Hub.Queue([
"Typeset",
window.MathJax.Hub,
previewRef.current,
]);
//delay display of div
var msDelay;
if ( //msDelayDisplay prop is a reasonable number of ms
msDelayDisplay !== null &&
!isNaN(+msDelayDisplay) &&
+msDelayDisplay >= 0 &&
+msDelayDisplay < 10000
) {
msDelay = +msDelayDisplay;
} else {
msDelay = 300; // default 300ms delay
}
const timeout = setTimeout(() => {
setDisplay("inline"); //display div after delay, hopefully typeset has finished
onDisplay && onDisplay();
}, msDelay);
return () => {
setDisplay("none");
clearTimeout(timeout);
};
}, [sanitizedMath, loadingState, previewRef]);
return React.createElement(
wrapperTag,
{ className, id, style: { display, ...style }, ref },
<>
{loadingState === "failed" && <span>fail loading mathjax lib</span>}
{React.createElement(wrapperTag, {
className: "react-mathjax-preview-result",
ref: previewRef,
})}
</>
);
});
MathJaxPreview.displayName = 'MathJaxPreview';
MathJaxPreview.propTypes = {
script: PropTypes.string,
config: PropTypes.object,
className: PropTypes.string,
math: PropTypes.string,
style: PropTypes.object,
wrapperTag: PropTypes.string,
id: PropTypes.string,
onLoad: PropTypes.func,
onError: PropTypes.func,
onDisplay: PropTypes.func,
sanitizeOptions: PropTypes.object,
};
MathJaxPreview.defaultProps = {
script:
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.6/MathJax.js?config=TeX-MML-AM_HTMLorMML",
id: "react-mathjax-preview",
sanitizeOptions: {},
wrapperTag: "div",
};
export default MathJaxPreview;