forked from apendua/meteor-mathjax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathjax.js
78 lines (69 loc) · 2.31 KB
/
mathjax.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
var cache = {};
Template.registerHelper('mathjax', function () {
var options = this,
wait = options.wait !== undefined ? options.wait : false;
var update = function (firstNode, lastNode) {
var alreadyThere = false;
$(firstNode).parent().contents().each(function (index, node) {
// TODO add support for text nodes
var cacheKey;
if (node === firstNode) {
alreadyThere = true;
}
if (alreadyThere && this.nodeType === 1) {
cacheKey = node.innerHTML;
if (cache[cacheKey]) {
node.innerHTML = cache[cacheKey];
} else {
MathJax.Hub.Queue(["Typeset", MathJax.Hub, this], function () {
cache[cacheKey] = node.innerHTML;
});
}
}
return this !== lastNode;
});
};
var mathjax = new Template('mathjax', function () {
var view = this, conent = '';
if (view.templateContentBlock) {
content = HTML.toText(Blaze._expandView(Blaze._TemplateWith(Template.parentData(),
view.templateContentBlock.renderFunction)), HTML.TEXTMODE.STRING);
}
return HTML.Raw(content);
});
mathjax.onRendered(function () {
var self = this;
//---------------------------------
onMathJaxReady(function (MathJax) {
if (!wait) {
Meteor.defer(function () { update(self.firstNode, self.lastNode); });
} else {
update(self.firstNode, self.lastNode);
}
}); // ready
});
return mathjax;
});
// loading MathJax
function onMathJaxReady(callback) {
if (window.MathJax) {
callback(window.MathJax);
} else {
if (!onMathJaxReady.listeners) {
$.getScript( // TODO: let the user change the source
'https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
).done(function () {
//------------------
MathJax.Hub.Config({
skipStartupTypeset: true,
showProcessingMessages: false,
tex2jax: { inlineMath: [['$','$'],['\\(','\\)']] }
});
//-------------------------------------------------------------------------------------------------------
while (onMathJaxReady.listeners.length > 0) { onMathJaxReady.listeners.pop().call(null, window.MathJax) }
});
onMathJaxReady.listeners = [];
}
onMathJaxReady.listeners.push(callback);
}
}