Spaces:
Sleeping
Sleeping
render user input LaTex
Browse files- web/src/App.tsx +23 -1
web/src/App.tsx
CHANGED
|
@@ -355,6 +355,28 @@ const splitStreamingContent = (content: string): { safe: string; tail: string }
|
|
| 355 |
return { safe: content, tail: '' };
|
| 356 |
};
|
| 357 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
const PlotImage: React.FC<{ src?: string; alt?: string }> = ({ src, alt, ...props }) => {
|
| 359 |
const [currentSrc, setCurrentSrc] = useState<string | undefined>(src);
|
| 360 |
const [fallbackSrc, setFallbackSrc] = useState<string | undefined>(undefined);
|
|
@@ -690,7 +712,7 @@ const MessageItem = React.memo(function MessageItem({
|
|
| 690 |
img({ src, alt, ...props }: any) { return <PlotImage src={src} alt={alt} {...props} />; },
|
| 691 |
}}
|
| 692 |
>
|
| 693 |
-
{message.content}
|
| 694 |
</ReactMarkdown>
|
| 695 |
)}
|
| 696 |
</div>
|
|
|
|
| 355 |
return { safe: content, tail: '' };
|
| 356 |
};
|
| 357 |
|
| 358 |
+
const normalizeUserLatex = (content: string): string => {
|
| 359 |
+
if (!content || content.includes('$')) {
|
| 360 |
+
return content;
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
let normalized = content;
|
| 364 |
+
|
| 365 |
+
// Wrap common block environments if user forgot $$ delimiters.
|
| 366 |
+
normalized = normalized.replace(
|
| 367 |
+
/\\begin\{(aligned|align\*?|equation\*?|gather\*?|multline\*?|array|matrix|pmatrix|bmatrix|vmatrix|Vmatrix|cases|split|eqnarray\*?)\}[\s\S]*?\\end\{\1\}/g,
|
| 368 |
+
(m) => `$$${m}$$`
|
| 369 |
+
);
|
| 370 |
+
|
| 371 |
+
// Wrap common inline commands if user forgot $ delimiters.
|
| 372 |
+
normalized = normalized.replace(
|
| 373 |
+
/\\(?:frac|sqrt|sum|int|prod|lim|alpha|beta|gamma|delta|theta|lambda|pi|sigma|mu|partial|cdot|times|leq|geq|neq|infty|forall|exists|sin|cos|tan|log|ln)(?:\s*\{[^{}]*\}){0,2}/g,
|
| 374 |
+
(m) => `$${m}$`
|
| 375 |
+
);
|
| 376 |
+
|
| 377 |
+
return normalized;
|
| 378 |
+
};
|
| 379 |
+
|
| 380 |
const PlotImage: React.FC<{ src?: string; alt?: string }> = ({ src, alt, ...props }) => {
|
| 381 |
const [currentSrc, setCurrentSrc] = useState<string | undefined>(src);
|
| 382 |
const [fallbackSrc, setFallbackSrc] = useState<string | undefined>(undefined);
|
|
|
|
| 712 |
img({ src, alt, ...props }: any) { return <PlotImage src={src} alt={alt} {...props} />; },
|
| 713 |
}}
|
| 714 |
>
|
| 715 |
+
{message.role === 'user' ? normalizeUserLatex(message.content) : message.content}
|
| 716 |
</ReactMarkdown>
|
| 717 |
)}
|
| 718 |
</div>
|