better-react-mathjax 1.0.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/MathJax3.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export {
2
+ MathJaxConfig as MathJax3Config,
3
+ MathJaxObject as MathJax3Object
4
+ } from "mathjax-full/js/components/startup"
5
+ export { OptionList } from "mathjax-full/js/util/Options"
@@ -1,8 +1,6 @@
1
1
  import React, { FC } from "react";
2
- import type { MathJax2Config, MathJax2Object } from "./MathJax2";
3
- import type { MathJaxObject as MathJax3Object, MathJaxConfig as MathJax3Config } from "mathjax-full/js/components/startup";
4
- import type { OptionList } from "mathjax-full/js/util/Options";
5
- export type { MathJax3Object, MathJax3Config, OptionList };
2
+ import type { MathJax2Config, MathJax2Object } from "../MathJax2";
3
+ import type { MathJax3Config, MathJax3Object, OptionList } from "../MathJax3";
6
4
  export declare type TypesettingFunction = "tex2chtml" | "tex2chtmlPromise" | "tex2svg" | "tex2svgPromise" | "tex2mml" | "tex2mmlPromise" | "mathml2chtml" | "mathml2chtmlPromise" | "mathml2svg" | "mathml2svgPromise" | "mathml2mml" | "mathml2mmlPromise" | "asciimath2chtml" | "asciimath2chtmlPromise" | "asciimath2svg" | "asciimath2svgPromise" | "asciimath2mml" | "asciimath2mmlPromise";
7
5
  export interface MathJaxOverrideableProps {
8
6
  hideUntilTypeset?: "first" | "every";
@@ -19,13 +17,7 @@ export declare type MathJaxSubscriberProps = ({
19
17
  version: 3;
20
18
  promise: Promise<MathJax3Object>;
21
19
  }) & MathJaxOverrideableProps;
22
- export declare const MathJaxBaseContext: React.Context<({
23
- version: 2;
24
- promise: Promise<MathJax2Object>;
25
- } & MathJaxOverrideableProps) | ({
26
- version: 3;
27
- promise: Promise<MathJax3Object>;
28
- } & MathJaxOverrideableProps) | undefined>;
20
+ export declare const MathJaxBaseContext: React.Context<MathJaxSubscriberProps | undefined>;
29
21
  interface MathJaxContextStaticProps extends MathJaxOverrideableProps {
30
22
  src?: string;
31
23
  onLoad?: () => void;
@@ -0,0 +1 @@
1
+ "use strict";var v2Promise,v3Promise,__assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e}).apply(this,arguments)},__createBinding=this&&this.__createBinding||(Object.create?function(e,t,n,o){void 0===o&&(o=n);var r=Object.getOwnPropertyDescriptor(t,n);r&&("get"in r?t.__esModule:!r.writable&&!r.configurable)||(r={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,o,r)}:function(e,t,n,o){e[o=void 0===o?n:o]=t[n]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)"default"!==n&&Object.prototype.hasOwnProperty.call(e,n)&&__createBinding(t,e,n);return __setModuleDefault(t,e),t},react_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.MathJaxBaseContext=void 0,__importStar(require("react"))),DEFAULT_V2_SRC=(exports.MathJaxBaseContext=(0,react_1.createContext)(void 0),"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/MathJax.js?config=TeX-MML-AM_CHTML"),DEFAULT_V3_SRC="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.0/es5/tex-mml-chtml.js",MathJaxContext=function(e){var o=e.config,t=e.version,t=void 0===t?3:t,n=e.src,n=void 0===n?2===t?DEFAULT_V2_SRC:DEFAULT_V3_SRC:n,r=e.onStartup,a=e.onLoad,i=e.onError,s=e.typesettingOptions,u=e.renderMode,u=void 0===u?"post":u,c=e.hideUntilTypeset,e=e.children,d=(0,react_1.useContext)(exports.MathJaxBaseContext);if(void 0!==(null==d?void 0:d.version)&&(null==d?void 0:d.version)!==t)throw Error("Cannot nest MathJaxContexts with different versions. MathJaxContexts should not be nested at all but if they are, they cannot have different versions. Stick with one version of MathJax in your app and avoid using more than one MathJaxContext.");if(2===t&&void 0!==v3Promise||3===t&&void 0!==v2Promise)throw Error("Cannot use MathJax versions 2 and 3 simultaneously in the same app due to how MathJax is set up in the browser; either you have multiple MathJaxContexts with different versions or you have mounted and unmounted MathJaxContexts with different versions. Please stick with one version of MathJax in your app. File an issue in the project Github page if you need this feature.");var h=(0,react_1.useRef)(d),d=(0,react_1.useRef)((null==d?void 0:d.version)||null);if(null===d.current)d.current=t;else if(d.current!==t)throw Error("Cannot change version of MathJax in a MathJaxContext after component has mounted. Either reload the page with a new setting when this should happen or use multiple, non-nested, MathJaxContexts in your app.");var l=n||(2===t?DEFAULT_V2_SRC:DEFAULT_V3_SRC);function f(t,n){o&&(window.MathJax=o);var e=document.createElement("script");e.type="text/javascript",e.src=l,e.async=!1,e.addEventListener("load",function(){var e=window.MathJax;r&&r(e),t(e),a&&a()}),e.addEventListener("error",function(e){return n(e)}),document.getElementsByTagName("head")[0].appendChild(e)}return void 0===h.current&&(d={typesettingOptions:s,renderMode:u,hideUntilTypeset:c},2===t?void 0===v2Promise&&("undefined"!=typeof window?(v2Promise=new Promise(f)).catch(function(e){if(!i)throw Error("Failed to download MathJax version 2 from '".concat(l,"' due to: ").concat(e));i(e)}):(v2Promise=Promise.reject()).catch(function(e){})):void 0===v3Promise&&("undefined"!=typeof window?(v3Promise=new Promise(f)).catch(function(e){if(!i)throw Error("Failed to download MathJax version 3 from '".concat(l,"' due to: ").concat(e));i(e)}):(v3Promise=Promise.reject()).catch(function(e){})),h.current=__assign(__assign({},d),2===t?{version:2,promise:v2Promise}:{version:3,promise:v3Promise})),react_1.default.createElement(exports.MathJaxBaseContext.Provider,{value:h.current},e)};exports.default=MathJaxContext;
@@ -0,0 +1 @@
1
+ export { default, default as MathJaxContext, MathJaxOverrideableProps, MathJaxContextProps, MathJaxBaseContext, MathJaxSubscriberProps } from "./MathJaxContext";
@@ -0,0 +1 @@
1
+ "use strict";var __importDefault=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}},MathJaxContext_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.MathJaxBaseContext=exports.MathJaxContext=exports.default=void 0,require("./MathJaxContext"));Object.defineProperty(exports,"default",{enumerable:!0,get:function(){return __importDefault(MathJaxContext_1).default}}),Object.defineProperty(exports,"MathJaxContext",{enumerable:!0,get:function(){return __importDefault(MathJaxContext_1).default}}),Object.defineProperty(exports,"MathJaxBaseContext",{enumerable:!0,get:function(){return MathJaxContext_1.MathJaxBaseContext}});
@@ -0,0 +1,5 @@
1
+ {
2
+ "sideEffects": false,
3
+ "module": "../esm/MathJaxContext/index.js",
4
+ "typings": "./index.d.ts"
5
+ }
package/README.md CHANGED
@@ -1,6 +1,4 @@
1
1
 
2
- <!-- prettier-ignore-start -->
3
-
4
2
  # A simple React component for MathJax #
5
3
 
6
4
  #### Note: The full documentation is available at https://github.com/fast-reflexes/better-react-mathjax ####
@@ -9,23 +7,17 @@ Up-to-date component for using MathJax in latest React (using functional compone
9
7
  React a pleasant experience without flashes of non-typeset content, both with respect to initial rendering as
10
8
  well as dynamic updates. Simple to use but with many configuration options.
11
9
 
12
- ## Basic workflow ##
13
-
14
- `better-react-mathjax` introduces two React components - `MathJaxContext` and `MathJax`. For MathJax to work with React,
15
- wrap the outermost component containing math (or the entire app) in a `MathJaxContext` component. Then simply use `MathJax` components at
16
- different levels for the actual math. In the typical case, the content of a `MathJax` component can be everything from a
17
- subtree of the DOM to a portion of text in a long paragraph. The `MathJaxContext` is responsible for downloading MathJax
18
- and providing it to all wrapped `MathJax` components that typeset math.
19
-
20
- ### Features ###
10
+ ## Features
21
11
 
22
12
  * Supports both MathJax version 2 and 3.
23
13
  * Supports local copy of MathJax or copy supplied via CDN.
24
- * Small imprint on production bundle with dependencies only for types (image shows a size of 7.32 KB and 2.37 KB gzipped in a NextJS project analyzed with their bundle analyzer).
14
+ * Small imprint on production bundle with dependencies only for types (image shows a size of 6.18 KB and 2.3 KB gzipped in a NextJS project analyzed with their bundle analyzer).
25
15
 
16
+ <br/>
26
17
  <p align="center" width="100%">
27
- <img align="center" src="https://github.com/fast-reflexes/better-react-mathjax/blob/master/bundle_imprint.png" width="75%">
18
+ <img align="center" src="https://github.com/fast-reflexes/better-react-mathjax/blob/master/images/bundle_imprint.png" width="75%">
28
19
  </p>
20
+ <br/>
29
21
 
30
22
  * Built in a modular fashion on top of MathJax with direct access to MathJax via the MathJax configuration.
31
23
  * Use MathJax functionality either through the `MathJax` component or by yourself through the `MathJaxBaseContext`.
@@ -33,25 +25,77 @@ and providing it to all wrapped `MathJax` components that typeset math.
33
25
  first and add it to the DOM afterwards (v. 3 only).
34
26
  * Hide your components before they are typeset to avoid flashes of non-typeset content and make the use of MathJax a
35
27
  pleasant experience.
36
-
37
- ### Installation ###
28
+ * Complete - no other dependencies related to MathJax are needed to enable the use of MathJax in your React app.
38
29
 
39
- Add this library manually as a dependency to `package.json`...
30
+ ## Basic workflow
40
31
 
41
- dependencies: {
42
- "better-react-mathjax": "^1.0.3"
43
- }
32
+ ### Installation
44
33
 
45
- ... and then run `npm install` **or** let `npm` or `yarn` do it for you, depending on which package manager you have
34
+ Add this library manually as a dependency to `package.json`...
35
+ ```
36
+ dependencies: {
37
+ "better-react-mathjax": "^2.0.0"
38
+ }
39
+ ```
40
+ ... and then run `npm install` **or** let `npm` or `yarn` do it for you, depending on which package manager you have
46
41
  chosen to use:
47
-
48
- # npm
49
- npm install better-react-mathjax
50
-
51
- # yarn
52
- yarn add better-react-mathjax
53
-
54
- ## Examples ##
42
+ ```shell
43
+ # npm
44
+ npm install better-react-mathjax
45
+
46
+ # yarn
47
+ yarn add better-react-mathjax
48
+ ```
49
+
50
+ ### Usage
51
+
52
+ `better-react-mathjax` introduces two React components - `MathJaxContext` and `MathJax`. For MathJax to work with React:
53
+
54
+ 1. Wrap your entire app in a `MathJaxContext` component (***only use one in your app***).
55
+ ```js
56
+ const App = () => {
57
+
58
+ return (
59
+ <MathJaxContext>
60
+ <!-- APP CONTENT -->
61
+ </MathJaxContext>
62
+ )
63
+ }
64
+ ```
65
+ 2. Then simply use `MathJax` components at different levels for the actual math.
66
+ ```js
67
+ const Component = () => {
68
+
69
+ return (
70
+ <div>
71
+ <MathJax>{ /* math content */ }</MathJax>
72
+ <h3>This is a header</h3>
73
+ <MathJax>
74
+ <div>
75
+ <h4>This is a subheader</h4>
76
+ <span>{ /* math content */ }</span>
77
+ <h4>This is a second subheader</h4>
78
+ <span>{ /* math content */ }</span>
79
+ ...
80
+ </div>
81
+ </MathJax>
82
+ <p>
83
+ This is text which involves math <MathJax>{ /* math content */ }</MathJax> inside the paragraph.
84
+ </p>
85
+ </div>
86
+ )
87
+ }
88
+ ```
89
+ In the typical case, the content of a `MathJax` component can be everything from a subtree of the DOM to a portion of
90
+ text in a long paragraph. If you have a lot of math, try to wrap as much as possible in the same `MathJax` component.
91
+ The `MathJaxContext` is responsible for downloading MathJax and providing it to all wrapped `MathJax` components that
92
+ typeset math. By default, `MathJaxContext` imports MathJax from a CDN which allows for use of Latex, AsciiMath and MathML
93
+ with [MathJax version 2](https://docs.mathjax.org/en/v2.7-latest/config-files.html#the-tex-mml-am-chtml-configuration-file) and
94
+ Latex and MathML with the default [MathJax version 3](https://docs.mathjax.org/en/latest/web/components/combined.html#tex-mml-chtml) with
95
+ HTML output for both. If you need something else or want to host your own copy of MathJax, read more about the `src`
96
+ attribute of the `MathJaxContext` below.
97
+
98
+ ## Examples
55
99
 
56
100
  The first 3 are basic examples with zero configuration standard setup using MathJax version 3 with default MathJax config
57
101
  and no extra options. Note that sandboxes tend to be slower than use in a real environment.
@@ -59,36 +103,39 @@ and no extra options. Note that sandboxes tend to be slower than use in a real e
59
103
  ### Example 1: Basic example with Latex ####
60
104
 
61
105
  Standard setup using MathJax version 3 with default MathJax config and no extra options.
62
-
63
- export default function App() {
106
+ ```js
107
+ export default function App() {
108
+
109
+ return (
110
+ <MathJaxContext>
111
+ <h2>Basic MathJax example with Latex</h2>
112
+ <MathJax>{"\\(\\frac{10}{4x} \\approx 2^{12}\\)"}</MathJax>
113
+ </MathJaxContext>
114
+ );
64
115
 
65
- return (
66
- <MathJaxContext>
67
- <h2>Basic MathJax example with Latex</h2>
68
- <MathJax>{"\\(\\frac{10}{4x} \\approx 2^{12}\\)"}</MathJax>
69
- </MathJaxContext>
70
- );
71
-
116
+ }
117
+ ```
72
118
  Sandbox: https://codesandbox.io/s/better-react-mathjax-basic-example-latex-bj8gd
73
119
 
74
120
  ### Example 2: Basic example with AsciiMath ####
75
121
 
76
- Using AsciiMath requires importing a specific loader (see the [MathJax documentation](http://docs.mathjax.org/en/latest/input/asciimath.html) for further information).
77
- AsciiMath uses the same display mode on the entire page, which is display math by default.
122
+ Using AsciiMath with the default version 3 import requires adding an extra loader (see the [MathJax documentation](http://docs.mathjax.org/en/latest/input/asciimath.html)
123
+ for further information). AsciiMath uses the same display mode on the entire page, which is display math by default.
78
124
  It can be changed to inline math by adding `asciimath: { displaystyle: false }` to the input config.
79
-
80
- export default function App() {
81
- const config = {
82
- loader: { load: ["input/asciimath"] }
83
- };
84
-
85
- return (
86
- <MathJaxContext config={config}>
87
- <h2>Basic MathJax example with AsciiMath</h2>
88
- <MathJax>{"`frac(10)(4x) approx 2^(12)`"}</MathJax>
89
- </MathJaxContext>
90
- );
91
- }
125
+ ```js
126
+ export default function App() {
127
+ const config = {
128
+ loader: { load: ["input/asciimath"] }
129
+ };
130
+
131
+ return (
132
+ <MathJaxContext config={config}>
133
+ <h2>Basic MathJax example with AsciiMath</h2>
134
+ <MathJax>{"`frac(10)(4x) approx 2^(12)`"}</MathJax>
135
+ </MathJaxContext>
136
+ );
137
+ }
138
+ ```
92
139
 
93
140
  Sandbox: https://codesandbox.io/s/better-react-mathjax-basic-example-asciimath-ddy4r
94
141
 
@@ -96,34 +143,34 @@ Sandbox: https://codesandbox.io/s/better-react-mathjax-basic-example-asciimath-d
96
143
 
97
144
  MathML is supported natively by a few but far from all browsers. It might be problematic to use with Typescript (no types for
98
145
  MathML included in this package).
99
-
100
- export default function App() {
101
- return (
102
- <MathJaxContext>
103
- <h2>Basic MathJax example with MathML</h2>
104
- <MathJax>
105
- <math>
146
+ ```js
147
+ export default function App() {
148
+ return (
149
+ <MathJaxContext>
150
+ <h2>Basic MathJax example with MathML</h2>
151
+ <MathJax>
152
+ <math>
153
+ <mrow>
106
154
  <mrow>
107
- <mrow>
108
- <mfrac>
109
- <mn>10</mn>
110
- <mi>4x</mi>
111
- </mfrac>
112
- </mrow>
113
- <mo>&asymp;</mo>
114
- <mrow>
115
- <msup>
116
- <mn>2</mn>
117
- <mn>12</mn>
118
- </msup>
119
- </mrow>
155
+ <mfrac>
156
+ <mn>10</mn>
157
+ <mi>4x</mi>
158
+ </mfrac>
120
159
  </mrow>
121
- </math>
122
- </MathJax>
123
- </MathJaxContext>
124
- );
125
- }
126
-
160
+ <mo>&asymp;</mo>
161
+ <mrow>
162
+ <msup>
163
+ <mn>2</mn>
164
+ <mn>12</mn>
165
+ </msup>
166
+ </mrow>
167
+ </mrow>
168
+ </math>
169
+ </MathJax>
170
+ </MathJaxContext>
171
+ );
172
+ }
173
+ ```
127
174
  Sandbox: https://codesandbox.io/s/better-react-mathjax-basic-example-mathml-20vv6
128
175
 
129
176
  ### Example 4: Elaborate example with Latex ###
@@ -133,11 +180,18 @@ Sandbox: https://codesandbox.io/s/better-react-mathjax-example-latex-3vsr5
133
180
  Sandbox: https://codesandbox.io/s/better-react-mathjax-example-asciimath-p0uf1
134
181
 
135
182
  ### Example 6: Elaborate example with MathML ###
136
- Sandbox link: https://codesandbox.io/s/better-react-mathjax-example-mathml-nprxz
137
183
 
138
184
  Make sure to study the comments in this file as MathML processing is a little bit different from Latex and AsciiMath.
139
185
 
186
+ Sandbox link: https://codesandbox.io/s/better-react-mathjax-example-mathml-nprxz
187
+
140
188
  ### Example 7: Elaborate example with optimal settings for dynamic updates with Latex ###
189
+
190
+ This example shows a configuration that in some particular cases has proven to result in a very smooth experience with
191
+ no flashes of non-typeset content. **It is by no means recommended as a first attempt and can be tried if you
192
+ experience problems with flashes of non-typeset content, long waiting times or other undesired behaviour**. Especially for
193
+ those using MathJax version 2, some of the configuration options can be used as an inspiration.
194
+
141
195
  Sandbox link: https://codesandbox.io/s/better-react-mathjax-example-latex-optimal-8nn9n
142
196
 
143
197
  # TypeScript types #
@@ -196,7 +250,6 @@ MathJax configuration object. Make sure it corresponds to the version used. More
196
250
 
197
251
  The location of MathJax.
198
252
 
199
-
200
253
  **Default**: `undefined` (default CDN `https://cdnjs.cloudflare.com` is used)
201
254
 
202
255
  Local or remote url to fetch MathJax from. More information about hosting your own copy of MathJax can be found
@@ -205,11 +258,14 @@ Local or remote url to fetch MathJax from. More information about hosting your o
205
258
 
206
259
  A source url may contain both some specific file and some query parameters corresponding to a configuration which, in turn, governs
207
260
  which additional assets MathJax fetches. The default sources used when this property is omitted are the same as those
208
- listed in the [MathJax instruction](https://www.mathjax.org/#gettingstarted) (however from a different CDN). These correspond
209
- to some typical and broad use of MathJax. If you have a use case where you, using standalone MathJax, would have to use a different
210
- source url, then you have to manually supply such a url (local or remote) here. This, in analogy to how you would modify
211
- the script import to adjust to your needs in a plain HTML environment with direct use of MathJax. Read more about different
212
- configurations [here](https://docs.mathjax.org/en/latest/web/components/combined.html) (for MathJax 3) and
261
+ listed in the [MathJax instruction](https://www.mathjax.org/#gettingstarted) (however from a different CDN). This means
262
+ that for version 2, the fetched resource (`MathJax.js?config=TeX-MML-AM_CHTML`) includes support for Latex, MML
263
+ and AsciiMath with HTML output by default, and for version 3, the fetched resource (`tex-mml-chtml.js`) supports
264
+ MML and Latex with HTML output. These correspond to some typical and broad use of MathJax in the browser. If you have a
265
+ use case where you, using standalone MathJax, would have to use a different source url, then you have to manually supply
266
+ such a url (local or remote) here. This, in analogy to how you would modify the script import to adjust to your needs in
267
+ a plain HTML environment with direct use of MathJax. Read more about different configurations
268
+ [here](https://docs.mathjax.org/en/latest/web/components/combined.html) (for MathJax 3) and
213
269
  [here](https://docs.mathjax.org/en/v2.7-latest/config-files.html#common-configurations) (for MathJax 2).
214
270
 
215
271
  ### `version: 2 | 3 | undefined` ###
@@ -290,20 +346,20 @@ as this functionality is used by the `MathJax` component itself.***
290
346
  ## Custom use of MathJax directly ##
291
347
  You can use the underlying MathJax object directly (not through the `MathJax` component) if you want as well. The
292
348
  following snippet illustrates how to use `MathJaxBaseContext` to accomplish this.
293
-
294
- // undefined or MathJaxSubscriberProps with properties version, hideUntilTypeset, renderMode, typesettingOptions and promise
295
- const mjContext = useContext(MathJaxBaseContext)
296
- if(mjContext)
297
- mjContext.promise.then(mathJaxObject => { // do work with the MathJax object here })
298
-
349
+ ```js
350
+ // undefined or MathJaxSubscriberProps with properties version, hideUntilTypeset, renderMode, typesettingOptions and promise
351
+ const mjContext = useContext(MathJaxBaseContext)
352
+ if(mjContext)
353
+ mjContext.promise.then(mathJaxObject => { /* do work with the MathJax object here */ })
354
+ ```
299
355
  This requires only a `MathJaxContext`, supplying the `MathJaxBaseContext`, to be in the hierarchy. The object passed from the `promise` property is the MathJax
300
356
  object for the version in use.
301
357
 
302
358
  Sandbox example: https://codesandbox.io/s/better-react-mathjax-custom-example-latex-e5kym
303
359
 
304
360
  ## MathJax documentation ##
305
- * Version 3: https://docs.mathjax.org/en/latest/
306
361
 
362
+ * Version 3: https://docs.mathjax.org/en/latest/
307
363
  * Version 2: https://docs.mathjax.org/en/v2.7-latest/
308
364
 
309
365
  ## Github ##
@@ -326,10 +382,38 @@ Read full documentation, file problems or contribute on Github: https://github.c
326
382
  * Added installation instructions.
327
383
  * Added information about AsciiMath display mode.
328
384
  * Corrected typo in API documentation on `version` property.
385
+ * v. 2.0.0
386
+ * **Breaking change**: can no longer use MathJax versions 2 and 3 side by side in different
387
+ `MathJaxContext`s. This did typically not work as intended before either and even though this change is
388
+ breaking, it should not affect many users, if any. Docs have been updated and `MathJaxContext` now throws an error
389
+ if it has been initialized with one version and another `MathJaxContext` (later or simultaneous) has a different
390
+ value on the `version` prop.
391
+ * Added support for hot reload by always setting `dynamic` to `true` when `dynamic` is not explicitly set to `false`
392
+ and `process.env.NODE_ENV` is not `production`.
393
+ * Honoring possible style settings of `style.visibility` in `MathJax` component when visibility is not controlled
394
+ by the component itself.
395
+ * Corrected sandbox example with AsciiMath where Latex default delimiter was used instead of AsciiMath delimiter
396
+ (still worked because Latex is loaded by default as well).
397
+ * Improved documentation with clarifications:
398
+ * The entire app should be wrapped in the only `MathJaxContext` that should exist in a project.
399
+ * How to deal with the situation when you have a lot of math on your page.
400
+ * The features available with the default imports from CDN.
401
+ * Difference between inline and display math.
402
+ * Added Q & A section to documentation.
403
+ * Changed all checks for undefined to use `typeof X === "undefined"` instead of `X === undefined`.
404
+ * Added support for esm modules as well as the previous cjs modules.
405
+ * Added language tags on code samples
406
+
407
+ ## Migration guides
408
+
409
+ * v1 to v2:
410
+ * Remove all simultaneous use of MathJax version 2 and 3 in the same app and use only one version of MathJax
411
+ per loaded page. This means that even if you unmount the `MathJaxContext` and then remount it, it must have the
412
+ same version both times. If you load a new page context (e.g. not just change page in a SPA (single-page application))
413
+ the version can be determined anew. If you need to use both versions side by side, file an issue on the project
414
+ Github page.
329
415
 
330
416
  ## License
331
417
 
332
418
  This project is licensed under the terms of the
333
419
  [MIT license](https://github.com/fast-reflexes/better-react-mathjax/blob/master/LICENSE).
334
-
335
- <!-- prettier-ignore-end -->
@@ -0,0 +1 @@
1
+ var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)},__rest=this&&this.__rest||function(t,e){var n={};for(i in t)Object.prototype.hasOwnProperty.call(t,i)&&e.indexOf(i)<0&&(n[i]=t[i]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols)for(var r=0,i=Object.getOwnPropertySymbols(t);r<i.length;r++)e.indexOf(i[r])<0&&Object.prototype.propertyIsEnumerable.call(t,i[r])&&(n[i[r]]=t[i[r]]);return n};import React,{useContext,useEffect,useLayoutEffect,useRef}from"react";import{MathJaxBaseContext}from"../MathJaxContext";var typesettingFailed=function(t){return"Typesetting failed: ".concat(void 0!==t.message?t.message:t.toString())},MathJax=function(t){function r(){var t;"every"===h&&m&&"post"===v&&null!==f.current&&(f.current.style.visibility=null!=(t=null==(t=p.style)?void 0:t.visibility)?t:"visible"),_.current||("first"===h&&null!==f.current&&(f.current.style.visibility="visible"),n&&n(),_.current=!0),o&&o(),b.current=!1}var e=t.inline,i=void 0!==e&&e,e=t.hideUntilTypeset,n=t.onInitTypeset,o=t.onTypeset,s=t.text,u=t.dynamic,a=t.typesettingOptions,l=t.renderMode,c=t.children,p=__rest(t,["inline","hideUntilTypeset","onInitTypeset","onTypeset","text","dynamic","typesettingOptions","renderMode","children"]),y=useRef(""),f=useRef(null),d=useContext(MathJaxBaseContext),h=null!=e?e:null==d?void 0:d.hideUntilTypeset,v=null!=l?l:null==d?void 0:d.renderMode,g=null!=a?a:null==d?void 0:d.typesettingOptions,m=!1!==u&&(u||"production"!==process.env.NODE_ENV),_=useRef(!1),b=useRef(!1);return!b.current&&null!==f.current&&m&&"every"===h&&"post"===v&&(f.current.style.visibility="hidden"),("undefined"!=typeof window?useLayoutEffect:useEffect)(function(){if((m||!_.current)&&null!==f.current){if(!d)throw Error("MathJax was not loaded, did you use the MathJax component outside of a MathJaxContext?");if("pre"===v){if(!("string"==typeof(t=s)&&0<t.length))throw Error("Render mode 'pre' requires text prop to be set and non-empty, which was currently \"".concat(s,'"'));if(!a||!a.fn)throw Error("Render mode 'pre' requires 'typesettingOptions' prop with 'fn' property to be set on MathJax element or in the MathJaxContext");if(2===d.version)throw Error("Render mode 'pre' only available with MathJax 3, and version 2 is currently in use")}"post"!==v&&s===y.current||b.current||(b.current=!0,3===d.version?d.promise.then(function(e){var n;"pre"===v?(n=function(t){y.current=s,e.startup.document.clear(),e.startup.document.updateDocument(),null!==f.current&&(f.current.innerHTML=t.outerHTML),r()},a.fn.endsWith("Promise")?e.startup.promise.then(function(){return e[g.fn](s,__assign(__assign({},(null==g?void 0:g.options)||{}),{display:!i}))}).then(n).catch(function(t){throw r(),Error(typesettingFailed(t))}):e.startup.promise.then(function(){var t=e[g.fn](s,__assign(__assign({},(null==g?void 0:g.options)||{}),{display:!i}));n(t)}).catch(function(t){throw r(),Error(typesettingFailed(t))})):e.startup.promise.then(function(){return e.typesetClear([f.current]),e.typesetPromise([f.current])}).then(r).catch(function(t){throw r(),Error(typesettingFailed(t))})}).catch(function(t){throw r(),Error(typesettingFailed(t))}):d.promise.then(function(t){t.Hub.Queue(["Typeset",t.Hub,f.current]),t.Hub.Queue(r)}).catch(function(t){throw r(),Error(typesettingFailed(t))}))}var t}),React.createElement("span",__assign({},p,{style:__assign(__assign({display:i?"inline":"block"},p.style),{visibility:h?"hidden":null==(t=p.style)?void 0:t.visibility}),ref:f}),c)};export default MathJax;
@@ -0,0 +1 @@
1
+ export{default as MathJax,default}from"./MathJax";
@@ -0,0 +1 @@
1
+ var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var t,o=1,n=arguments.length;o<n;o++)for(var a in t=arguments[o])Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a]);return e}).apply(this,arguments)};import React,{createContext,useContext,useRef}from"react";var v2Promise,v3Promise,MathJaxBaseContext=createContext(void 0),DEFAULT_V2_SRC="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/MathJax.js?config=TeX-MML-AM_CHTML",DEFAULT_V3_SRC="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.0/es5/tex-mml-chtml.js",MathJaxContext=function(e){var n=e.config,t=e.version,t=void 0===t?3:t,o=e.src,o=void 0===o?2===t?DEFAULT_V2_SRC:DEFAULT_V3_SRC:o,a=e.onStartup,r=e.onLoad,i=e.onError,s=e.typesettingOptions,h=e.renderMode,h=void 0===h?"post":h,d=e.hideUntilTypeset,e=e.children,c=useContext(MathJaxBaseContext);if(void 0!==(null==c?void 0:c.version)&&(null==c?void 0:c.version)!==t)throw Error("Cannot nest MathJaxContexts with different versions. MathJaxContexts should not be nested at all but if they are, they cannot have different versions. Stick with one version of MathJax in your app and avoid using more than one MathJaxContext.");if(2===t&&void 0!==v3Promise||3===t&&void 0!==v2Promise)throw Error("Cannot use MathJax versions 2 and 3 simultaneously in the same app due to how MathJax is set up in the browser; either you have multiple MathJaxContexts with different versions or you have mounted and unmounted MathJaxContexts with different versions. Please stick with one version of MathJax in your app. File an issue in the project Github page if you need this feature.");var u=useRef(c),c=useRef((null==c?void 0:c.version)||null);if(null===c.current)c.current=t;else if(c.current!==t)throw Error("Cannot change version of MathJax in a MathJaxContext after component has mounted. Either reload the page with a new setting when this should happen or use multiple, non-nested, MathJaxContexts in your app.");var v=o||(2===t?DEFAULT_V2_SRC:DEFAULT_V3_SRC);function l(t,o){n&&(window.MathJax=n);var e=document.createElement("script");e.type="text/javascript",e.src=v,e.async=!1,e.addEventListener("load",function(){var e=window.MathJax;a&&a(e),t(e),r&&r()}),e.addEventListener("error",function(e){return o(e)}),document.getElementsByTagName("head")[0].appendChild(e)}return void 0===u.current&&(c={typesettingOptions:s,renderMode:h,hideUntilTypeset:d},2===t?void 0===v2Promise&&("undefined"!=typeof window?(v2Promise=new Promise(l)).catch(function(e){if(!i)throw Error("Failed to download MathJax version 2 from '".concat(v,"' due to: ").concat(e));i(e)}):(v2Promise=Promise.reject()).catch(function(e){})):void 0===v3Promise&&("undefined"!=typeof window?(v3Promise=new Promise(l)).catch(function(e){if(!i)throw Error("Failed to download MathJax version 3 from '".concat(v,"' due to: ").concat(e));i(e)}):(v3Promise=Promise.reject()).catch(function(e){})),u.current=__assign(__assign({},c),2===t?{version:2,promise:v2Promise}:{version:3,promise:v3Promise})),React.createElement(MathJaxBaseContext.Provider,{value:u.current},e)};export default MathJaxContext;export{MathJaxBaseContext};
@@ -0,0 +1 @@
1
+ export{default,default as MathJaxContext,MathJaxBaseContext}from"./MathJaxContext";
package/esm/index.js ADDED
@@ -0,0 +1 @@
1
+ export{default as MathJax}from"./MathJax";export{default as MathJaxContext,MathJaxBaseContext}from"./MathJaxContext";
package/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { default as MathJax, MathJaxProps } from "./MathJax";
2
- export { default as MathJaxContext, MathJaxContextProps, MathJaxBaseContext, MathJaxSubscriberProps, MathJax3Object, MathJax3Config, OptionList } from "./MathJaxContext";
2
+ export { default as MathJaxContext, MathJaxContextProps, MathJaxBaseContext, MathJaxSubscriberProps } from "./MathJaxContext";
3
3
  export { MathJax2Config, MathJax2Object } from "./MathJax2";
4
+ export { MathJax3Object, MathJax3Config, OptionList } from "./MathJax3";
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var __importDefault=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.MathJaxBaseContext=exports.MathJaxContext=exports.MathJax=void 0;var MathJax_1=require("./MathJax");Object.defineProperty(exports,"MathJax",{enumerable:!0,get:function(){return __importDefault(MathJax_1).default}});var MathJaxContext_1=require("./MathJaxContext");Object.defineProperty(exports,"MathJaxContext",{enumerable:!0,get:function(){return __importDefault(MathJaxContext_1).default}}),Object.defineProperty(exports,"MathJaxBaseContext",{enumerable:!0,get:function(){return MathJaxContext_1.MathJaxBaseContext}});
1
+ "use strict";var __importDefault=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}},MathJax_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.MathJaxBaseContext=exports.MathJaxContext=exports.MathJax=void 0,require("./MathJax")),MathJaxContext_1=(Object.defineProperty(exports,"MathJax",{enumerable:!0,get:function(){return __importDefault(MathJax_1).default}}),require("./MathJaxContext"));Object.defineProperty(exports,"MathJaxContext",{enumerable:!0,get:function(){return __importDefault(MathJaxContext_1).default}}),Object.defineProperty(exports,"MathJaxBaseContext",{enumerable:!0,get:function(){return MathJaxContext_1.MathJaxBaseContext}});
package/package.json CHANGED
@@ -1,9 +1,11 @@
1
1
  {
2
2
  "name": "better-react-mathjax",
3
- "version": "1.0.3",
3
+ "version": "2.0.0",
4
4
  "description": "Up-to-date component for using MathJax in latest React (using functional components and hooks API). Focuses on being versatile and making the use of MathJax in React a pleasant experience without flashes of non-typeset content, both with respect to initial rendering as well as dynamic updates. Simple to use but with many configuration options.",
5
5
  "main": "index.js",
6
+ "module": "esm/index.js",
6
7
  "types": "index.d.ts",
8
+ "sideEffects": false,
7
9
  "repository": {
8
10
  "type": "git",
9
11
  "url": "git+https://github.com/fast-reflexes/better-react-mathjax.git"
package/MathJax.js DELETED
@@ -1 +0,0 @@
1
- "use strict";var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e}).apply(this,arguments)},__createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r),Object.defineProperty(e,n,{enumerable:!0,get:function(){return t[r]}})}:function(e,t,r,n){e[n=void 0===n?r:n]=t[r]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&__createBinding(t,e,r);return __setModuleDefault(t,e),t},__rest=this&&this.__rest||function(e,t){var r={};for(i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(r[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols)for(var n=0,i=Object.getOwnPropertySymbols(e);n<i.length;n++)t.indexOf(i[n])<0&&Object.prototype.propertyIsEnumerable.call(e,i[n])&&(r[i[n]]=e[i[n]]);return r};Object.defineProperty(exports,"__esModule",{value:!0});var react_1=__importStar(require("react")),MathJaxContext_1=require("./MathJaxContext"),typesettingFailed=function(e){return"Typesetting failed: "+(void 0!==e.message?e.message:e.toString())},MathJax=function(e){function n(){"every"===h&&u&&"post"===y&&null!==d.current&&(d.current.style.visibility="visible"),v.current||("first"===h&&null!==d.current&&(d.current.style.visibility="visible"),o&&o(),v.current=!0),s&&s(),g.current=!1}var t=e.inline,i=void 0!==t&&t,r=e.hideUntilTypeset,o=e.onInitTypeset,s=e.onTypeset,a=e.text,u=e.dynamic,c=e.typesettingOptions,l=e.renderMode,t=e.children,e=__rest(e,["inline","hideUntilTypeset","onInitTypeset","onTypeset","text","dynamic","typesettingOptions","renderMode","children"]),p=react_1.useRef(""),d=react_1.useRef(null),f=react_1.useContext(MathJaxContext_1.MathJaxBaseContext),h=void 0===r?null==f?void 0:f.hideUntilTypeset:r,y=void 0===l?null==f?void 0:f.renderMode:l,_=void 0===c?null==f?void 0:f.typesettingOptions:c,v=react_1.useRef(!1),g=react_1.useRef(!1);return!g.current&&null!==d.current&&u&&"every"===h&&"post"===y&&(d.current.style.visibility="hidden"),("undefined"!=typeof window?react_1.useLayoutEffect:react_1.useEffect)(function(){if((u||!v.current)&&null!==d.current){if(!f)throw Error("MathJax was not loaded, did you use the MathJax component outside of a MathJaxContext?");if("pre"===y){if(!("string"==typeof(e=a)&&0<e.length))throw Error("Render mode 'pre' requires text prop to be set and non-empty, which was currently \""+a+'"');if(!c||!c.fn)throw Error("Render mode 'pre' requires 'typesettingOptions' prop with 'fn' property to be set on MathJax element or in the MathJaxContext");if(2===f.version)throw Error("Render mode 'pre' only available with MathJax 3, and version 2 is currently in use")}"post"!==y&&a===p.current||g.current||(g.current=!0,3===f.version?f.promise.then(function(t){var r;"pre"===y?(r=function(e){p.current=a,t.startup.document.clear(),t.startup.document.updateDocument(),null!==d.current&&(d.current.innerHTML=e.outerHTML),n()},c.fn.endsWith("Promise")?t.startup.promise.then(function(){return t[_.fn](a,__assign(__assign({},(null==_?void 0:_.options)||{}),{display:!i}))}).then(r).catch(function(e){throw n(),Error(typesettingFailed(e))}):t.startup.promise.then(function(){var e=t[_.fn](a,__assign(__assign({},(null==_?void 0:_.options)||{}),{display:!i}));r(e)}).catch(function(e){throw n(),Error(typesettingFailed(e))})):t.startup.promise.then(function(){return t.typesetClear([d.current]),t.typesetPromise([d.current])}).then(n).catch(function(e){throw n(),Error(typesettingFailed(e))})}).catch(function(e){throw n(),Error(typesettingFailed(e))}):f.promise.then(function(e){e.Hub.Queue(["Typeset",e.Hub,d.current]),e.Hub.Queue(n)}).catch(function(e){throw n(),Error(typesettingFailed(e))}))}var e}),react_1.default.createElement("span",__assign({},e,{style:__assign(__assign({display:i?"inline":"block"},e.style),{visibility:h?"hidden":void 0}),ref:d}),t)};exports.default=MathJax;
package/MathJax2.js DELETED
@@ -1 +0,0 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});
package/MathJaxContext.js DELETED
@@ -1 +0,0 @@
1
- "use strict";var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)},__createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r),Object.defineProperty(e,n,{enumerable:!0,get:function(){return t[r]}})}:function(e,t,r,n){e[n=void 0===n?r:n]=t[r]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&__createBinding(t,e,r);return __setModuleDefault(t,e),t};Object.defineProperty(exports,"__esModule",{value:!0}),exports.MathJaxBaseContext=void 0;var react_1=__importStar(require("react"));exports.MathJaxBaseContext=react_1.createContext(void 0);var v2Promise,v3Promise,DEFAULT_V2_SRC="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/MathJax.js?config=TeX-MML-AM_CHTML",DEFAULT_V3_SRC="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.0/es5/tex-mml-chtml.js",MathJaxContext=function(e){var n=e.config,t=e.version,o=void 0===t?3:t,r=e.src,i=void 0===r?2===o?DEFAULT_V2_SRC:DEFAULT_V3_SRC:r,a=e.onStartup,s=e.onLoad,d=e.onError,u=e.typesettingOptions,c=e.renderMode,l=void 0===c?"post":c,t=e.hideUntilTypeset,r=e.children,c=react_1.useContext(exports.MathJaxBaseContext);if(void 0!==(null==c?void 0:c.version)&&(null==c?void 0:c.version)!==o)throw Error("Cannot nest MathJaxContexts with different versions. MathJaxContexts should not be nested at all but if they are, they inherit several properties. If you need different versions, then use multiple, non-nested, MathJaxContexts in your app.");e=react_1.useRef(c),c=react_1.useRef((null==c?void 0:c.version)||null);if(null===c.current)c.current=o;else if(c.current!==o)throw Error("Cannot change version of MathJax in a MathJaxContext after component has mounted. Either reload the page with a new setting when this should happen or use multiple, non-nested, MathJaxContexts in your app.");var h=i||(2===o?DEFAULT_V2_SRC:DEFAULT_V3_SRC);function f(t,r){n&&(window.MathJax=n);var e=document.createElement("script");e.type="text/javascript",e.src=i||(2===o?DEFAULT_V2_SRC:DEFAULT_V3_SRC),e.async=!1,e.addEventListener("load",function(){var e=window.MathJax;a&&a(e),t(e),s&&s()}),e.addEventListener("error",function(e){return r(e)}),document.getElementsByTagName("head")[0].appendChild(e)}return void 0===e.current&&(t={typesettingOptions:u,renderMode:l,hideUntilTypeset:t},2===o?void 0===v2Promise&&("undefined"!=typeof window?(v2Promise=new Promise(f)).catch(function(e){if(!d)throw Error("Failed to download MathJax version 2 from '"+h+"' due to: "+e);d(e)}):(v2Promise=Promise.reject()).catch(function(e){})):void 0===v3Promise&&("undefined"!=typeof window?(v3Promise=new Promise(f)).catch(function(e){if(!d)throw Error("Failed to download MathJax version 3 from '"+h+"' due to: "+e);d(e)}):(v3Promise=Promise.reject()).catch(function(e){})),e.current=__assign(__assign({},t),2===o?{version:2,promise:v2Promise}:{version:3,promise:v3Promise})),react_1.default.createElement(exports.MathJaxBaseContext.Provider,{value:e.current},r)};exports.default=MathJaxContext;