-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
78 lines (69 loc) · 2.24 KB
/
index.js
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
"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
};