2
2
/**
3
3
* For math rendered using KaTex and/or MathJax. Use me like <TeX>2x + 3</TeX>.
4
4
*/
5
- /* global MathJax, Khan */
6
- // TODO(joel) - require MathJax / katex so they don't have to be global
5
+ /* global MathJax */
7
6
8
7
const PureRenderMixin = require ( "react-addons-pure-render-mixin" ) ;
9
8
const React = require ( "react" ) ;
@@ -39,10 +38,24 @@ const unProcess = script => {
39
38
const loadMathJax = callback => {
40
39
if ( typeof MathJax !== "undefined" ) {
41
40
callback ( ) ;
42
- } else if ( typeof Khan !== "undefined" && Khan . mathJaxLoaded ) {
43
- Khan . mathJaxLoaded . then ( callback ) ;
44
41
} 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 ) ;
46
59
}
47
60
} ;
48
61
@@ -98,15 +111,6 @@ const TeX = createReactClass({
98
111
99
112
mixins : [ PureRenderMixin ] ,
100
113
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
-
110
114
getDefaultProps : function ( ) {
111
115
return {
112
116
katexOptions : {
@@ -124,6 +128,15 @@ const TeX = createReactClass({
124
128
} ;
125
129
} ,
126
130
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
+
127
140
componentDidMount : function ( ) {
128
141
this . _root = ReactDOM . findDOMNode ( this ) ;
129
142
@@ -160,27 +173,6 @@ const TeX = createReactClass({
160
173
}
161
174
} ,
162
175
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
-
184
176
componentDidUpdate : function ( prevProps , prevState ) {
185
177
if ( this . props . children !== prevProps . children ) {
186
178
this . maybeUnprocess ( ) ;
@@ -237,6 +229,27 @@ const TeX = createReactClass({
237
229
}
238
230
} ,
239
231
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
+
240
253
process : function ( callback ) {
241
254
this . hasProcessed = false ;
242
255
process ( this . script , ( ) => {
0 commit comments