import { memo, useEffect, useRef } from "react"; interface MathBlockProps { content: string; inline?: boolean } const MathBlock = memo(function MathBlock({ content, inline = false }: MathBlockProps) { const ref = useRef(null); useEffect(() => { if (!ref.current) return; let cancelled = false; import("katex").then(katex => { if (cancelled || !ref.current) return; try { katex.default.render(content.trim(), ref.current, { displayMode: !inline, throwOnError: false, output: "html", trust: false, }); } catch (e) { if (ref.current) ref.current.textContent = content; } }).catch(() => { if (ref.current) ref.current.textContent = content; }); return () => { cancelled = true; }; }, [content, inline]); if (inline) { return ; } return (
); } ); export default MathBlock;