Files
Samy Pessé cf958bacd1 Fallback to MathJax when KaTeX fails to compile math (#303)
* Start

* Use MathJax v3

* Format

* Improve font-size

* Lint

* Better handle loading

* Copy MathJax assets to public folder

* Attempt at using local assets for MathJax

* Use bun 1.0.31

* Try with bun 1.0.33

* Try fixing headers

* Simplify

* Simplify and not use suspense module

* Cleanup more

* Add test for Math

* Make math block scroll if needed

* Update packages/react-math/src/KaTeX.tsx

Co-authored-by: Greg Bergé <berge.greg@gmail.com>

* Use React.use

* Format

* Use exports

* Lint

* Disable cache in CI for lint

* Format

---------

Co-authored-by: Greg Bergé <berge.greg@gmail.com>
2024-03-19 11:34:53 +01:00

36 lines
860 B
TypeScript

import katex from 'katex';
import React from 'react';
const KaTeXCSS = React.lazy(() => import('./KaTeXCSS'));
/**
* Server component to compile the KaTeX formula to HTML.
*/
export function KaTeX(props: {
formula: string;
inline: boolean;
className?: string;
fallback?: React.ReactNode;
}) {
const { formula, inline, className } = props;
try {
const html = katex.renderToString(formula, {
displayMode: !inline,
output: 'htmlAndMathml',
throwOnError: true,
strict: false,
});
const Tag = inline ? 'span' : 'div';
return (
<>
<KaTeXCSS />
<Tag className={className} dangerouslySetInnerHTML={{ __html: html }} />
</>
);
} catch (error) {
return <>{props.fallback}</>;
}
}