diff --git a/lib/main.js b/lib/main.js index 54955baf..7f9f9a10 100644 --- a/lib/main.js +++ b/lib/main.js @@ -195,6 +195,10 @@ function ConfigureMathJax() { AddError("SVG - Unknown character: U+"+message[1].toString(16).toUpperCase()+ " in "+(message[2].fonts||["unknown"]).join(","),!undefinedChar); }); + MathJax.Hub.Register.MessageHook("CommonHTML Jax - unknown char",function (message) { + AddError("CHTML - Unknown character: U+"+message[1].toString(16).toUpperCase()+ + " in "+(message[2].fonts||["unknown"]).join(","),!undefinedChar); + }); MathJax.Hub.Register.MessageHook("MathML Jax - unknown node type",function (message) { AddError("MathML - Unknown node type: "+message[1]); }); @@ -825,7 +829,9 @@ function RestartMathJax() { // %%% cache results? // %%% check types and values of parameters // -exports.typeset = function (data,callback) { + +// callback API for compatibility with MathJax +var cbTypeset = function (data, callback) { if (!callback || typeof(callback) !== "function") { if (displayErrors) console.error("Missing callback"); return; @@ -841,6 +847,17 @@ exports.typeset = function (data,callback) { if (serverState == STATE.READY) StartQueue(); } +// main API, callback and promise compatible +exports.typeset = function (data, callback) { + if (callback) cbTypeset(data, callback); + else return new Promise(function (resolve, reject) { + cbTypeset(data, function (output, input) { + if (output.errors) reject(output.errors); + else resolve(output, input); + }); + }); +}; + // // Manually start MathJax (this is done automatically // when the first typeset() call is made) diff --git a/test/base-typeset-promise.js b/test/base-typeset-promise.js new file mode 100644 index 00000000..ae67b2bd --- /dev/null +++ b/test/base-typeset-promise.js @@ -0,0 +1,22 @@ +var tape = require('tape'); +var mjAPI = require("../lib/main.js"); + +tape('basic test: check typeset promise API', function (t) { + t.plan(2); + + var tex = ''; + mjAPI.start(); + + // promise resolved + mjAPI.typeset({ + math: tex, + format: "TeX", + mml: true + }).then((result) => t.ok(result.mml, 'Typset promise resolved on success')); + + mjAPI.typeset({ + math: tex, + format: "MathML", + mml: true + }).catch((error) => t.ok(error, 'Typeset promise rejected on error')); +}); diff --git a/test/base-warnings.js b/test/base-warnings.js new file mode 100644 index 00000000..e4e7130a --- /dev/null +++ b/test/base-warnings.js @@ -0,0 +1,11 @@ +var tape = require('tape'); +var mjAPI = require("../lib/main.js"); +mjAPI.config({undefinedCharError: true}); + +tape('basic test: check warnings', function (t) { + t.plan(2); + mjAPI.typeset({math:'\u5475', html:true}) + .catch(errors => t.ok(errors, 'CommonHTML output reports error')); + mjAPI.typeset({math:'\u5475', svg:true}) + .catch(errors => t.ok(errors, 'SVG output reports error')); +});