mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-19 17:15:24 +00:00
cf958bacd1
* 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>
36 lines
860 B
TypeScript
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}</>;
|
|
}
|
|
}
|