-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[main] typeset: add promise-based API #347
Conversation
if (callback) cbTypeset(data, callback); | ||
else return new Promise(function (resolve, reject) { | ||
cbTypeset(data, function (output, input) { | ||
if (output.errors) reject(output.errors); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to be worried about the fact that output.errors
an include warnings (like missing characters)? There is a configuration option for turning this off, so it may not be necessary to worry about that here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have said no since undefinedCharError
is false by default (and if you switch it on, you likely want to reject the promise).
However, I have noticed that switching it to true
has no effect for the promise-based API, e.g.,
var mjAPI = require("./lib/main.js");
mjAPI.config({undefinedCharError: true});
mjAPI.typeset({
math: "呵呵",
format: "TeX",
htmlNode: true,
html: true,
svg: true,
equationNumbers: "AMS"
}, function (data) {
console.log(data.errors); // has errors
});
mjAPI.typeset({math:'呵呵 ', html:true})
.then((output, input) => console.log(output))
.catch(errors => console.log("Errors:",errors)); //does not have errors
I don't understand what's going on here. (Hard errors work fine by the way, e.g., in the test file.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem turns out to be that the CHTML undefined character signals are not being trapped — only the SVG ones. So your first typeset()
call shows them because it has svg: true
, but the second doesn't, because it is only for html.
You can fix this by adding
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);
});
just after the corresponding SVG version at around line 198.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have said no since
undefinedCharError
is false by default
I thought it was true, but I see that you are right, so I agree, no need to do anything about it. But at least we did find out that the CHTML undefined character signals aren't currently being trapped. :-)
@dpvc PTAL |
test/base-warnings.js
Outdated
|
||
tape('basic test: check warnings', function (t) { | ||
t.plan(2); | ||
mjAPI.typeset({math:'呵', html:true}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to see the math character as a unicode reference ('\u5475
) rather than rely on the proper encoding of the javascript file.
test/base-warnings.js
Outdated
t.plan(2); | ||
mjAPI.typeset({math:'呵', html:true}) | ||
.catch(errors => t.ok(errors, 'CommonHTML output reports error')); | ||
mjAPI.typeset({math:'呵', svg:true}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One might argue that this won't be a problem on engines that understand promises but I'm happy to make the change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
edited the above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because you can't specify the encoding used in a javascript source file, it is bad practice to use raw unicode (I assume this is UTF-8, but there are other unicode encodings). For example, once of the editors I use will get this wrong, and were I to save it, the characters would change.
Were this to be loaded into a web page (and I know that it won't be), the javascript would be loaded (even in modern browsers, last time I checked) using the encoding of the web page itself, so if this were loaded into a non-UTF-8 page, it might not produce the correct character. This is why we use explicit \uXXXX
for the localization menu, for example. It isn't about modern versus old browsers, it is about making assumptions about the encoding used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, agreed. I was spouting nonsense.
Other than the raw unicode issue, this looks good, and is ready to merge. |
@dpvc PTAL. |
Resolves #297