Files
gitbook/packages/react-math/src/MathFormula.tsx
T
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

66 lines
1.4 KiB
TypeScript

import React from 'react';
import { KaTeX } from './KaTeX';
import { MathJaXLazy } from './MathJaXLazy';
import './default.css';
export interface MathFormulaProps {
/**
* The formula to render.
*/
formula: string;
/**
* Whether to render the formula inline or as a block.
*/
inline?: boolean;
/**
* Additional class name to apply to the rendered formula.
*/
className?: string;
/**
* Fallback to render while loading the formula with MathJax or KateX.
*/
fallback?: React.ReactNode;
/**
* URL to load MathJax from.
*/
mathJaxUrl: string;
}
/**
* Render a math formula using KaTeX, and fallback to MathJax if KaTeX fails.
*/
export function MathFormula(props: MathFormulaProps) {
const {
formula,
inline = false,
className,
fallback = React.createElement(props.inline ? 'span' : 'div', {
className: props.className,
children: props.formula,
}),
mathJaxUrl,
} = props;
return (
<KaTeX
formula={formula}
inline={inline}
className={className}
fallback={
<MathJaXLazy
formula={formula}
inline={inline}
className={className}
fallback={fallback}
mathJaxUrl={mathJaxUrl}
/>
}
/>
);
}