Skip to content

Commit de9ba5b

Browse files
ascholerChemeketarbeezer
authored andcommitted
HTML: add MathJax 4 configuration module (PR PreTeXtBook#2180)
1 parent 80f77ab commit de9ba5b

1 file changed

Lines changed: 156 additions & 0 deletions

File tree

js/mathjax_startup.js

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/***************************************************************
2+
* Implements startup of MathJax v4
3+
***************************************************************/
4+
5+
// Base config options. Will be supplemented by optional parts later
6+
let mathJaxOpts = {
7+
"tex": {
8+
"inlineMath": [
9+
[
10+
"\\(",
11+
"\\)"
12+
]
13+
],
14+
"tags": "none",
15+
"tagSide": "right",
16+
"tagIndent": ".8em",
17+
"packages": {
18+
"[+]": [
19+
"base",
20+
"ams",
21+
"amscd",
22+
"color",
23+
"newcommand",
24+
"knowl"
25+
]
26+
}
27+
},
28+
"options": {
29+
"ignoreHtmlClass": "tex2jax_ignore|ignore-math",
30+
"processHtmlClass": "process-math",
31+
},
32+
"chtml": {
33+
"scale": 0.98,
34+
"mtextInheritFont": true
35+
},
36+
"loader": {
37+
"load": [
38+
//"ui/lazy", // Vastly speeds up time to visible content. But breaks embedded knowl parsing
39+
"input/asciimath",
40+
//"[tex]/extpfeil", //remove backwards compatibility?
41+
"[tex]/amscd",
42+
"[tex]/color",
43+
"[tex]/newcommand",
44+
],
45+
"paths": {
46+
"pretext": "_static/pretext/js/lib"
47+
}
48+
}
49+
};
50+
51+
52+
export function startMathJax(opts) {
53+
if(opts.hasWebworkReps || opts.hasSage) {
54+
mathJaxOpts['renderActions'] = {
55+
"findScript": [
56+
10,
57+
function (doc) {
58+
document.querySelectorAll('script[type^="math/tex"]').forEach(function (node) {
59+
var display = !!node.type.match(/; *mode=display/);
60+
var math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
61+
var text = document.createTextNode('');
62+
node.parentNode.replaceChild(text, node);
63+
math.start = { node: text, delim: '', n: 0 };
64+
math.end = { node: text, delim: '', n: 0 };
65+
doc.math.push(math);
66+
});
67+
},
68+
""
69+
]
70+
}
71+
}
72+
73+
if(opts.isReact) {
74+
mathJaxOpts['startup'] = {
75+
typeset: false,
76+
}
77+
} else {
78+
mathJaxOpts['startup'] = {
79+
ready() {
80+
const { Configuration } = MathJax._.input.tex.Configuration;
81+
const configuration = Configuration.create("knowl", {
82+
handler: {
83+
macro: ["knowl"]
84+
}
85+
});
86+
87+
function GetArgumentMML(parser, name) {
88+
const NodeUtil = MathJax._.input.tex.NodeUtil.default;
89+
const arg = parser.ParseArg(name);
90+
if (!NodeUtil.isInferred(arg)) {
91+
return arg;
92+
}
93+
const children = NodeUtil.getChildren(arg);
94+
if (children.length === 1) {
95+
return children[0];
96+
}
97+
const mrow = parser.create("node", "mrow");
98+
NodeUtil.copyChildren(arg, mrow);
99+
NodeUtil.copyAttributes(arg, mrow);
100+
return mrow;
101+
};
102+
103+
let mathjaxKnowl = {};
104+
/**
105+
* Implements \knowl{url}{math}
106+
* @param {TexParser} parser The calling parser.
107+
* @param {string} name The TeX string
108+
*/
109+
mathjaxKnowl.Knowl = function (parser, name) {
110+
const url = parser.GetArgument(name);
111+
const arg = GetArgumentMML(parser, name);
112+
const mrow = parser.create("node", "mrow", [arg], { tabindex: '0', "data-knowl": url });
113+
parser.Push(mrow);
114+
};
115+
116+
const CommandMap = MathJax._.input.tex.TokenMap.CommandMap;
117+
new CommandMap(
118+
"knowl",
119+
{
120+
knowl: ["Knowl"]
121+
},
122+
mathjaxKnowl
123+
);
124+
125+
MathJax.startup.defaultReady();
126+
},
127+
pageReady() {
128+
return MathJax.startup.defaultPageReady().then(function () {
129+
rsMathReady();
130+
}
131+
)
132+
},
133+
}
134+
}
135+
136+
if(opts.htmlPresentation) {
137+
mathJaxOpts['options']['menuOptions'] = {
138+
"settings": {
139+
"zoom": "Click",
140+
"zscale": "300%"
141+
}
142+
}
143+
}
144+
145+
// Apply the options
146+
window.MathJax = mathJaxOpts;
147+
148+
// Lets Runestone know that MathJax is ready
149+
const runestoneMathReady = new Promise((resolve) => window.rsMathReady = resolve);
150+
window.runestoneMathReady = runestoneMathReady;
151+
}
152+
153+
154+
155+
156+

0 commit comments

Comments
 (0)