anki/ts/editable/mathjax.ts
2021-09-15 13:45:17 +02:00

58 lines
1.7 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { mathIcon } from "./icons";
const parser = new DOMParser();
function getCSS(nightMode: boolean, fontSize: number): string {
const color = nightMode ? "white" : "black";
/* color is set for Maths, fill for the empty icon */
return `svg { color: ${color}; fill: ${color}; font-size: ${fontSize}px; };`;
}
function getStyle(css: string): HTMLStyleElement {
const style = document.createElement("style") as HTMLStyleElement;
style.appendChild(document.createTextNode(css));
return style;
}
function getEmptyIcon(style: HTMLStyleElement): [string, string] {
const icon = parser.parseFromString(mathIcon, "image/svg+xml");
const svg = icon.children[0];
svg.insertBefore(style, svg.children[0]);
return [svg.outerHTML, "MathJax"];
}
export function convertMathjax(
input: string,
nightMode: boolean,
fontSize: number
): [string, string] {
const style = getStyle(getCSS(nightMode, fontSize));
if (input.trim().length === 0) {
return getEmptyIcon(style);
}
const output = globalThis.MathJax.tex2svg(input);
const svg = output.children[0];
if (svg.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;
} else {
svg.insertBefore(style, svg.children[0]);
}
return [svg.outerHTML, title];
}