@pie-lib/render-math-bundled 3.18.0-hotfix.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.
@@ -0,0 +1,387 @@
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';
7
+ import { MathML } from 'mathjax-full/js/input/mathml';
8
+ import { TeX } from 'mathjax-full/js/input/tex';
9
+
10
+ import { CHTML } from 'mathjax-full/js/output/chtml';
11
+ import { RegisterHTMLHandler } from 'mathjax-full/js/handlers/html';
12
+ import { browserAdaptor } from 'mathjax-full/js/adaptors/browserAdaptor';
13
+ import { AllPackages } from 'mathjax-full/js/input/tex/AllPackages';
14
+ import { engineReady } from 'speech-rule-engine/js/common/system';
15
+
16
+ if (typeof window !== 'undefined') {
17
+ RegisterHTMLHandler(browserAdaptor());
18
+ }
19
+
20
+ let sreReady = false;
21
+
22
+ engineReady().then(() => {
23
+ sreReady = true;
24
+ });
25
+
26
+ // import pkg from '../../package.json';
27
+ import { mmlNodes, chtmlNodes } from './mstack';
28
+ import debug from 'debug';
29
+ import { wrapMath, unWrapMath } from './normalization';
30
+ import { MmlFactory } from 'mathjax-full/js/core/MmlTree/MmlFactory';
31
+ import { SerializedMmlVisitor } from 'mathjax-full/js/core/MmlTree/SerializedMmlVisitor';
32
+ import { CHTMLWrapperFactory } from 'mathjax-full/js/output/chtml/WrapperFactory';
33
+ import { CHTMLmspace } from 'mathjax-full/js/output/chtml/Wrappers/mspace';
34
+ import { HTMLDomStrings } from 'mathjax-full/js/handlers/html/HTMLDomStrings';
35
+
36
+ const visitor = new SerializedMmlVisitor();
37
+ const toMMl = (node) => visitor.visitTree(node);
38
+
39
+ const log = debug('pie-lib:math-rendering');
40
+
41
+ const NEWLINE_BLOCK_REGEX = /\\embed\{newLine\}\[\]/g;
42
+ const NEWLINE_LATEX = '\\newline ';
43
+
44
+ const getGlobal = () => {
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';
54
+
55
+ if (typeof window !== 'undefined') {
56
+ if (!window[key]) {
57
+ window[key] = {};
58
+ }
59
+ return window[key];
60
+ } else {
61
+ return {};
62
+ }
63
+ };
64
+
65
+ /** Add temporary support for a global singleDollar override
66
+ * <code>
67
+ * // This will enable single dollar rendering
68
+ * window.pie = window.pie || {};
69
+ * window.pie.mathRendering = {useSingleDollar: true };
70
+ * </code>
71
+ */
72
+ const defaultOpts = () => getGlobal().opts || {};
73
+
74
+ export const fixMathElement = (element) => {
75
+ if (element.dataset.mathHandled) {
76
+ return;
77
+ }
78
+
79
+ let property = 'innerText';
80
+
81
+ if (element.textContent) {
82
+ property = 'textContent';
83
+ }
84
+
85
+ if (element[property]) {
86
+ element[property] = wrapMath(unWrapMath(element[property]).unwrapped);
87
+ // because mathquill doesn't understand line breaks, sometimes we end up with custom elements on prompts/rationale/etc.
88
+ // we need to replace the custom embedded elements with valid latex that Mathjax can understand
89
+ element[property] = element[property].replace(NEWLINE_BLOCK_REGEX, NEWLINE_LATEX);
90
+ element.dataset.mathHandled = true;
91
+ }
92
+ };
93
+
94
+ export const fixMathElements = (el = document) => {
95
+ const mathElements = el.querySelectorAll('[data-latex]');
96
+
97
+ mathElements.forEach((item) => fixMathElement(item));
98
+ };
99
+
100
+ const adjustMathMLStyle = (el = document) => {
101
+ const nodes = el.querySelectorAll('math');
102
+ nodes.forEach((node) => node.setAttribute('displaystyle', 'true'));
103
+ };
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
+
117
+ const createMathMLInstance = (opts, docProvided = document) => {
118
+ opts = opts || defaultOpts();
119
+
120
+ if (opts.useSingleDollar) {
121
+ // eslint-disable-next-line
122
+ console.warn('[math-rendering] using $ is not advisable, please use $$..$$ or \\(...\\)');
123
+ }
124
+
125
+ const packages = AllPackages.filter((name) => name !== 'bussproofs'); // Bussproofs needs an output jax
126
+
127
+ // The autoload extension predefines all the macros from the extensions that haven't been loaded already
128
+ // so that they automatically load the needed extension when they are first used
129
+ packages.push('autoload');
130
+
131
+ const macros = {
132
+ parallelogram: '\\lower.2em{\\Huge\\unicode{x25B1}}',
133
+ overarc: '\\overparen',
134
+ napprox: '\\not\\approx',
135
+ longdiv: '\\enclose{longdiv}',
136
+ };
137
+
138
+ const texConfig = opts.useSingleDollar
139
+ ? {
140
+ packages,
141
+ macros,
142
+ inlineMath: [
143
+ ['$', '$'],
144
+ ['\\(', '\\)'],
145
+ ],
146
+ processEscapes: true,
147
+ }
148
+ : {
149
+ packages,
150
+ macros,
151
+ };
152
+
153
+ const mmlConfig = {
154
+ parseError: function(node) {
155
+ // function to process parsing errors
156
+ // eslint-disable-next-line no-console
157
+ console.log('error:', node);
158
+ this.error(this.adaptor.textContent(node).replace(/\n.*/g, ''));
159
+ },
160
+ FindMathML: new myFindMathML(),
161
+ };
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
+
174
+ const fontURL = `https://unpkg.com/mathjax-full@${mathjax.version}/ts/output/chtml/fonts/tex-woff-v2`;
175
+ const htmlConfig = {
176
+ fontURL,
177
+
178
+ wrapperFactory: new CHTMLWrapperFactory({
179
+ ...CHTMLWrapperFactory.defaultNodes,
180
+ ...chtmlNodes,
181
+ }),
182
+ };
183
+
184
+ const mml = new MathML(mmlConfig);
185
+
186
+ const customMmlFactory = new MmlFactory({
187
+ ...MmlFactory.defaultNodes,
188
+ ...mmlNodes,
189
+ });
190
+ const classFactory = EnrichHandler(
191
+ MenuHandler(AssistiveMmlHandler(mathjax.handlers.handlesDocument(docProvided))),
192
+ mml,
193
+ );
194
+
195
+ const html = classFactory.create(docProvided, {
196
+ compileError: (mj, math, err) => {
197
+ // eslint-disable-next-line no-console
198
+ console.log('bad math?:', math);
199
+ // eslint-disable-next-line no-console
200
+ console.error(err);
201
+ },
202
+ typesetError: function(doc, math, err) {
203
+ // eslint-disable-next-line no-console
204
+ console.log('typeset error');
205
+ // eslint-disable-next-line no-console
206
+ console.error(err);
207
+ doc.typesetError(math, err);
208
+ },
209
+
210
+ sre: {
211
+ speech: 'deep',
212
+ },
213
+ enrichSpeech: 'deep',
214
+
215
+ InputJax: [new TeX(texConfig), mml],
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
+ }),
231
+ });
232
+
233
+ // Note: we must set this *after* mathjax.document (no idea why)
234
+ mml.setMmlFactory(customMmlFactory);
235
+
236
+ if (cachedMathjax) {
237
+ // if we have a cached version, we replace it here
238
+ window.MathJax = cachedMathjax;
239
+ }
240
+
241
+ return html;
242
+ };
243
+
244
+ let enrichSpeechInitialized = false;
245
+
246
+ const bootstrap = (opts) => {
247
+ if (typeof window === 'undefined') {
248
+ return { Typeset: () => ({}) };
249
+ }
250
+
251
+ const html = createMathMLInstance(opts);
252
+
253
+ return {
254
+ version: mathjax.version,
255
+ html: html,
256
+ Typeset: function(...elements) {
257
+ const attemptRender = (temporary = false) => {
258
+ let updatedDocument = this.html.findMath(elements.length ? { elements } : {}).compile();
259
+
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();
271
+
272
+ if (!enrichSpeechInitialized && typeof updatedDocument.math.list?.next?.data === 'object') {
273
+ enrichSpeechInitialized = true;
274
+ }
275
+
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());
291
+ }
292
+
293
+ updatedDocument.clear();
294
+ };
295
+
296
+ if (!enrichSpeechInitialized) {
297
+ attemptRender(true);
298
+ }
299
+
300
+ mathjax.handleRetriesFor(() => {
301
+ attemptRender();
302
+ });
303
+ },
304
+ };
305
+ };
306
+
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
+
317
+ const isString = typeof el === 'string';
318
+ let executeOn = document.body;
319
+
320
+ if (isString) {
321
+ const div = document.createElement('div');
322
+
323
+ div.innerHTML = el;
324
+ executeOn = div;
325
+ }
326
+
327
+ fixMathElements(executeOn);
328
+ adjustMathMLStyle(executeOn);
329
+
330
+ if (isString) {
331
+ const html = createMathMLInstance(undefined, executeOn);
332
+
333
+ const updatedDocument = html
334
+ .findMath()
335
+ .compile()
336
+ .getMetrics()
337
+ .typeset()
338
+ .updateDocument();
339
+
340
+ const list = updatedDocument.math.list;
341
+ const item = list.next;
342
+
343
+ if (!item) {
344
+ return '';
345
+ }
346
+
347
+ const mathMl = toMMl(item.data.root);
348
+ const parsedMathMl = mathMl.replaceAll('\n', '');
349
+
350
+ return parsedMathMl;
351
+ }
352
+
353
+ if (!getGlobal().instance) {
354
+ getGlobal().instance = bootstrap(renderOpts);
355
+ }
356
+
357
+ if (!el) {
358
+ log('el is undefined');
359
+ return;
360
+ }
361
+
362
+ if (el instanceof Element && getGlobal().instance?.Typeset) {
363
+ getGlobal().instance.Typeset(el);
364
+ } else if (el.length && getGlobal().instance?.Typeset) {
365
+ const arr = Array.from(el);
366
+ getGlobal().instance.Typeset(...arr);
367
+ }
368
+ };
369
+
370
+ /**
371
+ * This style is added to overried default styling of mjx-mspace Mathjax tag
372
+ * In mathjax src code \newline latex gets parsed to <mjx-mspace></mjx-mspace>,
373
+ * but has the default style
374
+ * 'mjx-mspace': {
375
+ "display": 'in-line',
376
+ "text-align": 'left'
377
+ } which prevents it from showing as a newline value
378
+ */
379
+ CHTMLmspace.styles = {
380
+ 'mjx-mspace': {
381
+ display: 'block',
382
+ 'text-align': 'center',
383
+ height: '5px',
384
+ },
385
+ };
386
+
387
+ export default renderMath;