Skip to content

Commit 7677258

Browse files
committed
refactor functions for v2 and v3 and strict conditions
1 parent 1de176c commit 7677258

File tree

1 file changed

+71
-68
lines changed

1 file changed

+71
-68
lines changed

src/lib/svg_text_utils.js

Lines changed: 71 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,14 @@ var inlineMath = [['$', '$'], ['\\(', '\\)']];
179179
function texToSVG(_texString, _config, _callback) {
180180
var MathJaxVersion = parseInt(
181181
(MathJax.version || '').split('.')[0]
182-
) || -1;
182+
);
183183

184-
if(!MathJaxVersion) {
184+
if(
185+
MathJaxVersion !== 2 &&
186+
MathJaxVersion !== 3
187+
) {
185188
Lib.warn('No MathJax version:', MathJax.version);
189+
return;
186190
}
187191

188192
var originalRenderer,
@@ -191,45 +195,44 @@ function texToSVG(_texString, _config, _callback) {
191195
noOriginalInlineMath,
192196
tmpDiv;
193197

194-
var setConfig = function() {
195-
if(MathJaxVersion < 3) {
196-
originalConfig = Lib.extendDeepAll({}, MathJax.Hub.config);
198+
var setConfig2 = function() {
199+
originalConfig = Lib.extendDeepAll({}, MathJax.Hub.config);
197200

198-
originalProcessSectionDelay = MathJax.Hub.processSectionDelay;
199-
if(MathJax.Hub.processSectionDelay !== undefined) {
200-
// MathJax 2.5+ but not 3+
201-
MathJax.Hub.processSectionDelay = 0;
202-
}
201+
originalProcessSectionDelay = MathJax.Hub.processSectionDelay;
202+
if(MathJax.Hub.processSectionDelay !== undefined) {
203+
// MathJax 2.5+ but not 3+
204+
MathJax.Hub.processSectionDelay = 0;
205+
}
203206

204-
return MathJax.Hub.Config({
205-
messageStyle: 'none',
206-
tex2jax: {
207-
inlineMath: inlineMath
208-
},
209-
displayAlign: 'left',
210-
});
211-
} else {
212-
originalConfig = Lib.extendDeepAll({}, MathJax.config);
207+
return MathJax.Hub.Config({
208+
messageStyle: 'none',
209+
tex2jax: {
210+
inlineMath: inlineMath
211+
},
212+
displayAlign: 'left',
213+
});
214+
};
213215

214-
if(!MathJax.config.tex.inlineMath) {
215-
MathJax.config.tex.inlineMath = inlineMath;
216-
noOriginalInlineMath = true;
217-
}
216+
var setConfig3 = function() {
217+
originalConfig = Lib.extendDeepAll({}, MathJax.config);
218+
219+
if(!MathJax.config.tex.inlineMath) {
220+
MathJax.config.tex.inlineMath = inlineMath;
221+
noOriginalInlineMath = true;
218222
}
219223
};
220224

221-
var setRenderer = function() {
222-
// Get original renderer
223-
if(MathJaxVersion < 3) {
224-
originalRenderer = MathJax.Hub.config.menuSettings.renderer;
225-
if(originalRenderer !== 'SVG') {
226-
return MathJax.Hub.setRenderer('SVG');
227-
}
228-
} else {
229-
originalRenderer = MathJax.config.startup.output;
230-
if(originalRenderer !== 'svg') {
231-
MathJax.config.startup.output = 'svg';
232-
}
225+
var setRenderer2 = function() {
226+
originalRenderer = MathJax.Hub.config.menuSettings.renderer;
227+
if(originalRenderer !== 'SVG') {
228+
return MathJax.Hub.setRenderer('SVG');
229+
}
230+
};
231+
232+
var setRenderer3 = function() {
233+
originalRenderer = MathJax.config.startup.output;
234+
if(originalRenderer !== 'svg') {
235+
MathJax.config.startup.output = 'svg';
233236
}
234237
};
235238

@@ -246,14 +249,14 @@ function texToSVG(_texString, _config, _callback) {
246249

247250
var tmpNode = tmpDiv.node();
248251

249-
return MathJaxVersion < 3 ?
252+
return MathJaxVersion === 2 ?
250253
MathJax.Hub.Typeset(tmpNode) :
251254
MathJax.typeset([tmpNode]);
252255
};
253256

254257
var finalizeMathJax = function() {
255258
var sel = tmpDiv.select(
256-
MathJaxVersion < 3 ? '.MathJax_SVG' : '.MathJax'
259+
MathJaxVersion === 2 ? '.MathJax_SVG' : '.MathJax'
257260
);
258261

259262
var node = !sel.empty() && tmpDiv.select('svg').node();
@@ -263,7 +266,7 @@ function texToSVG(_texString, _config, _callback) {
263266
} else {
264267
var nodeBBox = node.getBoundingClientRect();
265268
var glyphDefs;
266-
if(MathJaxVersion < 3) {
269+
if(MathJaxVersion === 2) {
267270
glyphDefs = d3.select('body').select('#MathJax_SVG_glyphs');
268271
} else {
269272
glyphDefs = sel.select('defs');
@@ -274,53 +277,53 @@ function texToSVG(_texString, _config, _callback) {
274277
tmpDiv.remove();
275278
};
276279

277-
var resetRenderer = function() {
278-
if(MathJaxVersion < 3) {
279-
if(originalRenderer !== 'SVG') {
280-
return MathJax.Hub.setRenderer(originalRenderer);
281-
}
282-
} else {
283-
if(originalRenderer !== 'svg') {
284-
MathJax.config.startup.output = originalRenderer;
285-
}
280+
var resetRenderer2 = function() {
281+
if(originalRenderer !== 'SVG') {
282+
return MathJax.Hub.setRenderer(originalRenderer);
286283
}
287284
};
288285

289-
var resetConfig = function() {
290-
if(MathJaxVersion < 3) {
291-
if(originalProcessSectionDelay !== undefined) {
292-
MathJax.Hub.processSectionDelay = originalProcessSectionDelay;
293-
}
294-
return MathJax.Hub.Config(originalConfig);
286+
var resetRenderer3 = function() {
287+
if(originalRenderer !== 'svg') {
288+
MathJax.config.startup.output = originalRenderer;
289+
}
290+
};
291+
292+
var resetConfig2 = function() {
293+
if(originalProcessSectionDelay !== undefined) {
294+
MathJax.Hub.processSectionDelay = originalProcessSectionDelay;
295+
}
296+
return MathJax.Hub.Config(originalConfig);
297+
};
298+
299+
var resetConfig3 = function() {
300+
if(noOriginalInlineMath) {
301+
delete MathJax.config.tex.inlineMath;
295302
} else {
296-
if(noOriginalInlineMath) {
297-
delete MathJax.config.tex.inlineMath;
298-
} else {
299-
MathJax.config.tex.inlineMath = originalConfig.tex.inlineMath;
300-
}
303+
MathJax.config.tex.inlineMath = originalConfig.tex.inlineMath;
301304
}
302305
};
303306

304-
if(MathJaxVersion < 3) {
307+
if(MathJaxVersion === 2) {
305308
MathJax.Hub.Queue(
306-
setConfig,
307-
setRenderer,
309+
setConfig2,
310+
setRenderer2,
308311
initiateMathJax,
309312
finalizeMathJax,
310-
resetRenderer,
311-
resetConfig
313+
resetRenderer2,
314+
resetConfig2
312315
);
313-
} else {
314-
setConfig();
315-
setRenderer();
316+
} else if(MathJaxVersion === 3) {
317+
setConfig3();
318+
setRenderer3();
316319
MathJax.startup.defaultReady();
317320

318321
MathJax.startup.promise.then(function() {
319322
initiateMathJax();
320323
finalizeMathJax();
321324

322-
resetRenderer();
323-
resetConfig();
325+
resetRenderer3();
326+
resetConfig3();
324327
});
325328
}
326329
}

0 commit comments

Comments
 (0)