mehdisadeghi / react-mathjax-preview

The MathJax React component you were looking for.
https://mehdisadeghi.github.io/react-mathjax-preview/
MIT License
49 stars 30 forks source link

Integrate with jsx MathML #6

Closed ryanehlers closed 4 years ago

ryanehlers commented 6 years ago

In React it's possible to return MathML from a jsx function like:

import React from 'react';

function oneThird() {
  return (
    <math>
      <mfrac>
        <mn>1</mn>
        <mn>3</mn>
      </mfrac>
    </math>
  );
}

Is it possible to integrate this with the <MathJax /> component?

My current work-around is to convert the MathML to a string with ReactDOMServer.renderToString():

render() {
  <MathJax math={ReactDOMServer.renderToString(oneThird())} />;
}
mehdisadeghi commented 6 years ago

@ryanehlers if you put raw MathML inside a react component, react will render it, however for MathJax to function properly we need to pass String to the underlying MathJax library.

I thought perhaps one could do something like this:

<MathJax>
    <math>
      <mfrac>
        <mn>1</mn>
        <mn>3</mn>
      </mfrac>
    </math>
<MathJax>

For this to work we need to extract children as string. With a quick search I found jsx-to-string but I was unable to extract the MathML.

If you can fix it, send me a PR. I'd be happy to merge it!

mehdisadeghi commented 4 years ago

I'll close this as a wontfix since the component (and MathJax) needs a string for typesetting. Moreover, your workaround looks perfectly fine to me. There is also Algolia's react-element-to-jsx-string which might be interesting.