Skip to content

Commit 4d199e1

Browse files
committed
initial svg_text_utils refactor to use a better MathJax rendering approach, and migrate to v3/v4
1 parent d139ca8 commit 4d199e1

1 file changed

Lines changed: 31 additions & 97 deletions

File tree

‎src/lib/svg_text_utils.js‎

Lines changed: 31 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -188,67 +188,37 @@ function cleanEscapesForTex(s) {
188188
var inlineMath = [['$', '$'], ['\\(', '\\)']];
189189

190190
function texToSVG(_texString, _config, _callback) {
191-
var MathJaxVersion = parseInt(
191+
const MathJaxVersion = parseInt(
192192
(MathJax.version || '').split('.')[0]
193193
);
194194

195195
if(
196-
MathJaxVersion !== 2 &&
197-
MathJaxVersion !== 3
196+
MathJaxVersion !== 3 &&
197+
MathJaxVersion !== 4
198198
) {
199-
Lib.warn('No MathJax version:', MathJax.version);
199+
Lib.warn('Unsupported MathJax version:', MathJax.version);
200200
return;
201201
}
202202

203-
var originalRenderer,
204-
originalConfig,
205-
originalProcessSectionDelay,
203+
var originalConfig,
206204
tmpDiv;
207205

208-
var setConfig2 = function() {
209-
originalConfig = Lib.extendDeepAll({}, MathJax.Hub.config);
210-
211-
originalProcessSectionDelay = MathJax.Hub.processSectionDelay;
212-
if(MathJax.Hub.processSectionDelay !== undefined) {
213-
// MathJax 2.5+ but not 3+
214-
MathJax.Hub.processSectionDelay = 0;
215-
}
216-
217-
return MathJax.Hub.Config({
218-
messageStyle: 'none',
219-
tex2jax: {
220-
inlineMath: inlineMath
221-
},
222-
displayAlign: 'left',
223-
});
224-
};
225-
226-
var setConfig3 = function() {
206+
const setConfig = function() {
227207
originalConfig = Lib.extendDeepAll({}, MathJax.config);
228208

229209
if(!MathJax.config.tex) {
230210
MathJax.config.tex = {};
231211
}
232212

233213
MathJax.config.tex.inlineMath = inlineMath;
234-
};
235-
236-
var setRenderer2 = function() {
237-
originalRenderer = MathJax.Hub.config.menuSettings.renderer;
238-
if(originalRenderer !== 'SVG') {
239-
return MathJax.Hub.setRenderer('SVG');
240-
}
241-
};
242214

243-
var setRenderer3 = function() {
244-
originalRenderer = MathJax.config.startup.output;
245-
if(originalRenderer !== 'svg') {
215+
if(MathJax.config.startup.output !== 'svg') {
246216
MathJax.config.startup.output = 'svg';
247217
}
248218
};
249219

250-
var initiateMathJax = function() {
251-
var randomID = 'math-output-' + Lib.randstr({}, 64);
220+
const initiateMathJax = function() {
221+
const randomID = 'math-output-' + Lib.randstr({}, 64);
252222
tmpDiv = d3.select('body').append('div')
253223
.attr({id: randomID})
254224
.style({
@@ -258,81 +228,45 @@ function texToSVG(_texString, _config, _callback) {
258228
})
259229
.text(cleanEscapesForTex(_texString));
260230

261-
var tmpNode = tmpDiv.node();
231+
const tmpNode = tmpDiv.node();
262232

263-
return MathJaxVersion === 2 ?
264-
MathJax.Hub.Typeset(tmpNode) :
265-
MathJax.typeset([tmpNode]);
233+
return MathJax.typesetPromise([tmpNode]);
266234
};
267235

268-
var finalizeMathJax = function() {
269-
var sel = tmpDiv.select(
270-
MathJaxVersion === 2 ? '.MathJax_SVG' : '.MathJax'
271-
);
236+
const finalizeMathJax = function() {
237+
const sel = tmpDiv.select('.MathJax');
272238

273-
var node = !sel.empty() && tmpDiv.select('svg').node();
239+
const node = !sel.empty() && tmpDiv.select('svg').node();
274240
if(!node) {
275241
Lib.log('There was an error in the tex syntax.', _texString);
276242
_callback();
277243
} else {
278-
var nodeBBox = node.getBoundingClientRect();
279-
var glyphDefs;
280-
if(MathJaxVersion === 2) {
281-
glyphDefs = d3.select('body').select('#MathJax_SVG_glyphs');
282-
} else {
283-
glyphDefs = sel.select('defs');
284-
}
244+
const nodeBBox = node.getBoundingClientRect();
245+
const glyphDefs = sel.select('defs');
285246
_callback(sel, glyphDefs, nodeBBox);
286247
}
287248

288249
tmpDiv.remove();
289250
};
290251

291-
var resetRenderer2 = function() {
292-
if(originalRenderer !== 'SVG') {
293-
return MathJax.Hub.setRenderer(originalRenderer);
294-
}
295-
};
296-
297-
var resetRenderer3 = function() {
298-
if(originalRenderer !== 'svg') {
299-
MathJax.config.startup.output = originalRenderer;
300-
}
301-
};
302-
303-
var resetConfig2 = function() {
304-
if(originalProcessSectionDelay !== undefined) {
305-
MathJax.Hub.processSectionDelay = originalProcessSectionDelay;
306-
}
307-
return MathJax.Hub.Config(originalConfig);
308-
};
309-
310-
var resetConfig3 = function() {
252+
// Restore the original state of the global MathJax config we mutated above
253+
// This also restores the renderer to its original value
254+
const resetConfig = function() {
311255
MathJax.config = originalConfig;
312256
};
313257

314-
if(MathJaxVersion === 2) {
315-
MathJax.Hub.Queue(
316-
setConfig2,
317-
setRenderer2,
318-
initiateMathJax,
319-
finalizeMathJax,
320-
resetRenderer2,
321-
resetConfig2
322-
);
323-
} else if(MathJaxVersion === 3) {
324-
setConfig3();
325-
setRenderer3();
326-
MathJax.startup.defaultReady();
327-
328-
MathJax.startup.promise.then(function() {
329-
initiateMathJax();
330-
finalizeMathJax();
331-
332-
resetRenderer3();
333-
resetConfig3();
334-
});
335-
}
258+
// Set up MathJax, render tex, then clean up and return
259+
setConfig();
260+
MathJax.startup.defaultReady();
261+
MathJax.startup.promise
262+
.then(initiateMathJax)
263+
.then(finalizeMathJax)
264+
.catch((err) => {
265+
Lib.log('MathJax typesetting failed.', _texString, err);
266+
if(tmpDiv) tmpDiv.remove();
267+
_callback();
268+
})
269+
.then(resetConfig);
336270
}
337271

338272
var TAG_STYLES = {

0 commit comments

Comments
 (0)