diff --git a/package.json b/package.json index 8c264ce98..c8856f6f0 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "color": "^3.1.0", "fast-isnumeric": "^1.1.3", "highlight.js": "^9.17.1", + "mathjax": "^3.0.0", "moment": "^2.20.1", "plotly.js": "1.52.2", "prop-types": "^15.6.0", @@ -50,6 +51,7 @@ "react-resize-detector": "^4.2.1", "react-select-fast-filter-options": "^0.2.3", "react-virtualized-select": "^3.1.3", + "remark-math": "^1.0.6", "uniqid": "^5.0.3" }, "devDependencies": { diff --git a/src/fragments/Markdown.react.js b/src/fragments/Markdown.react.js index 84854e7a7..a258a1f11 100644 --- a/src/fragments/Markdown.react.js +++ b/src/fragments/Markdown.react.js @@ -2,7 +2,9 @@ import React, {Component} from 'react'; import {mergeDeepRight, pick, type} from 'ramda'; import JsxParser from 'react-jsx-parser'; import Markdown from 'react-markdown'; +import RemarkMathPlugin from 'remark-math'; +import Math from './Math.react'; import MarkdownHighlighter from '../utils/MarkdownHighlighter'; import {propTypes, defaultProps} from '../components/Markdown.react'; @@ -131,7 +133,10 @@ export default class DashMarkdown extends Component { , + inlineMath: props => , html: props => props.escapeHtml ? ( props.value diff --git a/src/fragments/Math.react.js b/src/fragments/Math.react.js new file mode 100644 index 000000000..d1f93bab5 --- /dev/null +++ b/src/fragments/Math.react.js @@ -0,0 +1,34 @@ +import React, {Component} from 'react'; +import _ from 'mathjax/es5/tex-svg'; + + +export default class Math extends Component { + constructor(props) { + super(props); + this.span_element = React.createRef(); + } + + componentDidMount() { + this.renderMath(); + } + + componentDidUpdate(prevProps) { + if (prevProps.tex !== this.props.tex || prevProps.inline !== this.props.inline){ + this.renderMath(); + } + } + + renderMath() { + window.MathJax.typesetPromise([this.span_element.current]); + } + + render() { + return ( + + {this.props.inline ? String.raw`\(` : String.raw`\[`} + {this.props.tex} + {this.props.inline ? String.raw`\)` : String.raw`\]`} + + ) + } +}