Merge pull request #973 from hgiesel/mathjaxerror

Render error if MathJax raises error
This commit is contained in:
Damien Elmes 2021-02-03 13:09:12 +10:00 committed by GitHub
commit e683e5c4d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,9 +16,9 @@ var onUpdateHook: Array<Callback>;
var onShownHook: Array<Callback>; var onShownHook: Array<Callback>;
function _runHook(arr: Array<Callback>): Promise<void[]> { function _runHook(arr: Array<Callback>): Promise<void[]> {
var promises = []; const promises = [];
for (var i = 0; i < arr.length; i++) { for (let i = 0; i < arr.length; i++) {
promises.push(arr[i]()); promises.push(arr[i]());
} }
@ -38,7 +38,18 @@ async function _updateQA(
onUpdateHook = [onupdate]; onUpdateHook = [onupdate];
onShownHook = [onshown]; onShownHook = [onshown];
var qa = $("#qa"); const qa = $("#qa");
const renderError = (kind: string) => (error: Error): void => {
const errorMessage = String(error).substring(0, 2000);
const errorStack = String(error.stack).substring(0, 2000);
qa.html(
`Invalid ${kind} on card: ${errorMessage}\n${errorStack}`.replace(
/\n/g,
"<br />"
)
);
};
// fade out current text // fade out current text
await qa.fadeTo(fadeTime, 0).promise(); await qa.fadeTo(fadeTime, 0).promise();
@ -46,23 +57,19 @@ async function _updateQA(
// update text // update text
try { try {
qa.html(html); qa.html(html);
} catch (err) { } catch (error) {
qa.html( renderError("HTML")(error);
(
`Invalid HTML on card: ${String(err).substring(0, 2000)}\n` +
String(err.stack).substring(0, 2000)
).replace(/\n/g, "<br />")
);
} }
await _runHook(onUpdateHook);
// wait for mathjax to ready // wait for mathjax to ready
await MathJax.startup.promise.then(() => { await MathJax.startup.promise
// clear MathJax buffers from previous typesets .then(() => {
MathJax.typesetClear(); // clear MathJax buffers from previous typesets
MathJax.typesetClear();
return MathJax.typesetPromise(qa.slice(0, 1)); return MathJax.typesetPromise(qa.slice(0, 1));
}); })
.catch(renderError("MathJax"));
// and reveal when processing is done // and reveal when processing is done
await qa.fadeTo(fadeTime, 1).promise(); await qa.fadeTo(fadeTime, 1).promise();