webui: Fix parsing non-LaTeX occurrencies of `\(` or `\)` (#17810)
* fix: Improve latex protection logic to prevent turning non-latex `\(` into `$` * chore: update webui build output
This commit is contained in:
parent
54a0fee4b7
commit
12280ae905
Binary file not shown.
|
|
@ -303,6 +303,27 @@ $$\n\\pi_n(\\mathbb{S}^3) = \\begin{cases}
|
||||||
expect(output).toBe(input); // Code blocks prevent misinterpretation
|
expect(output).toBe(input); // Code blocks prevent misinterpretation
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('preserves backslash parentheses in code blocks (GitHub issue)', () => {
|
||||||
|
const input = '```python\nfoo = "\\(bar\\)"\n```';
|
||||||
|
const output = preprocessLaTeX(input);
|
||||||
|
|
||||||
|
expect(output).toBe(input); // Code blocks should not have LaTeX conversion applied
|
||||||
|
});
|
||||||
|
|
||||||
|
test('preserves backslash brackets in code blocks', () => {
|
||||||
|
const input = '```python\nfoo = "\\[bar\\]"\n```';
|
||||||
|
const output = preprocessLaTeX(input);
|
||||||
|
|
||||||
|
expect(output).toBe(input); // Code blocks should not have LaTeX conversion applied
|
||||||
|
});
|
||||||
|
|
||||||
|
test('preserves backslash parentheses in inline code', () => {
|
||||||
|
const input = 'Use `foo = "\\(bar\\)"` in your code.';
|
||||||
|
const output = preprocessLaTeX(input);
|
||||||
|
|
||||||
|
expect(output).toBe(input);
|
||||||
|
});
|
||||||
|
|
||||||
test('escape backslash in mchem ce', () => {
|
test('escape backslash in mchem ce', () => {
|
||||||
const input = 'mchem ce:\n$\\ce{2H2(g) + O2(g) -> 2H2O(l)}$';
|
const input = 'mchem ce:\n$\\ce{2H2(g) + O2(g) -> 2H2O(l)}$';
|
||||||
const output = preprocessLaTeX(input);
|
const output = preprocessLaTeX(input);
|
||||||
|
|
|
||||||
|
|
@ -226,19 +226,16 @@ export function preprocessLaTeX(content: string): string {
|
||||||
return expr;
|
return expr;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Step 5: Restore code blocks
|
// Step 5: Apply additional escaping functions (brackets and mhchem)
|
||||||
content = content.replace(/<<CODE_BLOCK_(\d+)>>/g, (_, index) => {
|
// This must happen BEFORE restoring code blocks to avoid affecting code content
|
||||||
return codeBlocks[parseInt(index)];
|
|
||||||
});
|
|
||||||
|
|
||||||
// Step 6: Apply additional escaping functions (brackets and mhchem)
|
|
||||||
content = escapeBrackets(content);
|
content = escapeBrackets(content);
|
||||||
|
|
||||||
if (doEscapeMhchem && (content.includes('\\ce{') || content.includes('\\pu{'))) {
|
if (doEscapeMhchem && (content.includes('\\ce{') || content.includes('\\pu{'))) {
|
||||||
content = escapeMhchem(content);
|
content = escapeMhchem(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Final pass: Convert \(...\) → $...$, \[...\] → $$...$$
|
// Step 6: Convert remaining \(...\) → $...$, \[...\] → $$...$$
|
||||||
|
// This must happen BEFORE restoring code blocks to avoid affecting code content
|
||||||
content = content
|
content = content
|
||||||
// Using the look‑behind pattern `(?<!\\)` we skip matches
|
// Using the look‑behind pattern `(?<!\\)` we skip matches
|
||||||
// that are preceded by a backslash, e.g.
|
// that are preceded by a backslash, e.g.
|
||||||
|
|
@ -248,12 +245,18 @@ export function preprocessLaTeX(content: string): string {
|
||||||
// Using the look‑behind pattern `(?<!\\)` we skip matches
|
// Using the look‑behind pattern `(?<!\\)` we skip matches
|
||||||
// that are preceded by a backslash, e.g. `\\[4pt]`.
|
// that are preceded by a backslash, e.g. `\\[4pt]`.
|
||||||
/(?<!\\)\\\[([\s\S]*?)\\\]/g, // display, see also PR #16599
|
/(?<!\\)\\\[([\s\S]*?)\\\]/g, // display, see also PR #16599
|
||||||
(_, prefix: string, content: string) => {
|
(_, content: string) => {
|
||||||
return `${prefix}$$${content}$$`;
|
return `$$${content}$$`;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// Step 7: Restore blockquote markers
|
// Step 7: Restore code blocks
|
||||||
|
// This happens AFTER all LaTeX conversions to preserve code content
|
||||||
|
content = content.replace(/<<CODE_BLOCK_(\d+)>>/g, (_, index) => {
|
||||||
|
return codeBlocks[parseInt(index)];
|
||||||
|
});
|
||||||
|
|
||||||
|
// Step 8: Restore blockquote markers
|
||||||
if (blockquoteMarkers.size > 0) {
|
if (blockquoteMarkers.size > 0) {
|
||||||
const finalLines = content.split('\n');
|
const finalLines = content.split('\n');
|
||||||
const restoredLines = finalLines.map((line, index) => {
|
const restoredLines = finalLines.map((line, index) => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue