forked from uetchy/math-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaptor.ts
43 lines (40 loc) · 1.08 KB
/
adaptor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { mathjax } from "mathjax-full/js/mathjax";
import { TeX } from "mathjax-full/js/input/tex";
import { SVG } from "mathjax-full/js/output/svg";
import { LiteAdaptor } from "mathjax-full/js/adaptors/liteAdaptor";
import { RegisterHTMLHandler } from "mathjax-full/js/handlers/html";
import { AllPackages } from "mathjax-full/js/input/tex/AllPackages";
// MathJax bootstrap
const adaptor = new LiteAdaptor();
RegisterHTMLHandler(adaptor);
const html = mathjax.document("", {
InputJax: new TeX({ packages: AllPackages }),
OutputJax: new SVG({ fontCache: "none" }),
});
export function tex2svg(
equation: string,
isInline: boolean,
color?: string,
alternateColor?: string
): string {
const svg = adaptor
.innerHTML(html.convert(equation, { display: !isInline }))
.replace(
/(?<=<svg.+?>)/,
`
<style>
* {
fill: ${color || "black"};
}
@media (prefers-color-scheme: dark) {
* {
fill: ${alternateColor || color || "white"};
}
}
</style>`
);
if (svg.includes("merror")) {
return svg.replace(/<rect.+?><\/rect>/, "");
}
return svg;
}