discourse-math/assets/javascripts/initializers/discourse-math-mathjax.js

150 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-08-08 11:58:37 +10:00
import { withPluginApi } from "discourse/lib/plugin-api";
2020-07-06 14:49:01 -04:00
import { getURLWithCDN } from "discourse-common/lib/get-url";
2018-08-08 11:58:37 +10:00
import loadScript from "discourse/lib/load-script";
import { later } from "@ember/runloop";
2017-07-06 16:50:38 -04:00
let initializedMathJax = false;
function initMathJax(opts) {
2018-08-08 11:58:37 +10:00
if (initializedMathJax) {
return;
}
2017-07-06 16:50:38 -04:00
const extensions = ["toMathML.js", "Safe.js"];
2017-11-20 15:06:24 +11:00
if (opts.enable_accessibility) {
2017-08-02 18:18:43 +09:00
extensions.push("[a11y]/accessibility-menu.js");
}
2017-08-02 18:18:43 +09:00
2021-01-27 10:36:08 +01:00
let settings = {
2018-08-08 11:58:37 +10:00
jax: ["input/TeX", "input/AsciiMath", "input/MathML", "output/CommonHTML"],
TeX: { extensions: ["AMSmath.js", "AMSsymbols.js", "autoload-all.js"] },
extensions,
2017-07-06 16:50:38 -04:00
showProcessingMessages: false,
2020-09-04 13:22:43 +02:00
root: getURLWithCDN("/plugins/discourse-math/mathjax"),
2017-08-02 18:18:43 +09:00
};
2018-08-08 11:58:37 +10:00
if (opts.zoom_on_hover) {
settings.menuSettings = { zoom: "Hover" };
settings.MathEvents = { hover: 750 };
}
window.MathJax = settings;
2017-07-06 16:50:38 -04:00
initializedMathJax = true;
}
2018-08-08 11:58:37 +10:00
function ensureMathJax(opts) {
initMathJax(opts);
2018-08-08 11:58:37 +10:00
return loadScript("/plugins/discourse-math/mathjax/MathJax.2.7.5.js");
2017-07-06 16:50:38 -04:00
}
2018-08-08 11:58:37 +10:00
function decorate(elem, isPreview) {
if (elem.dataset.appliedMathjax) {
2017-07-06 17:15:37 -04:00
return;
}
2018-08-08 11:58:37 +10:00
elem.dataset.appliedMathjax = true;
let tag, classList, type;
2017-07-06 17:15:37 -04:00
if (elem.classList.contains("math")) {
tag = elem.tagName === "DIV" ? "div" : "span";
const display = tag === "div" ? "; mode=display" : "";
const displayClass = tag === "div" ? "block-math" : "inline-math";
type = `math/tex${display}`;
classList = `math-container ${displayClass} mathjax-math`;
} else if (elem.classList.contains("asciimath")) {
tag = "span";
classList = "math-container inline-math ascii-math";
type = "math/asciimath";
}
2017-07-06 16:50:38 -04:00
const mathScript = document.createElement("script");
mathScript.type = type;
mathScript.innerText = elem.innerText;
const mathWrapper = document.createElement(tag);
mathWrapper.classList.add(classList.split(" "));
mathWrapper.style.display = "none";
mathWrapper.appendChild(mathScript);
elem.after(mathWrapper);
later(
2018-08-08 11:58:37 +10:00
this,
() => {
window.MathJax.Hub.Queue(() => {
// don't bother processing previews removed from DOM
if (elem?.parentElement?.offsetParent !== null) {
window.MathJax.Hub.Typeset(mathScript, () => {
elem.style.display = "none";
mathWrapper.style.display = "block";
2018-08-08 11:58:37 +10:00
});
}
});
},
isPreview ? 200 : 0
);
2017-07-06 16:50:38 -04:00
}
function mathjax(elem, opts) {
if (!elem) {
2017-11-20 15:06:24 +11:00
return;
}
let mathElems;
2018-08-08 11:58:37 +10:00
if (opts.enable_asciimath) {
mathElems = elem.querySelectorAll(".math, .asciimath");
2018-08-08 11:58:37 +10:00
} else {
mathElems = elem.querySelectorAll(".math");
}
2017-07-06 16:50:38 -04:00
if (mathElems.length > 0) {
const isPreview = elem.classList.contains("d-editor-preview");
2017-07-06 17:15:37 -04:00
2018-08-08 11:58:37 +10:00
ensureMathJax(opts).then(() => {
mathElems.forEach((mathElem) => decorate(mathElem, isPreview));
2017-07-06 16:50:38 -04:00
});
}
}
function initializeMath(api, discourseMathOptions) {
api.decorateCookedElement(
(element) => {
mathjax(element, discourseMathOptions);
2019-06-20 10:14:57 -04:00
},
{ id: "mathjax" }
);
if (api.decorateChatMessage) {
api.decorateChatMessage(
(element) => {
mathjax(element, discourseMathOptions);
},
{
id: "mathjax-chat",
}
);
}
2017-07-06 16:50:38 -04:00
}
export default {
name: "apply-math-mathjax",
2017-07-06 16:50:38 -04:00
initialize(container) {
2018-08-08 11:58:37 +10:00
const siteSettings = container.lookup("site-settings:main");
let discourse_math_opts = {
zoom_on_hover: siteSettings.discourse_math_zoom_on_hover,
enable_accessibility: siteSettings.discourse_math_enable_accessibility,
2020-09-04 13:22:43 +02:00
enable_asciimath: siteSettings.discourse_math_enable_asciimath,
};
2019-06-14 13:28:18 -04:00
if (
siteSettings.discourse_math_enabled &&
siteSettings.discourse_math_provider === "mathjax"
) {
2020-09-04 13:22:43 +02:00
withPluginApi("0.5", function (api) {
2018-08-08 11:58:37 +10:00
initializeMath(api, discourse_math_opts);
});
2017-07-06 16:50:38 -04:00
}
2020-09-04 13:22:43 +02:00
},
2017-07-06 16:50:38 -04:00
};