revert: markdown katex parse
This commit is contained in:
@@ -32,60 +32,24 @@ export function escapeDollarNumber(text: string) {
|
|||||||
return escapedText;
|
return escapedText;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function escapeBrackets(text: string): string {
|
export function escapeBrackets(text: string) {
|
||||||
const codeRegex = /```[\s\S]*?```|`[^`\n]+`/g;
|
const pattern =
|
||||||
|
/(```[\S\s]*?```|`.*?`)|\\\[([\S\s]*?[^\\])\\]|\\\((.*?)\\\)/g;
|
||||||
const codeBlocks: string[] = [];
|
return text.replaceAll(
|
||||||
const placeholder = '%%CODE_BLOCK%%';
|
pattern,
|
||||||
|
(match, codeBlock, squareBracket, roundBracket) => {
|
||||||
// 1. replace code blocks with a placeholder
|
if (codeBlock) {
|
||||||
// and store them in an array
|
return codeBlock;
|
||||||
const temp = text.replace(codeRegex, (match) => {
|
} else if (squareBracket) {
|
||||||
codeBlocks.push(match);
|
return `$$${squareBracket}$$`;
|
||||||
return placeholder;
|
} else if (roundBracket) {
|
||||||
});
|
return `$${roundBracket}$`;
|
||||||
|
}
|
||||||
// 2. repace \[...\] with $$...$$,and replace \(...\) with $...$
|
return match;
|
||||||
const replaced = temp
|
}
|
||||||
.replace(/\\\[([\s\S]*?)\\\]/g, (_, inner) => `$$${inner}$$`)
|
|
||||||
.replace(/\\\(([\s\S]*?)\\\)/g, (_, inner) => `$${inner}$`);
|
|
||||||
|
|
||||||
// 3. restore code blocks from the placeholder
|
|
||||||
let i = 0;
|
|
||||||
return replaced.replace(
|
|
||||||
new RegExp(placeholder, 'g'),
|
|
||||||
() => codeBlocks[i++] || ''
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function escapeMhchem_2(text: string) {
|
export function escapeMhchem(text: string) {
|
||||||
return text.replaceAll('$\\ce{', '$\\\\ce{').replaceAll('$\\pu{', '$\\\\pu{');
|
return text.replaceAll('$\\ce{', '$\\\\ce{').replaceAll('$\\pu{', '$\\\\pu{');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function escapeMhchem(text: string): string {
|
|
||||||
const codeRegex = /```[\s\S]*?```|`[^`\n]+`/g;
|
|
||||||
const codeBlocks: string[] = [];
|
|
||||||
const placeholder = '%%CODE_BLOCK%%';
|
|
||||||
|
|
||||||
// 1. Replace code blocks with a placeholder
|
|
||||||
const temp = text.replace(codeRegex, (match) => {
|
|
||||||
codeBlocks.push(match);
|
|
||||||
return placeholder;
|
|
||||||
});
|
|
||||||
|
|
||||||
// 2. Only perform \ce{} / \pu{} escaping inside $...$ (to avoid incorrect escaping outside math environments)
|
|
||||||
const mathRegex = /\$(.+?)\$/gs;
|
|
||||||
const escaped = temp.replace(mathRegex, (_, content) => {
|
|
||||||
const fixed = content
|
|
||||||
.replace(/(?<!\\)\\ce\{/g, '\\\\ce{')
|
|
||||||
.replace(/(?<!\\)\\pu\{/g, '\\\\pu{');
|
|
||||||
return `$${fixed}$`;
|
|
||||||
});
|
|
||||||
|
|
||||||
// 3. Restore code blocks
|
|
||||||
let i = 0;
|
|
||||||
return escaped.replace(
|
|
||||||
new RegExp(placeholder, 'g'),
|
|
||||||
() => codeBlocks[i++] || ''
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user