fix: dynamically import katex

This commit is contained in:
diced 2023-03-21 19:46:13 -07:00
parent bdf34bbbbf
commit 6ada79017a
No known key found for this signature in database
GPG key ID: 370BD1BA142842D1

View file

@ -1,5 +1,5 @@
import { Alert } from '@mantine/core'; import { Alert } from '@mantine/core';
import katex, { ParseError } from 'katex'; import type { ParseError } from 'katex';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import 'katex/dist/katex.min.css'; import 'katex/dist/katex.min.css';
@ -19,23 +19,27 @@ export default function KaTeX({ code, ...props }) {
</Alert> </Alert>
); );
}; };
// dynamic
useEffect(() => { useEffect(() => {
try { (async () => {
const html = katex.renderToString(code, { const katex = await import('katex');
displayMode: true, try {
throwOnError: true, const html = katex.default.renderToString(code, {
errorColor: '#f44336', displayMode: true,
}); throwOnError: true,
errorColor: '#f44336',
});
setRendered(html); setRendered(html);
} catch (e) { } catch (e) {
if (e instanceof Error) { if (e instanceof Error) {
setError(renderError(e)); setError(renderError(e));
} else { } else {
throw e; throw e;
}
} }
} })();
}, [rendered, error, code]); }, [rendered, error, code]);
if (error) return error; if (error) return error;