@pie-lib/math-rendering 3.2.2-next.4 → 3.3.1-beta.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/CHANGELOG.md +22 -42
- package/NEXT.CHANGELOG.json +1 -0
- package/package.json +5 -3
- package/src/__tests__/mml-to-latex.test.js +14 -0
- package/src/__tests__/normalization.test.js +51 -0
- package/src/__tests__/render-math.test.js +155 -0
- package/src/mml-to-latex.js +2 -2
- package/src/mstack/__tests__/__snapshots__/chtml.test.js.snap +9 -0
- package/src/mstack/__tests__/chtml.test.js +104 -0
- package/src/render-math.js +127 -71
- package/lib/index.js +0 -38
- package/lib/index.js.map +0 -1
- package/lib/mml-to-latex.js +0 -17
- package/lib/mml-to-latex.js.map +0 -1
- package/lib/mstack/chtml.js +0 -296
- package/lib/mstack/chtml.js.map +0 -1
- package/lib/mstack/index.js +0 -23
- package/lib/mstack/index.js.map +0 -1
- package/lib/mstack/mml.js +0 -109
- package/lib/mstack/mml.js.map +0 -1
- package/lib/normalization.js +0 -95
- package/lib/normalization.js.map +0 -1
- package/lib/render-math.js +0 -358
- package/lib/render-math.js.map +0 -1
- package/playground/demo.html +0 -958
- package/playground/demo.js +0 -108
- package/playground/main.html +0 -158
- package/playground/main.js +0 -16
- package/playground/webpack.config.js +0 -29
package/src/render-math.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { mathjax } from 'mathjax-full/js/mathjax';
|
|
2
|
+
import { MathJax as globalMathjax } from 'mathjax-full/js/components/global';
|
|
3
|
+
import { AssistiveMmlHandler } from 'mathjax-full/js/a11y/assistive-mml';
|
|
4
|
+
import { EnrichHandler } from 'mathjax-full/js/a11y/semantic-enrich';
|
|
5
|
+
import { MenuHandler } from 'mathjax-full/js/ui/menu/MenuHandler';
|
|
6
|
+
import { FindMathML } from 'mathjax-full/js/input/mathml/FindMathML';
|
|
2
7
|
import { MathML } from 'mathjax-full/js/input/mathml';
|
|
3
8
|
import { TeX } from 'mathjax-full/js/input/tex';
|
|
4
9
|
|
|
@@ -6,12 +11,19 @@ import { CHTML } from 'mathjax-full/js/output/chtml';
|
|
|
6
11
|
import { RegisterHTMLHandler } from 'mathjax-full/js/handlers/html';
|
|
7
12
|
import { browserAdaptor } from 'mathjax-full/js/adaptors/browserAdaptor';
|
|
8
13
|
import { AllPackages } from 'mathjax-full/js/input/tex/AllPackages';
|
|
14
|
+
import { engineReady } from 'speech-rule-engine/js/common/system';
|
|
9
15
|
|
|
10
16
|
if (typeof window !== 'undefined') {
|
|
11
17
|
RegisterHTMLHandler(browserAdaptor());
|
|
12
18
|
}
|
|
13
19
|
|
|
14
|
-
|
|
20
|
+
let sreReady = false;
|
|
21
|
+
|
|
22
|
+
engineReady().then(() => {
|
|
23
|
+
sreReady = true;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// import pkg from '../../package.json';
|
|
15
27
|
import { mmlNodes, chtmlNodes } from './mstack';
|
|
16
28
|
import debug from 'debug';
|
|
17
29
|
import { wrapMath, unWrapMath } from './normalization';
|
|
@@ -19,6 +31,7 @@ import { MmlFactory } from 'mathjax-full/js/core/MmlTree/MmlFactory';
|
|
|
19
31
|
import { SerializedMmlVisitor } from 'mathjax-full/js/core/MmlTree/SerializedMmlVisitor';
|
|
20
32
|
import { CHTMLWrapperFactory } from 'mathjax-full/js/output/chtml/WrapperFactory';
|
|
21
33
|
import { CHTMLmspace } from 'mathjax-full/js/output/chtml/Wrappers/mspace';
|
|
34
|
+
import { HTMLDomStrings } from 'mathjax-full/js/handlers/html/HTMLDomStrings';
|
|
22
35
|
|
|
23
36
|
const visitor = new SerializedMmlVisitor();
|
|
24
37
|
const toMMl = (node) => visitor.visitTree(node);
|
|
@@ -29,7 +42,15 @@ const NEWLINE_BLOCK_REGEX = /\\embed\{newLine\}\[\]/g;
|
|
|
29
42
|
const NEWLINE_LATEX = '\\newline ';
|
|
30
43
|
|
|
31
44
|
const getGlobal = () => {
|
|
32
|
-
|
|
45
|
+
// TODO does it make sense to use version?
|
|
46
|
+
// const key = `${pkg.name}@${pkg.version.split('.')[0]}`;
|
|
47
|
+
// It looks like Ed made this change when he switched from mathjax3 to mathjax-full
|
|
48
|
+
// I think it was supposed to make sure version 1 (using mathjax3) is not used
|
|
49
|
+
// in combination with version 2 (using mathjax-full)
|
|
50
|
+
|
|
51
|
+
// TODO higher level wrappers use this instance of math-rendering, and if 2 different instances are used, math rendering is not working
|
|
52
|
+
// so I will hardcode this for now until a better solution is found
|
|
53
|
+
const key = '@pie-lib/math-rendering@2';
|
|
33
54
|
|
|
34
55
|
if (typeof window !== 'undefined') {
|
|
35
56
|
if (!window[key]) {
|
|
@@ -81,6 +102,18 @@ const adjustMathMLStyle = (el = document) => {
|
|
|
81
102
|
nodes.forEach((node) => node.setAttribute('displaystyle', 'true'));
|
|
82
103
|
};
|
|
83
104
|
|
|
105
|
+
class myFindMathML extends FindMathML {
|
|
106
|
+
processMath(set) {
|
|
107
|
+
const adaptor = this.adaptor;
|
|
108
|
+
for (const mml of Array.from(set)) {
|
|
109
|
+
if (adaptor.kind(adaptor.parent(mml)) === 'mjx-assistive-mml') {
|
|
110
|
+
set.delete(mml);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return super.processMath(set);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
84
117
|
const createMathMLInstance = (opts, docProvided = document) => {
|
|
85
118
|
opts = opts || defaultOpts();
|
|
86
119
|
|
|
@@ -111,56 +144,33 @@ const createMathMLInstance = (opts, docProvided = document) => {
|
|
|
111
144
|
['\\(', '\\)'],
|
|
112
145
|
],
|
|
113
146
|
processEscapes: true,
|
|
114
|
-
options: {
|
|
115
|
-
enableExplorer: true,
|
|
116
|
-
enableAssistiveMml: true,
|
|
117
|
-
a11y: {
|
|
118
|
-
speech: true,
|
|
119
|
-
braille: true,
|
|
120
|
-
subtitles: true,
|
|
121
|
-
},
|
|
122
|
-
sre: {
|
|
123
|
-
domain: 'default',
|
|
124
|
-
style: 'default',
|
|
125
|
-
locale: 'en',
|
|
126
|
-
},
|
|
127
|
-
},
|
|
128
147
|
}
|
|
129
148
|
: {
|
|
130
149
|
packages,
|
|
131
150
|
macros,
|
|
132
|
-
options: {
|
|
133
|
-
enableExplorer: true,
|
|
134
|
-
enableAssistiveMml: true,
|
|
135
|
-
a11y: {
|
|
136
|
-
speech: true,
|
|
137
|
-
braille: true,
|
|
138
|
-
subtitles: true,
|
|
139
|
-
},
|
|
140
|
-
sre: {
|
|
141
|
-
domain: 'default',
|
|
142
|
-
style: 'default',
|
|
143
|
-
locale: 'en',
|
|
144
|
-
},
|
|
145
|
-
},
|
|
146
151
|
};
|
|
147
152
|
|
|
148
153
|
const mmlConfig = {
|
|
149
|
-
options: {
|
|
150
|
-
a11y: {
|
|
151
|
-
speech: true,
|
|
152
|
-
braille: true,
|
|
153
|
-
subtitles: true,
|
|
154
|
-
},
|
|
155
|
-
},
|
|
156
154
|
parseError: function(node) {
|
|
157
155
|
// function to process parsing errors
|
|
158
156
|
// eslint-disable-next-line no-console
|
|
159
157
|
console.log('error:', node);
|
|
160
158
|
this.error(this.adaptor.textContent(node).replace(/\n.*/g, ''));
|
|
161
159
|
},
|
|
160
|
+
FindMathML: new myFindMathML(),
|
|
162
161
|
};
|
|
163
162
|
|
|
163
|
+
let cachedMathjax;
|
|
164
|
+
|
|
165
|
+
if (globalMathjax && globalMathjax.version !== mathjax.version) {
|
|
166
|
+
// handling other MathJax version on the page
|
|
167
|
+
// replacing it temporarily with the version we have
|
|
168
|
+
window.MathJax._ = window.MathJax._ || {};
|
|
169
|
+
window.MathJax.config = window.MathJax.config || {};
|
|
170
|
+
cachedMathjax = window.MathJax;
|
|
171
|
+
Object.assign(globalMathjax, mathjax);
|
|
172
|
+
}
|
|
173
|
+
|
|
164
174
|
const fontURL = `https://unpkg.com/mathjax-full@${mathjax.version}/ts/output/chtml/fonts/tex-woff-v2`;
|
|
165
175
|
const htmlConfig = {
|
|
166
176
|
fontURL,
|
|
@@ -169,12 +179,6 @@ const createMathMLInstance = (opts, docProvided = document) => {
|
|
|
169
179
|
...CHTMLWrapperFactory.defaultNodes,
|
|
170
180
|
...chtmlNodes,
|
|
171
181
|
}),
|
|
172
|
-
|
|
173
|
-
options: {
|
|
174
|
-
renderActions: {
|
|
175
|
-
assistiveMml: [['AssistiveMmlHandler']],
|
|
176
|
-
},
|
|
177
|
-
},
|
|
178
182
|
};
|
|
179
183
|
|
|
180
184
|
const mml = new MathML(mmlConfig);
|
|
@@ -183,8 +187,12 @@ const createMathMLInstance = (opts, docProvided = document) => {
|
|
|
183
187
|
...MmlFactory.defaultNodes,
|
|
184
188
|
...mmlNodes,
|
|
185
189
|
});
|
|
190
|
+
const classFactory = EnrichHandler(
|
|
191
|
+
MenuHandler(AssistiveMmlHandler(mathjax.handlers.handlesDocument(docProvided))),
|
|
192
|
+
mml,
|
|
193
|
+
);
|
|
186
194
|
|
|
187
|
-
const html =
|
|
195
|
+
const html = classFactory.create(docProvided, {
|
|
188
196
|
compileError: (mj, math, err) => {
|
|
189
197
|
// eslint-disable-next-line no-console
|
|
190
198
|
console.log('bad math?:', math);
|
|
@@ -199,27 +207,42 @@ const createMathMLInstance = (opts, docProvided = document) => {
|
|
|
199
207
|
doc.typesetError(math, err);
|
|
200
208
|
},
|
|
201
209
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
menuOptions: {
|
|
205
|
-
settings: {
|
|
206
|
-
assistiveMml: true,
|
|
207
|
-
collapsible: true,
|
|
208
|
-
explorer: true,
|
|
209
|
-
},
|
|
210
|
-
},
|
|
210
|
+
sre: {
|
|
211
|
+
speech: 'deep',
|
|
211
212
|
},
|
|
213
|
+
enrichSpeech: 'deep',
|
|
212
214
|
|
|
213
215
|
InputJax: [new TeX(texConfig), mml],
|
|
214
216
|
OutputJax: new CHTML(htmlConfig),
|
|
217
|
+
DomStrings: new HTMLDomStrings({
|
|
218
|
+
skipHtmlTags: [
|
|
219
|
+
'script',
|
|
220
|
+
'noscript',
|
|
221
|
+
'style',
|
|
222
|
+
'textarea',
|
|
223
|
+
'pre',
|
|
224
|
+
'code',
|
|
225
|
+
'annotation',
|
|
226
|
+
'annotation-xml',
|
|
227
|
+
'mjx-assistive-mml',
|
|
228
|
+
'mjx-container',
|
|
229
|
+
],
|
|
230
|
+
}),
|
|
215
231
|
});
|
|
216
232
|
|
|
217
233
|
// Note: we must set this *after* mathjax.document (no idea why)
|
|
218
234
|
mml.setMmlFactory(customMmlFactory);
|
|
219
235
|
|
|
236
|
+
if (cachedMathjax) {
|
|
237
|
+
// if we have a cached version, we replace it here
|
|
238
|
+
window.MathJax = cachedMathjax;
|
|
239
|
+
}
|
|
240
|
+
|
|
220
241
|
return html;
|
|
221
242
|
};
|
|
222
243
|
|
|
244
|
+
let enrichSpeechInitialized = false;
|
|
245
|
+
|
|
223
246
|
const bootstrap = (opts) => {
|
|
224
247
|
if (typeof window === 'undefined') {
|
|
225
248
|
return { Typeset: () => ({}) };
|
|
@@ -231,33 +254,66 @@ const bootstrap = (opts) => {
|
|
|
231
254
|
version: mathjax.version,
|
|
232
255
|
html: html,
|
|
233
256
|
Typeset: function(...elements) {
|
|
234
|
-
const
|
|
235
|
-
.findMath(elements.length ? { elements } : {})
|
|
236
|
-
.compile()
|
|
237
|
-
.getMetrics()
|
|
238
|
-
.typeset()
|
|
239
|
-
.updateDocument();
|
|
257
|
+
const attemptRender = (temporary = false) => {
|
|
258
|
+
let updatedDocument = this.html.findMath(elements.length ? { elements } : {}).compile();
|
|
240
259
|
|
|
241
|
-
|
|
242
|
-
|
|
260
|
+
if (!temporary && sreReady) {
|
|
261
|
+
updatedDocument = updatedDocument.enrich();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
updatedDocument = updatedDocument
|
|
265
|
+
.getMetrics()
|
|
266
|
+
.typeset()
|
|
267
|
+
.assistiveMml()
|
|
268
|
+
.attachSpeech()
|
|
269
|
+
.addMenu()
|
|
270
|
+
.updateDocument();
|
|
243
271
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
272
|
+
if (!enrichSpeechInitialized && typeof updatedDocument.math.list?.next?.data === 'object') {
|
|
273
|
+
enrichSpeechInitialized = true;
|
|
274
|
+
}
|
|
247
275
|
|
|
248
|
-
|
|
276
|
+
try {
|
|
277
|
+
const list = updatedDocument.math.list;
|
|
278
|
+
|
|
279
|
+
if (list) {
|
|
280
|
+
for (let item = list.next; typeof item.data !== 'symbol'; item = item.next) {
|
|
281
|
+
const mathMl = toMMl(item.data.root);
|
|
282
|
+
const parsedMathMl = mathMl.replaceAll('\n', '');
|
|
283
|
+
|
|
284
|
+
item.data.typesetRoot.setAttribute('data-mathml', parsedMathMl);
|
|
285
|
+
item.data.typesetRoot.setAttribute('tabindex', '-1');
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
} catch (e) {
|
|
289
|
+
// eslint-disable-next-line no-console
|
|
290
|
+
console.error(e.toString());
|
|
249
291
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
292
|
+
|
|
293
|
+
updatedDocument.clear();
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
if (!enrichSpeechInitialized) {
|
|
297
|
+
attemptRender(true);
|
|
253
298
|
}
|
|
254
299
|
|
|
255
|
-
|
|
300
|
+
mathjax.handleRetriesFor(() => {
|
|
301
|
+
attemptRender();
|
|
302
|
+
});
|
|
256
303
|
},
|
|
257
304
|
};
|
|
258
305
|
};
|
|
259
306
|
|
|
260
307
|
const renderMath = (el, renderOpts) => {
|
|
308
|
+
if (
|
|
309
|
+
window &&
|
|
310
|
+
window.MathJax &&
|
|
311
|
+
window.MathJax.customKey &&
|
|
312
|
+
window.MathJax.customKey == '@pie-lib/math-rendering-accessible@1'
|
|
313
|
+
) {
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
|
|
261
317
|
const isString = typeof el === 'string';
|
|
262
318
|
let executeOn = document.body;
|
|
263
319
|
|
|
@@ -303,9 +359,9 @@ const renderMath = (el, renderOpts) => {
|
|
|
303
359
|
return;
|
|
304
360
|
}
|
|
305
361
|
|
|
306
|
-
if (el instanceof Element) {
|
|
362
|
+
if (el instanceof Element && getGlobal().instance?.Typeset) {
|
|
307
363
|
getGlobal().instance.Typeset(el);
|
|
308
|
-
} else if (el.length) {
|
|
364
|
+
} else if (el.length && getGlobal().instance?.Typeset) {
|
|
309
365
|
const arr = Array.from(el);
|
|
310
366
|
getGlobal().instance.Typeset(...arr);
|
|
311
367
|
}
|
package/lib/index.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
Object.defineProperty(exports, "mmlToLatex", {
|
|
9
|
-
enumerable: true,
|
|
10
|
-
get: function get() {
|
|
11
|
-
return _mmlToLatex["default"];
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
Object.defineProperty(exports, "renderMath", {
|
|
15
|
-
enumerable: true,
|
|
16
|
-
get: function get() {
|
|
17
|
-
return _renderMath["default"];
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
Object.defineProperty(exports, "unWrapMath", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
get: function get() {
|
|
23
|
-
return _normalization.unWrapMath;
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
Object.defineProperty(exports, "wrapMath", {
|
|
27
|
-
enumerable: true,
|
|
28
|
-
get: function get() {
|
|
29
|
-
return _normalization.wrapMath;
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
var _renderMath = _interopRequireDefault(require("./render-math"));
|
|
34
|
-
|
|
35
|
-
var _mmlToLatex = _interopRequireDefault(require("./mml-to-latex"));
|
|
36
|
-
|
|
37
|
-
var _normalization = require("./normalization");
|
|
38
|
-
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA","sourcesContent":["import renderMath from './render-math';\nimport mmlToLatex from './mml-to-latex';\nimport { wrapMath, unWrapMath } from './normalization';\n\nexport { renderMath, wrapMath, unWrapMath, mmlToLatex };\n"],"file":"index.js"}
|
package/lib/mml-to-latex.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports["default"] = void 0;
|
|
9
|
-
|
|
10
|
-
var _mathmlToLatex = _interopRequireDefault(require("mathml-to-latex"));
|
|
11
|
-
|
|
12
|
-
var _default = function _default(mathml) {
|
|
13
|
-
return _mathmlToLatex["default"].convert(mathml);
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
exports["default"] = _default;
|
|
17
|
-
//# sourceMappingURL=mml-to-latex.js.map
|
package/lib/mml-to-latex.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/mml-to-latex.js"],"names":["mathml","Mathml2latex","convert"],"mappings":";;;;;;;;;AAAA;;eACe,kBAACA,MAAD;AAAA,SAAYC,0BAAaC,OAAb,CAAqBF,MAArB,CAAZ;AAAA,C","sourcesContent":["import Mathml2latex from 'mathml-to-latex';\nexport default (mathml) => Mathml2latex.convert(mathml);\n"],"file":"mml-to-latex.js"}
|
package/lib/mstack/chtml.js
DELETED
|
@@ -1,296 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports.getStackData = exports.Row = exports.Line = exports.CHTMLmstack = void 0;
|
|
9
|
-
|
|
10
|
-
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
|
11
|
-
|
|
12
|
-
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
|
|
13
|
-
|
|
14
|
-
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
|
|
15
|
-
|
|
16
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
17
|
-
|
|
18
|
-
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
19
|
-
|
|
20
|
-
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
21
|
-
|
|
22
|
-
var _Wrapper = require("mathjax-full/js/output/chtml/Wrapper");
|
|
23
|
-
|
|
24
|
-
var _lodash = _interopRequireDefault(require("lodash"));
|
|
25
|
-
|
|
26
|
-
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
|
|
27
|
-
|
|
28
|
-
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
29
|
-
|
|
30
|
-
var reduceText = function reduceText(acc, n) {
|
|
31
|
-
if (n.node && n.node.kind === 'text') {
|
|
32
|
-
acc += n.node.text;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return acc;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
var Line = /*#__PURE__*/function () {
|
|
39
|
-
function Line() {
|
|
40
|
-
(0, _classCallCheck2["default"])(this, Line);
|
|
41
|
-
this.kind = 'line';
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
(0, _createClass2["default"])(Line, [{
|
|
45
|
-
key: "columns",
|
|
46
|
-
get: function get() {
|
|
47
|
-
return [];
|
|
48
|
-
}
|
|
49
|
-
}]);
|
|
50
|
-
return Line;
|
|
51
|
-
}();
|
|
52
|
-
|
|
53
|
-
exports.Line = Line;
|
|
54
|
-
|
|
55
|
-
var Row = /*#__PURE__*/function () {
|
|
56
|
-
function Row(columns, operator) {
|
|
57
|
-
(0, _classCallCheck2["default"])(this, Row);
|
|
58
|
-
this.kind = 'row';
|
|
59
|
-
this.operator = operator;
|
|
60
|
-
this.columns = columns;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
(0, _createClass2["default"])(Row, [{
|
|
64
|
-
key: "pad",
|
|
65
|
-
value: function pad(count) {
|
|
66
|
-
var direction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'right';
|
|
67
|
-
|
|
68
|
-
if (count < this.columns.length) {
|
|
69
|
-
throw new Error('no');
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
var diff = count - this.columns.length;
|
|
73
|
-
|
|
74
|
-
var padding = _lodash["default"].times(diff).map(function () {
|
|
75
|
-
return '__pad__';
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
return direction === 'right' ? [].concat((0, _toConsumableArray2["default"])(padding), (0, _toConsumableArray2["default"])(this.columns)) : [].concat((0, _toConsumableArray2["default"])(this.columns), (0, _toConsumableArray2["default"])(padding));
|
|
79
|
-
}
|
|
80
|
-
}]);
|
|
81
|
-
return Row;
|
|
82
|
-
}();
|
|
83
|
-
|
|
84
|
-
exports.Row = Row;
|
|
85
|
-
|
|
86
|
-
var mathNodeToCharArray = function mathNodeToCharArray(mn) {
|
|
87
|
-
var text = mn.childNodes.reduce(reduceText, '');
|
|
88
|
-
return text.split('');
|
|
89
|
-
};
|
|
90
|
-
/**
|
|
91
|
-
* Convert child a column entry
|
|
92
|
-
* @param {*} child
|
|
93
|
-
* @return an array of column content
|
|
94
|
-
*/
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
var toColumnArray = function toColumnArray(child) {
|
|
98
|
-
if (!child || !child.kind) {
|
|
99
|
-
return [];
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (child.kind === 'msrow') {
|
|
103
|
-
throw new Error('msrow in msrow?');
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (child.kind === 'mo') {
|
|
107
|
-
// We are going to treat this operator as a text array.
|
|
108
|
-
// It's probably going to be a decimal point
|
|
109
|
-
// eslint-disable-next-line no-console
|
|
110
|
-
console.warn('mo that is not 1st node in msrow?');
|
|
111
|
-
return mathNodeToCharArray(child); // throw new Error('mo must be first child of msrow');
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (child.kind === 'mn') {
|
|
115
|
-
return mathNodeToCharArray(child);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (child.toCHTML) {
|
|
119
|
-
return child;
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
/**
|
|
123
|
-
* convert mstack chtml childNodes into a Row
|
|
124
|
-
* @param child chtml child node of mstack
|
|
125
|
-
* @return Row | Line
|
|
126
|
-
*/
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
var rowStack = function rowStack(child) {
|
|
130
|
-
if (!child || !child.kind) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
if (child.kind === 'msrow') {
|
|
135
|
-
if (!child.childNodes || child.childNodes.length === 0) {
|
|
136
|
-
return new Row([]);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
var f = _lodash["default"].first(child.childNodes);
|
|
140
|
-
|
|
141
|
-
var nodes = f && f.kind === 'mo' ? _lodash["default"].tail(child.childNodes) : child.childNodes;
|
|
142
|
-
|
|
143
|
-
var columns = _lodash["default"].flatten(nodes.map(toColumnArray));
|
|
144
|
-
|
|
145
|
-
return new Row(columns, f.kind === 'mo' ? f : undefined);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (child.kind === 'mn') {
|
|
149
|
-
var _columns = mathNodeToCharArray(child);
|
|
150
|
-
|
|
151
|
-
return new Row(_columns, undefined);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (child.kind === 'mo') {
|
|
155
|
-
// eslint-disable-next-line no-console
|
|
156
|
-
console.warn('mo on its own row?');
|
|
157
|
-
return new Row([], child);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (child.kind === 'msline') {
|
|
161
|
-
return new Line();
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (child.toCHTML) {
|
|
165
|
-
return new Row([child]);
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
/** convert MathJax chtml tree to Row[]
|
|
169
|
-
* @param mstack the root of the mathjax chtml tree
|
|
170
|
-
* @return Row[]
|
|
171
|
-
*/
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
var getStackData = function getStackData(mstack) {
|
|
175
|
-
if (!mstack || !mstack.childNodes) {
|
|
176
|
-
return [];
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return _lodash["default"].compact(mstack.childNodes.map(rowStack));
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
exports.getStackData = getStackData;
|
|
183
|
-
|
|
184
|
-
var CHTMLmstack = /*#__PURE__*/function (_CHTMLWrapper) {
|
|
185
|
-
(0, _inherits2["default"])(CHTMLmstack, _CHTMLWrapper);
|
|
186
|
-
|
|
187
|
-
var _super = _createSuper(CHTMLmstack);
|
|
188
|
-
|
|
189
|
-
function CHTMLmstack(factory, node) {
|
|
190
|
-
var _this;
|
|
191
|
-
|
|
192
|
-
var parent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
193
|
-
(0, _classCallCheck2["default"])(this, CHTMLmstack);
|
|
194
|
-
_this = _super.call(this, factory, node, parent);
|
|
195
|
-
_this.ce = _this.adaptor.document.createElement.bind(_this.adaptor.document);
|
|
196
|
-
return _this;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
(0, _createClass2["default"])(CHTMLmstack, [{
|
|
200
|
-
key: "toCHTML",
|
|
201
|
-
value: function toCHTML(parent) {
|
|
202
|
-
var _this2 = this;
|
|
203
|
-
|
|
204
|
-
var chtml = this.standardCHTMLnode(parent);
|
|
205
|
-
var stackData = getStackData(this); // console.log('stackData', stackData);
|
|
206
|
-
|
|
207
|
-
var maxCols = stackData.reduce(function (acc, r) {
|
|
208
|
-
if (r && r.columns.length > acc) {
|
|
209
|
-
acc = r.columns.length;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
return acc;
|
|
213
|
-
}, 0);
|
|
214
|
-
var table = this.ce('table');
|
|
215
|
-
chtml.appendChild(table);
|
|
216
|
-
stackData.forEach(function (row) {
|
|
217
|
-
var tr = _this2.ce('tr');
|
|
218
|
-
|
|
219
|
-
table.appendChild(tr);
|
|
220
|
-
|
|
221
|
-
if (row.kind === 'row') {
|
|
222
|
-
var td = _this2.ce('td');
|
|
223
|
-
|
|
224
|
-
tr.appendChild(td);
|
|
225
|
-
|
|
226
|
-
if (row.operator && row.operator.toCHTML) {
|
|
227
|
-
td.setAttribute('class', 'inner');
|
|
228
|
-
row.operator.toCHTML(td);
|
|
229
|
-
} else {
|
|
230
|
-
td.textContent = '';
|
|
231
|
-
} // align right for now:
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
var cols = row.pad(maxCols, 'right');
|
|
235
|
-
cols.forEach(function (c) {
|
|
236
|
-
var t = _this2.ce('td');
|
|
237
|
-
|
|
238
|
-
tr.appendChild(t);
|
|
239
|
-
|
|
240
|
-
if (c === '__pad__') {
|
|
241
|
-
t.textContent = '';
|
|
242
|
-
} else if (typeof c === 'string') {
|
|
243
|
-
t.textContent = c;
|
|
244
|
-
} else if (c.kind === 'none') {
|
|
245
|
-
t.textContent = '';
|
|
246
|
-
} else if (c.toCHTML) {
|
|
247
|
-
t.setAttribute('class', 'inner');
|
|
248
|
-
c.toCHTML(t);
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
} else if (row.kind === 'line') {
|
|
252
|
-
var _td = _this2.ce('td');
|
|
253
|
-
|
|
254
|
-
tr.appendChild(_td);
|
|
255
|
-
|
|
256
|
-
_td.setAttribute('colspan', maxCols + 1);
|
|
257
|
-
|
|
258
|
-
_td.setAttribute('class', 'mjx-line');
|
|
259
|
-
|
|
260
|
-
_td.textContent = '';
|
|
261
|
-
}
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
}]);
|
|
265
|
-
return CHTMLmstack;
|
|
266
|
-
}(_Wrapper.CHTMLWrapper);
|
|
267
|
-
|
|
268
|
-
exports.CHTMLmstack = CHTMLmstack;
|
|
269
|
-
CHTMLmstack.styles = {
|
|
270
|
-
'mjx-mstack > table': {
|
|
271
|
-
'line-height': 'initial',
|
|
272
|
-
border: 'solid 0px red',
|
|
273
|
-
'border-spacing': '0em',
|
|
274
|
-
'border-collapse': 'separate'
|
|
275
|
-
},
|
|
276
|
-
'mjx-mstack > table > tr': {
|
|
277
|
-
'line-height': 'initial'
|
|
278
|
-
},
|
|
279
|
-
'mjx-mstack > table > tr > td': {
|
|
280
|
-
// padding: '1.2rem',
|
|
281
|
-
border: 'solid 0px blue',
|
|
282
|
-
'font-family': 'sans-serif',
|
|
283
|
-
'line-height': 'initial'
|
|
284
|
-
},
|
|
285
|
-
'mjx-mstack > table > tr > td.inner': {
|
|
286
|
-
'font-family': 'inherit'
|
|
287
|
-
},
|
|
288
|
-
'mjx-mstack > table > tr > .mjx-line': {
|
|
289
|
-
padding: 0,
|
|
290
|
-
'border-top': 'solid 1px black'
|
|
291
|
-
},
|
|
292
|
-
'.TEX-A': {
|
|
293
|
-
'font-family': 'MJXZERO, MJXTEX !important'
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
//# sourceMappingURL=chtml.js.map
|