Skip to content
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

Merged
merged 3 commits into from
Aug 15, 2017
Merged

[main] typeset: add promise-based API #347

merged 3 commits into from
Aug 15, 2017

Conversation

pkra
Copy link
Contributor

@pkra pkra commented Aug 1, 2017

Resolves #297

@pkra pkra changed the base branch from master to develop August 1, 2017 10:37
if (callback) cbTypeset(data, callback);
else return new Promise(function (resolve, reject) {
cbTypeset(data, function (output, input) {
if (output.errors) reject(output.errors);
Copy link
Member

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.

Copy link
Contributor Author

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.)

Copy link
Member

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.

Copy link
Member

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. :-)

@pkra
Copy link
Contributor Author

pkra commented Aug 15, 2017

@dpvc PTAL


tape('basic test: check warnings', function (t) {
t.plan(2);
mjAPI.typeset({math:'呵', html:true})
Copy link
Member

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.

t.plan(2);
mjAPI.typeset({math:'呵', html:true})
.catch(errors => t.ok(errors, 'CommonHTML output reports error'));
mjAPI.typeset({math:'呵', svg:true})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Contributor Author

@pkra pkra Aug 15, 2017

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edited the above.

Copy link
Member

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.

Copy link
Contributor Author

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.

@dpvc
Copy link
Member

dpvc commented Aug 15, 2017

Other than the raw unicode issue, this looks good, and is ready to merge.

@pkra
Copy link
Contributor Author

pkra commented Aug 15, 2017

@dpvc PTAL.

@dpvc dpvc merged commit 1ed132d into develop Aug 15, 2017
@dpvc dpvc deleted the 297 branch August 15, 2017 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants