v0.2.0
This commit is contained in:
@@ -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;
|
||||
Reference in New Issue
Block a user