This commit is contained in:
Brent Larsen
2026-10-07 09:27:20 -05:00
parent cf1ab0a7af
commit bf02eff6ea
12 changed files with 808 additions and 91 deletions
+59
View File
@@ -0,0 +1,59 @@
const TEX_SYMBOLS = Object.freeze({
"\\rightarrow": "→",
"\\to": "→",
"\\leftarrow": "←",
"\\leftrightarrow": "↔",
"\\Rightarrow": "⇒",
"\\Leftarrow": "⇐",
"\\Leftrightarrow": "⇔",
"\\times": "×",
"\\cdot": "·",
"\\pm": "±",
"\\neq": "≠",
"\\ne": "≠",
"\\leq": "≤",
"\\le": "≤",
"\\geq": "≥",
"\\ge": "≥",
"\\ldots": "…",
"\\dots": "…",
"\\bullet": "•",
});
function cleanText(value) {
let text = String(value ?? "");
// The models commonly wrap ordinary words in TeX text commands, sometimes
// inside math delimiters. Convert the presentation intent, not the math language.
text = text.replace(/\$\\textbf\{([^{}]*)\}\$/g, "**$1**");
text = text.replace(/\$\\(?:textit|emph)\{([^{}]*)\}\$/g, "//$1//");
text = text.replace(/\$\\text\{([^{}]*)\}\$/g, "$1");
text = text.replace(/\\textbf\{([^{}]*)\}/g, "**$1**");
text = text.replace(/\\(?:textit|emph)\{([^{}]*)\}/g, "//$1//");
text = text.replace(/\\text\{([^{}]*)\}/g, "$1");
// Unwrap a TeX math span only when a known presentation command was actually
// converted. Unknown TeX remains visible rather than being guessed at.
text = text.replace(/\$([^$\n]+)\$/g, (whole, inner) => {
const converted = replaceKnownTexSymbols(inner);
return converted !== inner && !/\\[A-Za-z]+/.test(converted) ? converted : whole;
});
text = replaceKnownTexSymbols(text);
// Normalize common Markdown emphasis into the tiny inline dialect used by
// Cumorah renderers. Existing Cumorah **bold** and //italic// pass through.
text = text.replace(/__([^_\n]+)__/g, "**$1**");
text = text.replace(/(?<!\*)\*([^*\n]+)\*(?!\*)/g, "//$1//");
text = text.replace(/(?<![\w])_([^_\n]+)_(?![\w])/g, "//$1//");
return text;
}
function replaceKnownTexSymbols(value) {
let text = value;
for (const [source, target] of Object.entries(TEX_SYMBOLS)) text = text.split(source).join(target);
return text;
}
module.exports = cleanText;