简体   繁体   English

使用react.js动态mathjax

[英]dynamic mathjax with react.js

I tried to combine the markdown example on the react.js page and tried to have it render equations with mathjax. 我试图在react.js页面上组合markdown示例并尝试使用mathjax渲染方程式。 ( jsFiddle ) jsFiddle

Ideally it would take in an input 理想情况下,它会接受输入

 Type some *markd**o**wn* here! $$ int $$

and return the integral sign 并返回积分符号

Type some markd o wn here! 键入一些markdØWN这里!

The react.js code is taken directly from Facebook's page. react.js代码直接来自Facebook的页面。 I hope I called MathJax.Hub.Queue in the correct place -- their docs have a discussion of dynamic MathJax : 我希望我在正确的地方调用MathJax.Hub.Queue - 他们的文档讨论了动态MathJax

/** @jsx React.DOM */

var converter = new Showdown.converter();

var MarkdownEditor = React.createClass({
  getInitialState: function() {
    return {value: 'Type some *markdown* here! \\\( \int \\\)'};
  },
  handleChange: function() {
    this.setState({value: this.refs.textarea.getDOMNode().value});
  },
  render: function() {
    console.log(this.state.value);
    return (
      <div className="MarkdownEditor">
        <h3>Input</h3>
        <textarea
          onChange={this.handleChange}
          ref="textarea"
          id="input"
          defaultValue={this.state.value} />
        <h3>Output</h3>
        <div
          className="content"
          id="output"
          dangerouslySetInnerHTML={{
            __html: converter.makeHtml(this.state.value)
          }}
        />
      </div>
    );
  MathJax.Hub.Queue(["Typeset",MathJax.Hub,"output"]);
  }
});

React.renderComponent(<MarkdownEditor />,  document.getElementById('content'))

There is a similar example on the MathJax page that handles equations but not markdown. MathJax页面上有一个类似的例子来处理方程式而不是降价。

Your placement of the MathJax.Hub.Queue function is incorrect. 您对MathJax.Hub.Queue函数的放置不正确。 Note that it follows the return statement, so it never gets performed (since the function returns before getting to it). 请注意,它遵循return语句,因此永远不会执行(因为函数在到达之前返回)。 The documentation for the render() method of the React.js documentation suggests that it should not modify the DOM, so you don't want to do the typeset at this point anyway. React.js文档的render()方法的文档表明它不应该修改DOM,所以你不想在这一点上做排版。 And since you are returning the HTML string, it hasn't been added to the DOM yet anyway, so it would not be there for MathJax to process anyway. 既然你正在返回HTML字符串,那么它还没有被添加到DOM中,所以无论如何它都不会被MathJax处理。 The documentation suggests that componentDidMount() and componentDidUpdate() are the places where you should have MathJax typeset the new mathematical content. 文档建议componentDidMount()componentDidUpdate()是您应该让MathJax排版新数学内容的地方。

I've adjusted your jsFiddle example to include the changes. 我已经调整了你的jsFiddle示例以包含更改。

Note also that Markdown is going to interact with your backslashes, so it removes the ones for the math delimiters \\(...\\) and you just get (...) , so MathJa won't see them. 另请注意,Markdown将与您的反斜杠进行交互,因此它会删除数学分隔符\\(...\\)并且您只得到(...) ,因此MathJa将不会看到它们。 I've reconfigured MathJax using MathJax.Hub.Config() to use dollar sign delimiters $...$ for in-line math (and the default $$...$$ for displayed math). 我使用MathJax.Hub.Config()重新配置了MathJax,使用美元符号分隔符$...$进行内联数学运算(以及显示数学运算的默认$$...$$ )。 Otherwise you will need to type \\\\(...\\\\) to get the backslashes into the Markdown output where MathJax can see them (and \\\\\\\\( \\int \\\\\\\\) in your initial string). 否则,您需要输入\\\\(...\\\\)以将反斜杠输入到Markdown输出中,MathJax可以在其中看到它们(以及初始字符串中的\\\\\\\\( \\int \\\\\\\\) )。 You can, of course, configure MathJax to use whatever delimiters you want, but dollars are the plain TeX approach. 当然,您可以将MathJax配置为使用您想要的任何分隔符,但美元是普通的TeX方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM