/**
* Checks if content contains LaTeX math expressions
*/
export function hasLaTeX(content: string): boolean {
// Check for inline math: $...$ or \(...\)
const inlineMathPattern = /\$[^$]+\$|\\\([^)]+\\\)/;
// Check for block math: $$...$$ or \[...\]
const blockMathPattern = /\$\$[^$]+\$\$|\\\[[^\]]+\\\]/;
return inlineMathPattern.test(content) || blockMathPattern.test(content);
}
/**
* Processes LaTeX math expressions in HTML content
* Wraps LaTeX expressions in appropriate HTML for rendering with MathJax or KaTeX
*/
export function processLaTeX(html: string): string {
// Process block math: $$...$$ or \[...\]
// Convert to
...
for MathJax/KaTeX
const blockMathPattern = /\$\$([^$]+)\$\$|\\\[([^\]]+)\\\]/gs;
html = html.replace(blockMathPattern, (match, dollarContent, bracketContent) => {
const mathContent = (dollarContent || bracketContent || '').trim();
// Wrap in appropriate tags for MathJax/KaTeX
return `\\[${mathContent}\\]
`;
});
// Process inline math: $...$ or \(...\)
// Convert to ... for MathJax/KaTeX
const inlineMathPattern = /\$([^$\n]+)\$|\\\(([^)]+)\\\)/g;
html = html.replace(inlineMathPattern, (match, dollarContent, bracketContent) => {
const mathContent = (dollarContent || bracketContent || '').trim();
// Wrap in appropriate tags for MathJax/KaTeX
return `\\(${mathContent}\\)`;
});
return html;
}