fix: Remove single dollar sign math rendering due to false positives

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2025-12-05 17:24:40 +00:00
parent bcfdff1067
commit 5d014f50df

View File

@@ -12,7 +12,6 @@ const DANGEROUS_TAGS = ['svg', 'math'];
const latexDelimiters = [ const latexDelimiters = [
{ left: '$$', right: '$$', display: true }, { left: '$$', right: '$$', display: true },
{ left: '\\[', right: '\\]', display: true }, { left: '\\[', right: '\\]', display: true },
{ left: '$', right: '$', display: false },
{ left: '\\(', right: '\\)', display: false } { left: '\\(', right: '\\)', display: false }
]; ];
@@ -106,7 +105,7 @@ function Article({ cache }) {
if (v.nodeName === '#text') { if (v.nodeName === '#text') {
const text = v.data; const text = v.data;
if (text.includes('\\[') || text.includes('\\(') || text.includes('$$') || text.includes('$')) { if (text.includes('\\[') || text.includes('\\(') || text.includes('$$')) {
return <Latex key={key} delimiters={latexDelimiters}>{text}</Latex>; return <Latex key={key} delimiters={latexDelimiters}>{text}</Latex>;
} }
@@ -138,8 +137,7 @@ function Article({ cache }) {
const textContent = v.textContent.trim(); const textContent = v.textContent.trim();
const isMath = (textContent.startsWith('\\(') && textContent.endsWith('\\)')) || const isMath = (textContent.startsWith('\\(') && textContent.endsWith('\\)')) ||
(textContent.startsWith('\\[') && textContent.endsWith('\\]')) || (textContent.startsWith('\\[') && textContent.endsWith('\\]')) ||
(textContent.startsWith('$$') && textContent.endsWith('$$')) || (textContent.startsWith('$$') && textContent.endsWith('$$'));
(textContent.startsWith('$') && textContent.endsWith('$') && textContent.indexOf('$') !== textContent.lastIndexOf('$'));
const props = { key: key }; const props = { key: key };
if (v.hasAttributes()) { if (v.hasAttributes()) {
@@ -159,13 +157,6 @@ function Article({ cache }) {
const firstParen = mathContent.indexOf('\\('); const firstParen = mathContent.indexOf('\\(');
const lastParen = mathContent.lastIndexOf('\\)'); const lastParen = mathContent.lastIndexOf('\\)');
mathContent = mathContent.substring(0, firstParen) + '\\[' + mathContent.substring(firstParen + 2, lastParen) + '\\]' + mathContent.substring(lastParen + 2); mathContent = mathContent.substring(0, firstParen) + '\\[' + mathContent.substring(firstParen + 2, lastParen) + '\\]' + mathContent.substring(lastParen + 2);
} else if (trimmed.startsWith('$') && !trimmed.startsWith('$$')) {
// Replace $ with $$
const firstDollar = mathContent.indexOf('$');
const lastDollar = mathContent.lastIndexOf('$');
if (firstDollar !== lastDollar) {
mathContent = mathContent.substring(0, firstDollar) + '$$' + mathContent.substring(firstDollar + 1, lastDollar) + '$$' + mathContent.substring(lastDollar + 1);
}
} }
} }
return <Tag {...props}><Latex delimiters={latexDelimiters}>{mathContent}</Latex></Tag>; return <Tag {...props}><Latex delimiters={latexDelimiters}>{mathContent}</Latex></Tag>;