88 lines
2.3 KiB
JavaScript
88 lines
2.3 KiB
JavaScript
(() => {
|
|
const rendererUrl = new URL(
|
|
"../assets/vendor/mathjax/tex-mml-chtml.js",
|
|
document.currentScript.src
|
|
);
|
|
const assetRoot = new URL(".", rendererUrl).href.replace(/\/$/, "");
|
|
let loading;
|
|
let revision = 0;
|
|
let rendering = Promise.resolve();
|
|
|
|
function loadMathJax() {
|
|
if (loading) {
|
|
return loading;
|
|
}
|
|
|
|
window.MathJax = {
|
|
loader: {
|
|
paths: {
|
|
mathjax: assetRoot,
|
|
sre: `${assetRoot}/sre/mathmaps`
|
|
}
|
|
},
|
|
tex: {
|
|
inlineMath: [["\\(", "\\)"]],
|
|
displayMath: [["\\[", "\\]"]],
|
|
processEscapes: true,
|
|
processEnvironments: true
|
|
},
|
|
options: {
|
|
ignoreHtmlClass: ".*|",
|
|
processHtmlClass: "arithmatex"
|
|
},
|
|
chtml: {
|
|
fontURL: `${assetRoot}/output/chtml/fonts/woff-v2`
|
|
},
|
|
startup: {
|
|
typeset: false
|
|
}
|
|
};
|
|
|
|
loading = new Promise((resolve, reject) => {
|
|
const script = document.createElement("script");
|
|
script.src = rendererUrl.href;
|
|
script.async = true;
|
|
script.onload = () => {
|
|
window.MathJax.startup.promise.then(() => resolve(window.MathJax), reject);
|
|
};
|
|
script.onerror = () => {
|
|
loading = undefined;
|
|
script.remove();
|
|
reject(new Error(`Failed to load MathJax from ${rendererUrl.href}`));
|
|
};
|
|
document.head.append(script);
|
|
});
|
|
return loading;
|
|
}
|
|
|
|
document$.subscribe(() => {
|
|
const currentRevision = ++revision;
|
|
|
|
// Serialize rendering and discard navigation events superseded during loading.
|
|
rendering = rendering.then(async () => {
|
|
if (currentRevision !== revision) {
|
|
return;
|
|
}
|
|
const content = document.querySelector(".md-content");
|
|
if (!content || !content.querySelector(".arithmatex")) {
|
|
if (loading) {
|
|
const mathjax = await loading;
|
|
mathjax.typesetClear();
|
|
}
|
|
return;
|
|
}
|
|
|
|
const mathjax = await loadMathJax();
|
|
if (currentRevision !== revision) {
|
|
return;
|
|
}
|
|
mathjax.startup.output.clearCache();
|
|
mathjax.typesetClear();
|
|
mathjax.texReset();
|
|
await mathjax.typesetPromise([content]);
|
|
}).catch(error => {
|
|
console.error("Jupiter Notes: unable to render mathematics.", error);
|
|
});
|
|
});
|
|
})();
|