Wrap Mathjax compilation into try/catch

This commit is contained in:
Henrik Giesel 2021-09-16 14:31:20 +02:00
parent f00f7f099f
commit a1dde7c966

View file

@ -36,19 +36,25 @@ export function convertMathjax(
return getEmptyIcon(style);
}
const output = globalThis.MathJax.tex2svg(input);
const svg = output.children[0];
let output: Element;
try {
output = globalThis.MathJax.tex2svg(input);
} catch (e) {
return ["Mathjax Error", String(e)];
}
if (svg.viewBox.baseVal.height === 16) {
const svg = output.children[0] as SVGElement;
if ((svg as any).viewBox.baseVal.height === 16) {
return getEmptyIcon(style);
}
let title = "";
if (svg.innerHTML.includes("data-mjx-error")) {
svg.querySelector("rect").setAttribute("fill", "yellow");
svg.querySelector("text").setAttribute("color", "red");
title = svg.querySelector("title").innerHTML;
svg.querySelector("rect")?.setAttribute("fill", "yellow");
svg.querySelector("text")?.setAttribute("color", "red");
title = svg.querySelector("title")?.innerHTML ?? "";
} else {
svg.insertBefore(style, svg.children[0]);
}