UNPKG

react-katex

Version:

Display math in TeX with KaTeX and ReactJS

143 lines (109 loc) 3.6 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: index.jsx</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: index.jsx</h1> <section> <article> <pre class="prettyprint source linenums"><code>import React, { useMemo } from 'react'; import PropTypes from 'prop-types'; import KaTeX from 'katex'; /** * @typedef {import("react").ReactNode} ReactNode * * * @callback ErrorRenderer * @param {Error} error * @returns {ReactNode} * * * @typedef {object} MathComponentPropsWithMath * @property {string} math * @property {ReactNode=} children * @property {string=} errorColor * @property {ErrorRenderer=} renderError * * * @typedef {object} MathComponentPropsWithChildren * @property {string=} math * @property {ReactNode} children * @property {string=} errorColor * @property {ErrorRenderer=} renderError * * @typedef {MathComponentPropsWithMath | MathComponentPropsWithChildren} MathComponentProps */ const createMathComponent = (Component, { displayMode }) => { /** * * @param {MathComponentProps} props * @returns {ReactNode} */ const MathComponent = ({ children, errorColor, math, renderError }) => { const formula = math ?? children; const { html, error } = useMemo(() => { try { const html = KaTeX.renderToString(formula, { displayMode, errorColor, throwOnError: !!renderError, }); return { html, error: undefined }; } catch (error) { if (error instanceof KaTeX.ParseError || error instanceof TypeError) { return { error }; } throw error; } }, [formula, errorColor, renderError]); if (error) { return renderError ? renderError(error) : &lt;Component html={`${error.message}`} />; } return &lt;Component html={html} />; }; MathComponent.propTypes = { children: PropTypes.string, errorColor: PropTypes.string, math: PropTypes.string, renderError: PropTypes.func, }; return MathComponent; }; const InternalPathComponentPropTypes = { html: PropTypes.string.isRequired, }; const InternalBlockMath = ({ html }) => { return &lt;div data-testid="react-katex" dangerouslySetInnerHTML={{ __html: html }} />; }; InternalBlockMath.propTypes = InternalPathComponentPropTypes; const InternalInlineMath = ({ html }) => { return &lt;span data-testid="react-katex" dangerouslySetInnerHTML={{ __html: html }} />; }; InternalInlineMath.propTypes = InternalPathComponentPropTypes; export const BlockMath = createMathComponent(InternalBlockMath, { displayMode: true }); export const InlineMath = createMathComponent(InternalInlineMath, { displayMode: false }); </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3><a href="global.html">Global</a></h3> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Wed Aug 10 2022 20:21:11 GMT-0300 (Brasilia Standard Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>