fix: Cache SVGs separately for mixed-text and math-only modes.

This commit is contained in:
Daniel J. Geiger 2022-12-30 10:48:47 -06:00
parent 9ee2bf36cf
commit 0f6ad916c0

View File

@ -120,6 +120,7 @@ const mathJax = {} as {
visitor: any; visitor: any;
mmlSvg: any; mmlSvg: any;
mmlSre: any; mmlSre: any;
amFixes: any;
}; };
let stopLoadingMathJax = false; let stopLoadingMathJax = false;
@ -142,6 +143,7 @@ const loadMathJax = async () => {
mathJax.texHtml === undefined || mathJax.texHtml === undefined ||
mathJax.visitor === undefined || mathJax.visitor === undefined ||
mathJax.mmlSvg === undefined || mathJax.mmlSvg === undefined ||
mathJax.amFixes === undefined ||
(useSRE && mathJax.mmlSre === undefined)); (useSRE && mathJax.mmlSre === undefined));
if (!shouldLoad && !mathJaxLoaded) { if (!shouldLoad && !mathJaxLoaded) {
stopLoadingMathJax = true; stopLoadingMathJax = true;
@ -198,7 +200,7 @@ const loadMathJax = async () => {
const MathJax = ( const MathJax = (
await require("mathjax-full/js/input/asciimath/mathjax2/legacy/MathJax") await require("mathjax-full/js/input/asciimath/mathjax2/legacy/MathJax")
).MathJax; ).MathJax;
MathJax.InputJax.AsciiMath.AM.Augment({ displaystyle: false }); mathJax.amFixes = MathJax.InputJax.AsciiMath.AM.Augment;
// Load the document creator last // Load the document creator last
const mathjax = (await import("mathjax-full/js/mathjax")).mathjax; const mathjax = (await import("mathjax-full/js/mathjax")).mathjax;
@ -415,15 +417,21 @@ const math2Svg = (
isMathJaxLoaded: boolean, isMathJaxLoaded: boolean,
): { svg: string; aria: string } => { ): { svg: string; aria: string } => {
const useTex = mathProps.useTex; const useTex = mathProps.useTex;
const key = `${mathProps.mathOnly}${text}`;
if ( if (
isMathJaxLoaded && isMathJaxLoaded &&
(useTex ? mathJaxSvgCacheTex[text] : mathJaxSvgCacheAM[text]) (useTex ? mathJaxSvgCacheTex[key] : mathJaxSvgCacheAM[key])
) { ) {
return useTex ? mathJaxSvgCacheTex[text] : mathJaxSvgCacheAM[text]; return useTex ? mathJaxSvgCacheTex[key] : mathJaxSvgCacheAM[key];
} }
loadMathJax(); loadMathJax();
try { try {
const userOptions = { display: mathProps.mathOnly }; const userOptions = { display: mathProps.mathOnly };
// For some reason this needs to be set before each call to the AsciiMath
// input jax to render display/inline correctly.
if (isMathJaxLoaded && !useTex) {
mathJax.amFixes({ displaystyle: mathProps.mathOnly });
}
// Intermediate MathML // Intermediate MathML
const mmlString = isMathJaxLoaded const mmlString = isMathJaxLoaded
? mathJax.visitor.visitTree( ? mathJax.visitor.visitTree(
@ -444,9 +452,9 @@ const math2Svg = (
: mmlString; : mmlString;
if (isMathJaxLoaded) { if (isMathJaxLoaded) {
if (useTex) { if (useTex) {
mathJaxSvgCacheTex[text] = { svg: htmlString, aria: ariaString }; mathJaxSvgCacheTex[key] = { svg: htmlString, aria: ariaString };
} else { } else {
mathJaxSvgCacheAM[text] = { svg: htmlString, aria: ariaString }; mathJaxSvgCacheAM[key] = { svg: htmlString, aria: ariaString };
} }
} }
return { svg: htmlString, aria: ariaString }; return { svg: htmlString, aria: ariaString };