-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-mathjax.js
More file actions
63 lines (60 loc) · 2.34 KB
/
Copy pathload-mathjax.js
File metadata and controls
63 lines (60 loc) · 2.34 KB
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
"use strict"
window.MathJax = mergeObj(
{
loader: {
load: ["[tex]/mathtools", "[tex]/amscd", "[tex]/physics"],
versionWarnings: false
},
tex: {
packages: { "[+]": ["mathtools", "amscd", "physics"] },
tags: "ams",
inlineMath: [["$", "$"], ["\\(", "\\)"]]
},
svg: { fontCache: "global" },
options: { ignoreHtmlClass: "mathjax_ignore" }
}
,
tryEval(localStorage.getItem("userCustomMathJax")?.replace(/;+\s*$/, ""))
)
loadScript("https://cdn.jsdelivr.net/npm/mathjax/es5/tex-svg-full.js").addEventListener("load", async () => {
await MathJax.startup.promise
const regexTex = /(?:\$|\\\(|\\\[|\\begin\{.*?})/
MathJax.__configMacros_tokens = /^\s*\\(def|(re)?new(command|environment)|DeclareMathOperator|let)\b/m
const textareaHandler = async () => {
if (mjx._forciblyHidden || typeof textarea.__preTest === "function" && textarea.__preTest() === false) return
textarea.__value ||= textarea.value
if (!(mjx._use || regexTex.test(textarea.__value))) return
mjx._use = true
mjx.hidden = ""
mjx.innerHTML = textarea.__value
.replace(/<(?!\s)(?=[^>]+\$)/g, "$& ")
.replace(/(\n{2,})| $\n(?!\n)/gm, (_, $1) => "<br>".repeat($1 ? $1.length : 1))
.replace(/(?<=^# .+\n(?!<br>))/gm, "<br>")
textarea.__value = ""
if (textareaHandler._executing) return
textareaHandler._executing = true
resetClear()
try { await MathJax.typesetPromise([mjx]) }
catch (e) {
resetClear()
if (e.message.endsWith("retry")) return "retry"
if (!MathJax.config._ignoreErrors.has(e.message)) console.error(e)
}
finally { delete textareaHandler._executing }
}
function resetClear() { MathJax.texReset(); MathJax.typesetClear() }
MathJax.startup.__domReady = (handlerName = "update") => {
textarea.addEventListener("input", textarea[handlerName] = throttle(textareaHandler))
}
MathJax.config._ignoreErrors = new Set([
// "this.parent(...) is null",
// "Command of variable renderer failed."
])
MathJax.config._menuSettings = function ({ renderer = "SVG" } = {}) {
const menuOptName = "MathJax-Menu-Settings"
const menuOpt = localStorage._getItem(menuOptName)
localStorage._setItem(menuOptName, Object.assign(menuOpt, { renderer }))
MathJax.config._ ??= {}
Object.entries(menuOpt).forEach(([k, v]) => MathJax.config._[k] = v)
}
})