2021-08-04 01:55:39 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2021-09-17 21:30:32 +02:00
|
|
|
/* eslint
|
|
|
|
@typescript-eslint/no-explicit-any: "off",
|
|
|
|
*/
|
|
|
|
|
Switch editor to full MathJax package to fix broken autoloads
Re: https://forums.ankiweb.net/t/anki-2-1-53-release-candidate/20122/2
Autoloads in MathJax are asynchronous, and the caller is expected to
use asynchronous APIs when they are in use [1]. The editor uses the synchronous
tex2svg(), which throws a "MathJax retry" error when an autoload package
has not yet loaded. Attempting to use the package before it has loaded
appears to break future invocations as well, so the package fails to work
at all until a new webview is created.
The following HTML will reproduce the issue when added to a single card
in a new profile:
```
<strong>6 </strong>Every combination of <anki-mathjax>\boldsymbol{v}=(1,-2,1)</anki-mathjax> and <anki-mathjax>\boldsymbol{w}=(0,1,-1)</anki-mathjax> has components that add to _____.<br>
```
Ideally we'd switch the MathJax rendering to be asynchronous, but that
didn't work well when I tried it in #1862. For now I've just switched
to the full package, which adds about 130KB to the final minified JS
(2.76MB), and likely slows down editor loading somewhat.
[1] https://github.com/mathjax/MathJax/issues/2557#issuecomment-727655089
2022-05-21 06:01:38 +02:00
|
|
|
import "mathjax/es5/tex-svg-full";
|
2022-05-13 05:23:35 +02:00
|
|
|
|
2021-08-07 05:46:52 +02:00
|
|
|
import { mathIcon } from "./icons";
|
|
|
|
|
|
|
|
const parser = new DOMParser();
|
2021-08-04 18:57:34 +02:00
|
|
|
|
2021-08-07 22:07:57 +02:00
|
|
|
function getCSS(nightMode: boolean, fontSize: number): string {
|
2021-08-07 21:03:36 +02:00
|
|
|
const color = nightMode ? "white" : "black";
|
2021-08-07 20:26:39 +02:00
|
|
|
/* color is set for Maths, fill for the empty icon */
|
2021-08-07 22:52:48 +02:00
|
|
|
return `svg { color: ${color}; fill: ${color}; font-size: ${fontSize}px; };`;
|
2021-08-07 22:07:57 +02:00
|
|
|
}
|
2021-08-07 05:46:52 +02:00
|
|
|
|
2021-08-07 22:07:57 +02:00
|
|
|
function getStyle(css: string): HTMLStyleElement {
|
|
|
|
const style = document.createElement("style") as HTMLStyleElement;
|
|
|
|
style.appendChild(document.createTextNode(css));
|
|
|
|
return style;
|
|
|
|
}
|
2021-08-04 01:55:39 +02:00
|
|
|
|
2021-08-07 22:07:57 +02:00
|
|
|
function getEmptyIcon(style: HTMLStyleElement): [string, string] {
|
2021-08-07 05:46:52 +02:00
|
|
|
const icon = parser.parseFromString(mathIcon, "image/svg+xml");
|
|
|
|
const svg = icon.children[0];
|
2021-08-04 18:57:34 +02:00
|
|
|
svg.insertBefore(style, svg.children[0]);
|
2021-08-07 05:46:52 +02:00
|
|
|
|
2021-08-07 22:23:18 +02:00
|
|
|
return [svg.outerHTML, "MathJax"];
|
2021-08-04 01:55:39 +02:00
|
|
|
}
|
2021-08-07 05:46:52 +02:00
|
|
|
|
2021-08-07 21:03:36 +02:00
|
|
|
export function convertMathjax(
|
|
|
|
input: string,
|
|
|
|
nightMode: boolean,
|
2021-10-19 01:06:00 +02:00
|
|
|
fontSize: number,
|
2021-08-07 21:03:36 +02:00
|
|
|
): [string, string] {
|
2022-05-19 04:47:49 +02:00
|
|
|
input = revealClozeAnswers(input);
|
2021-08-07 22:07:57 +02:00
|
|
|
const style = getStyle(getCSS(nightMode, fontSize));
|
|
|
|
|
2021-08-07 05:46:52 +02:00
|
|
|
if (input.trim().length === 0) {
|
2021-08-07 22:07:57 +02:00
|
|
|
return getEmptyIcon(style);
|
2021-08-07 05:46:52 +02:00
|
|
|
}
|
|
|
|
|
2021-09-16 14:31:20 +02:00
|
|
|
let output: Element;
|
|
|
|
try {
|
|
|
|
output = globalThis.MathJax.tex2svg(input);
|
|
|
|
} catch (e) {
|
|
|
|
return ["Mathjax Error", String(e)];
|
|
|
|
}
|
|
|
|
|
|
|
|
const svg = output.children[0] as SVGElement;
|
2021-08-07 05:46:52 +02:00
|
|
|
|
2021-09-16 14:31:20 +02:00
|
|
|
if ((svg as any).viewBox.baseVal.height === 16) {
|
2021-08-07 22:07:57 +02:00
|
|
|
return getEmptyIcon(style);
|
2021-08-07 05:46:52 +02:00
|
|
|
}
|
|
|
|
|
2021-08-07 19:33:01 +02:00
|
|
|
let title = "";
|
|
|
|
|
2021-08-07 19:13:08 +02:00
|
|
|
if (svg.innerHTML.includes("data-mjx-error")) {
|
2021-09-16 14:31:20 +02:00
|
|
|
svg.querySelector("rect")?.setAttribute("fill", "yellow");
|
|
|
|
svg.querySelector("text")?.setAttribute("color", "red");
|
|
|
|
title = svg.querySelector("title")?.innerHTML ?? "";
|
2021-08-07 19:13:08 +02:00
|
|
|
} else {
|
2021-08-07 05:46:52 +02:00
|
|
|
svg.insertBefore(style, svg.children[0]);
|
|
|
|
}
|
2021-08-07 19:13:08 +02:00
|
|
|
|
2021-08-07 19:33:01 +02:00
|
|
|
return [svg.outerHTML, title];
|
2021-08-07 05:46:52 +02:00
|
|
|
}
|
2022-04-25 05:42:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape characters which are technically legal in Mathjax, but confuse HTML.
|
|
|
|
*/
|
|
|
|
export function escapeSomeEntities(value: string): string {
|
2022-05-10 03:54:06 +02:00
|
|
|
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
2022-04-25 05:42:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function unescapeSomeEntities(value: string): string {
|
2022-05-03 06:21:31 +02:00
|
|
|
return value.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
|
2022-04-25 05:42:54 +02:00
|
|
|
}
|
2022-05-19 04:47:49 +02:00
|
|
|
|
|
|
|
function revealClozeAnswers(input: string): string {
|
|
|
|
// one-line version of regex in cloze.rs
|
|
|
|
const regex = /\{\{c(\d+)::(.*?)(?:::(.*?))?\}\}/gis;
|
|
|
|
return input.replace(regex, "[$2]");
|
|
|
|
}
|