mathjax / MathJax-node

MathJax for Node
Apache License 2.0
614 stars 96 forks source link

setting state.ID does not affect first typeset() call #426

Closed pkra closed 5 years ago

pkra commented 6 years ago

The following seems like a bug:

var mjAPI = require("./lib/main.js");
var yourMath = `x`;
var state = {};
state.ID = 5;
mjAPI.start();
mjAPI.typeset({
  useGlobalCache: true,
  math: yourMath,
  state: state,
  format: "TeX",
  svg:true
}, function (data) {
  console.log(state.ID, data.svg); // 1 , <svg ... aria-labelledby="MathJax-SVG-0-Title" ... >
  state.ID = 30;
});
 mjAPI.typeset({
    useGlobalCache: true,
    math: yourMath,
    state: state,
    format: "TeX", // "inline-TeX", "MathML"
    svg:true
  }, function (data) {
    console.log(state.ID, data.svg); // 31, <svg ... aria-labelledby="MathJax-SVG-30-Title" ... >
   });
dpvc commented 5 years ago

The state property is not intended to be configurable itself. It is expected to be one of two values: either a blank object {}, or a state object from a previous call. It represents internal state data that mathjax-node maintains itself, not configuration parameters that you set. So setting state.ID as you have done is not a supported (indeed, you are not supposed to be aware of the structure of the state itself, as it is internal data that could change between versions).

The reason your setting it doesn't affect the first call is that the state is only used to initialize mathjax-node if it is a "complete" state (as opposed to an empty state). Mathjax-node uses the presence of the state.AMS sub-object to decide if the state is already initialized, and since your state doesn't have that, it is considered to be a blank state and is not used initially (it is only used to store the state as it is after the typeset call is made). So your initial value of ID is not used, as you have seen. In order to to get your ID value to be used, you would need to provide a complete state object, including all the values it holds.

I think the correct solution is to provide an actual configuration property for the ID, as you suggest in #427.

pkra commented 5 years ago

I'm ok with implementing an option for the IDs. But...

you are not supposed to be aware of the structure of the state itself,

The state is documented at https://github.com/mathjax/MathJax-node#promiseresolveresultoptions--promiserejecterrors--callbackresult-options, so I would have expected that part to be stable. If it's not, then the readme should reflect which parts are. For example, I think we definitely need to know where to find the glyphs if useGlobalCache is used.

pkra commented 5 years ago

duplicate from above

pkra commented 5 years ago

Sorry for the double post -- Firefox remembers the comment when re-opening a closed tab.