All files / src createMathComponent.js

93.75% Statements 15/16
91.66% Branches 11/12
100% Functions 3/3
93.75% Lines 15/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47        2x 4x 32x   32x   32x 32x 32x           22x   10x 10x             32x 10x     22x     4x             4x        
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import KaTeX from 'katex';
 
const createMathComponent = (Component, { displayMode }) => {
  const MathComponent = ({ children, errorColor, math, renderError }) => {
    !math && console.log(children);
 
    const formula = math ?? children;
 
    const { html, error } = useMemo(() => {
      try {
        const html = KaTeX.renderToString(formula, {
          displayMode,
          errorColor,
          throwOnError: !!renderError,
        });
 
        return { html, error: undefined };
      } catch (error) {
        Eif (error instanceof KaTeX.ParseError || error instanceof TypeError) {
          return { error };
        }
 
        throw error;
      }
    }, [formula, errorColor, renderError]);
 
    if (error) {
      return renderError ? renderError(error) : <Component html={`${error.message}`} />;
    }
 
    return <Component html={html} />;
  };
 
  MathComponent.propTypes = {
    children: PropTypes.string,
    errorColor: PropTypes.string,
    math: PropTypes.string,
    renderError: PropTypes.func,
  };
 
  return MathComponent;
};
 
export default createMathComponent;