UNPKG

mathjax-electron

Version:

A trimmed down version of the MathJax library for use with Electron and modern browsers

79 lines (69 loc) 2.3 kB
"use strict"; var path = require("path"); /** * Path to manually load configured MathJax */ var mathJaxPath = path.join(__dirname, "MathJax-2.7.5", "MathJax.js") + "?config=nteract"; /** * Loads and configures MathJax if necessary. * @param {Document} document - A Document Object Model. * The MathJax Script is included in the <head> section of the HTML document. * @param {Callback} callback - A callback to run when MathJax is loaded. */ function loadMathJax(document, callback) { if (typeof MathJax === "undefined" || MathJax === null) { var script = document.createElement("script"); if (typeof callback === "function") { script.addEventListener("load", function() { callback(); }); } script.type = "text/javascript"; try { script.src = mathJaxPath; document.getElementsByTagName("head")[0].appendChild(script); } catch (error) { throw new Error(error.message, "loadMathJax"); } } else { if (typeof callback === "function") callback(); } } /** * Typesets any math elements within the element. * @param {HTMLElement} container - The element whose math is to be typeset. * @param {Callback} callback - A callback to run when the typeset * is complete. */ function typesetMath(container, callback) { try { if (typeof callback === "function") { MathJax.Hub.Queue(["Typeset", MathJax.Hub, container], callback); } else { MathJax.Hub.Queue(["Typeset", MathJax.Hub, container]); } } catch (error) { throw new Error(error.message, "typesetMath"); } } /** * A helper function which loads MathJax if necessary and typesets any math * elements within the container. * @param {Document} document - A Document Object Model. * The MathJax Script is included in the <head> section of the HTML document. * @param {HTMLElement} container - The element whose math is to be typeset. * @param {Callback} callback - A callback to run when the typeset * is complete. */ function loadAndTypeset(document, container, callback) { loadMathJax(document, function() { typesetMath(container, callback); }); } module.exports = { mathJaxPath: mathJaxPath, loadMathJax: loadMathJax, typesetMath: typesetMath, loadAndTypeset: loadAndTypeset };