Skip to content

Commit cb238ef

Browse files
committed
Allow macros to be persisted using state
1 parent f4f2296 commit cb238ef

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

lib/main.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -742,12 +742,18 @@ function GetState(state) {
742742
MathJax.OutputJax.CommonHTML.ID = 0;
743743
}
744744

745-
// Clear any existing user defined macros
745+
// Clear any existing user defined macros, then load macros from state
746746
Object.keys(TEX.Definitions.macros).forEach(function(macroName){
747747
if (TEX.Definitions.macros[macroName].isUser) {
748748
delete TEX.Definitions.macros[macroName];
749749
}
750750
});
751+
if (state && state.macros) {
752+
for (var macroName in state.macros) {
753+
TEX.Definitions.macros[macroName] = state.macros[macroName];
754+
TEX.Definitions.macros[macroName].isUser = true;
755+
}
756+
}
751757
}
752758

753759
//
@@ -772,6 +778,7 @@ function ReturnResult(result) {
772778
if (state) {
773779
var AMS = MathJax.Extension["TeX/AMSmath"];
774780
var GLYPH = MathJax.OutputJax.SVG.BBOX.GLYPH;
781+
var TEX = MathJax.InputJax.TeX;
775782
state.AMS.startNumber = AMS.startNumber;
776783
state.AMS.labels = AMS.labels;
777784
state.AMS.IDs = AMS.IDs;
@@ -780,6 +787,13 @@ function ReturnResult(result) {
780787
state.defs = GLYPH.defs;
781788
state.n = GLYPH.n;
782789
state.ID = ID;
790+
state.macros = {};
791+
for (var macroName in TEX.Definitions.macros) {
792+
var macro = TEX.Definitions.macros[macroName];
793+
if (macro.isUser) {
794+
state.macros[macroName] = macro;
795+
}
796+
}
783797
}
784798
serverState = STATE.READY;
785799
callback(result, originalData);

test/macro-state.js

+37
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,40 @@ tape('macros should be cleared from global state', function(t) {
2121
t.true(data.errors, '\\mymacro should no longer be defined');
2222
});
2323
});
24+
25+
tape('macros can be persisted using state option', function(t) {
26+
t.plan(4);
27+
mjAPI.start();
28+
29+
var state = {};
30+
31+
mjAPI.typeset({
32+
math: "\\def\\mymacro{2\\pi} \\mymacro",
33+
format: "TeX",
34+
mml: true,
35+
state: state
36+
}, function(data) {
37+
t.false(data.errors, 'Defined and used macro');
38+
t.equal(Object.keys(state.macros).length, 1, 'Only stores the user defined macro in state');
39+
40+
// Ensure state contains only serializable data
41+
state = JSON.parse(JSON.stringify(state));
42+
43+
mjAPI.typeset({
44+
math: "\\mymacro",
45+
format: "TeX",
46+
mml: true,
47+
state: state
48+
}, function(data) {
49+
t.false(data.errors, '\\mymacro was not persisted in state');
50+
});
51+
52+
mjAPI.typeset({
53+
math: "\\mymacro",
54+
format: "TeX",
55+
mml: true,
56+
}, function(data) {
57+
t.true(data.errors, '\\mymacro should no longer be defined if state is not provided');
58+
});
59+
});
60+
});

0 commit comments

Comments
 (0)