Skip to content

Commit

Permalink
Allow macros to be persisted using state
Browse files Browse the repository at this point in the history
  • Loading branch information
rianmcguire committed Aug 3, 2017
1 parent f4f2296 commit cb238ef
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,12 +742,18 @@ function GetState(state) {
MathJax.OutputJax.CommonHTML.ID = 0;
}

// Clear any existing user defined macros
// Clear any existing user defined macros, then load macros from state
Object.keys(TEX.Definitions.macros).forEach(function(macroName){
if (TEX.Definitions.macros[macroName].isUser) {
delete TEX.Definitions.macros[macroName];
}
});
if (state && state.macros) {
for (var macroName in state.macros) {
TEX.Definitions.macros[macroName] = state.macros[macroName];
TEX.Definitions.macros[macroName].isUser = true;
}
}
}

//
Expand All @@ -772,6 +778,7 @@ function ReturnResult(result) {
if (state) {
var AMS = MathJax.Extension["TeX/AMSmath"];
var GLYPH = MathJax.OutputJax.SVG.BBOX.GLYPH;
var TEX = MathJax.InputJax.TeX;
state.AMS.startNumber = AMS.startNumber;
state.AMS.labels = AMS.labels;
state.AMS.IDs = AMS.IDs;
Expand All @@ -780,6 +787,13 @@ function ReturnResult(result) {
state.defs = GLYPH.defs;
state.n = GLYPH.n;
state.ID = ID;
state.macros = {};
for (var macroName in TEX.Definitions.macros) {
var macro = TEX.Definitions.macros[macroName];
if (macro.isUser) {
state.macros[macroName] = macro;
}
}
}
serverState = STATE.READY;
callback(result, originalData);
Expand Down
37 changes: 37 additions & 0 deletions test/macro-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,40 @@ tape('macros should be cleared from global state', function(t) {
t.true(data.errors, '\\mymacro should no longer be defined');
});
});

tape('macros can be persisted using state option', function(t) {
t.plan(4);
mjAPI.start();

var state = {};

mjAPI.typeset({
math: "\\def\\mymacro{2\\pi} \\mymacro",
format: "TeX",
mml: true,
state: state
}, function(data) {
t.false(data.errors, 'Defined and used macro');
t.equal(Object.keys(state.macros).length, 1, 'Only stores the user defined macro in state');

// Ensure state contains only serializable data
state = JSON.parse(JSON.stringify(state));

mjAPI.typeset({
math: "\\mymacro",
format: "TeX",
mml: true,
state: state
}, function(data) {
t.false(data.errors, '\\mymacro was not persisted in state');
});

mjAPI.typeset({
math: "\\mymacro",
format: "TeX",
mml: true,
}, function(data) {
t.true(data.errors, '\\mymacro should no longer be defined if state is not provided');
});
});
});

0 comments on commit cb238ef

Please sign in to comment.