Skip to content
This repository was archived by the owner on May 15, 2019. It is now read-only.

Commit 9ed4999

Browse files
author
Jesse Atkinson
committed
Update tex to include load-mathjax rather than rely on global
Summary: Please note that the linter complained about the order of a few methods so I had to move them around. Test Plan: Check out some fixtures that use tex.jsx such as `texified-code.jsx.fixture.js` and ensure everything renders properly. Reviewers: #webpack, john Reviewed By: #webpack, john Subscribers: john, kevinb, davidflanagan Differential Revision: https://phabricator.khanacademy.org/D49193
1 parent 62965bc commit 9ed4999

File tree

1 file changed

+48
-35
lines changed

1 file changed

+48
-35
lines changed

js/tex.jsx

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
/**
33
* For math rendered using KaTex and/or MathJax. Use me like <TeX>2x + 3</TeX>.
44
*/
5-
/* global MathJax, Khan */
6-
// TODO(joel) - require MathJax / katex so they don't have to be global
5+
/* global MathJax */
76

87
const PureRenderMixin = require("react-addons-pure-render-mixin");
98
const React = require("react");
@@ -39,10 +38,24 @@ const unProcess = script => {
3938
const loadMathJax = callback => {
4039
if (typeof MathJax !== "undefined") {
4140
callback();
42-
} else if (typeof Khan !== "undefined" && Khan.mathJaxLoaded) {
43-
Khan.mathJaxLoaded.then(callback);
4441
} else {
45-
throw new Error("MathJax wasn't loaded before it was needed by <TeX/>");
42+
/**
43+
* We can either...
44+
*
45+
* A) Reach up and out of `third_party` folder into our core webapp
46+
* code to include this `load-mathjax` file that, well, loads MathJax
47+
* or...
48+
*
49+
* B) Move this file into the KateX package next to `load-mathjax` and
50+
* then update everything that calls for this file to point at that
51+
* path instead.
52+
*
53+
* There is no easy or obvious solution. I am choosing A.
54+
*
55+
* - Jesse
56+
*/
57+
const loadMathJax = require("../../../../javascript/katex-package/load-mathjax.js");
58+
loadMathJax.then(callback);
4659
}
4760
};
4861

@@ -98,15 +111,6 @@ const TeX = createReactClass({
98111

99112
mixins: [PureRenderMixin],
100113

101-
// TODO(joshuan): Once we are using React 16.3+,
102-
// migrate to getDerivedStateFromProps
103-
getInitialState: function() {
104-
return {
105-
mounted: false,
106-
katexHtml: this.getKatexHtml(this.props),
107-
};
108-
},
109-
110114
getDefaultProps: function() {
111115
return {
112116
katexOptions: {
@@ -124,6 +128,15 @@ const TeX = createReactClass({
124128
};
125129
},
126130

131+
// TODO(joshuan): Once we are using React 16.3+,
132+
// migrate to getDerivedStateFromProps
133+
getInitialState: function() {
134+
return {
135+
mounted: false,
136+
katexHtml: this.getKatexHtml(this.props),
137+
};
138+
},
139+
127140
componentDidMount: function() {
128141
this._root = ReactDOM.findDOMNode(this);
129142

@@ -160,27 +173,6 @@ const TeX = createReactClass({
160173
}
161174
},
162175

163-
getKatexHtml(props) {
164-
// Try to render the math content with KaTeX.
165-
// If this fails, componentDidUpdate() will notice and
166-
// use MathJAX instead.
167-
try {
168-
return {
169-
__html: katex.renderToString(
170-
props.children,
171-
props.katexOptions,
172-
),
173-
};
174-
} catch (e) {
175-
/* jshint -W103 */
176-
if (e.__proto__ !== katex.ParseError.prototype) {
177-
/* jshint +W103 */
178-
throw e;
179-
}
180-
return null;
181-
}
182-
},
183-
184176
componentDidUpdate: function(prevProps, prevState) {
185177
if (this.props.children !== prevProps.children) {
186178
this.maybeUnprocess();
@@ -237,6 +229,27 @@ const TeX = createReactClass({
237229
}
238230
},
239231

232+
getKatexHtml(props) {
233+
// Try to render the math content with KaTeX.
234+
// If this fails, componentDidUpdate() will notice and
235+
// use MathJAX instead.
236+
try {
237+
return {
238+
__html: katex.renderToString(
239+
props.children,
240+
props.katexOptions,
241+
),
242+
};
243+
} catch (e) {
244+
/* jshint -W103 */
245+
if (e.__proto__ !== katex.ParseError.prototype) {
246+
/* jshint +W103 */
247+
throw e;
248+
}
249+
return null;
250+
}
251+
},
252+
240253
process: function(callback) {
241254
this.hasProcessed = false;
242255
process(this.script, () => {

0 commit comments

Comments
 (0)