MathJax: Use $ as LaTeX delimiters. Fall back to \( and \) if detected. Interpret \$ as a text literal "$" sign.

This commit is contained in:
Daniel J. Geiger 2023-06-15 13:56:33 -05:00
parent 94f4b727bb
commit be057bde39

View File

@ -106,12 +106,16 @@ class GetMathProps {
} }
const getMathProps = new GetMathProps(); const getMathProps = new GetMathProps();
const getStartDelimiter = (useTex: boolean): string => { const deEscapeTex = (text: string, mathProps: MathProps): string => {
return useTex ? "\\(" : "`"; return mathProps.useTex ? text.replaceAll("\\$", "$") : text;
}; };
const getEndDelimiter = (useTex: boolean): string => { const getStartDelimiter = (useTex: boolean, desktop = false): string => {
return useTex ? "\\)" : "`"; return useTex ? (desktop ? "\\(" : "$") : "`";
};
const getEndDelimiter = (useTex: boolean, desktop = false): string => {
return useTex ? (desktop ? "\\)" : "$") : "`";
}; };
const mathJax = {} as { const mathJax = {} as {
@ -279,8 +283,17 @@ const roundDec = (x: number, n: number) => {
}; };
const splitMath = (text: string, mathProps: MathProps) => { const splitMath = (text: string, mathProps: MathProps) => {
const startDelimiter = getStartDelimiter(mathProps.useTex); const desktopStartDelimiter = getStartDelimiter(mathProps.useTex, true);
const endDelimiter = getEndDelimiter(mathProps.useTex); const desktopEndDelimiter = getEndDelimiter(mathProps.useTex, true);
const webStartDelimiter = getStartDelimiter(mathProps.useTex);
const webEndDelimiter = getEndDelimiter(mathProps.useTex);
const desktopStyle = text.indexOf(desktopStartDelimiter) >= 0;
const startDelimiter = desktopStyle
? desktopStartDelimiter
: webStartDelimiter;
const endDelimiter = desktopStyle ? desktopEndDelimiter : webEndDelimiter;
let curIndex = 0; let curIndex = 0;
let oldIndex = 0; let oldIndex = 0;
const array = []; const array = [];
@ -298,7 +311,14 @@ const splitMath = (text: string, mathProps: MathProps) => {
? endDelimiter.length ? endDelimiter.length
: 0 : 0
: startDelimiter.length); : startDelimiter.length);
curIndex = text.indexOf(inText ? startDelimiter : endDelimiter, oldIndex); const delim = inText ? startDelimiter : endDelimiter;
curIndex = text.indexOf(delim, oldIndex);
// Avoid splitting at escaped "$" characters
if (!desktopStyle) {
while (curIndex > 0 && text.charAt(curIndex - 1) === "\\") {
curIndex = text.indexOf(delim, curIndex + 1);
}
}
if (curIndex >= oldIndex || (curIndex < 0 && oldIndex > 0)) { if (curIndex >= oldIndex || (curIndex < 0 && oldIndex > 0)) {
inText = !inText; inText = !inText;
array.push( array.push(
@ -318,8 +338,8 @@ const joinMath = (
mathProps: MathProps, mathProps: MathProps,
isMathJaxLoaded: boolean, isMathJaxLoaded: boolean,
) => { ) => {
const startDelimiter = getStartDelimiter(mathProps.useTex); const startDelimiter = getStartDelimiter(mathProps.useTex, true);
const endDelimiter = getEndDelimiter(mathProps.useTex); const endDelimiter = getEndDelimiter(mathProps.useTex, true);
let inText = true; let inText = true;
let joined = ""; let joined = "";
for (let index = 0; index < text.length; index++) { for (let index = 0; index < text.length; index++) {
@ -529,8 +549,9 @@ const markupText = (
markup[index].push(math.svg); markup[index].push(math.svg);
aria[index].push(math.aria); aria[index].push(math.aria);
} else { } else {
markup[index].push(lineArray[i]); const text = deEscapeTex(lineArray[i], mathProps);
aria[index].push(lineArray[i]); markup[index].push(text);
aria[index].push(text);
} }
} }
if (lineArray.length === 0) { if (lineArray.length === 0) {
@ -585,7 +606,7 @@ const measureMarkup = (
container.appendChild(markup[i] as Element); container.appendChild(markup[i] as Element);
} else { } else {
// Append as text // Append as text
container.append(markup[i]); container.append(deEscapeTex(markup[i] as string, mathProps));
} }
} }