@pie-lib/math-rendering 5.0.2 → 5.0.3-next.2

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.
@@ -1,410 +0,0 @@
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
- // import pkg from '../../package.json';
16
- import { chtmlNodes, mmlNodes } from './mstack';
17
- import debug from 'debug';
18
- import { unWrapMath, wrapMath } from './normalization';
19
- import { MmlFactory } from 'mathjax-full/js/core/MmlTree/MmlFactory';
20
- import { SerializedMmlVisitor } from 'mathjax-full/js/core/MmlTree/SerializedMmlVisitor';
21
- import { CHTMLWrapperFactory } from 'mathjax-full/js/output/chtml/WrapperFactory';
22
- import { CHTMLmspace } from 'mathjax-full/js/output/chtml/Wrappers/mspace';
23
- import { HTMLDomStrings } from 'mathjax-full/js/handlers/html/HTMLDomStrings';
24
-
25
- if (typeof window !== 'undefined') {
26
- RegisterHTMLHandler(browserAdaptor());
27
- }
28
-
29
- let sreReady = false;
30
-
31
- engineReady().then(() => {
32
- sreReady = true;
33
- });
34
-
35
- const visitor = new SerializedMmlVisitor();
36
- const toMMl = (node) => visitor.visitTree(node);
37
-
38
- const log = debug('pie-lib:math-rendering');
39
-
40
- const NEWLINE_BLOCK_REGEX = /\\embed\{newLine\}\[\]/g;
41
- const NEWLINE_LATEX = '\\newline ';
42
-
43
- const getGlobal = () => {
44
- // TODO does it make sense to use version?
45
- // const key = `${pkg.name}@${pkg.version.split('.')[0]}`;
46
- // It looks like Ed made this change when he switched from mathjax3 to mathjax-full
47
- // I think it was supposed to make sure version 1 (using mathjax3) is not used
48
- // in combination with version 2 (using mathjax-full)
49
-
50
- // TODO higher level wrappers use this instance of math-rendering, and if 2 different instances are used, math rendering is not working
51
- // so I will hardcode this for now until a better solution is found
52
- const key = '@pie-lib/math-rendering@2';
53
-
54
- if (typeof window !== 'undefined') {
55
- if (!window[key]) {
56
- window[key] = {};
57
- }
58
- return window[key];
59
- } else {
60
- return {};
61
- }
62
- };
63
-
64
- /** Add temporary support for a global singleDollar override
65
- * <code>
66
- * // This will enable single dollar rendering
67
- * window.pie = window.pie || {};
68
- * window.pie.mathRendering = {useSingleDollar: true };
69
- * </code>
70
- */
71
- const defaultOpts = () => getGlobal().opts || {};
72
-
73
- export const fixMathElement = (element) => {
74
- if (element.dataset.mathHandled) {
75
- return;
76
- }
77
-
78
- let property = 'innerText';
79
-
80
- if (element.textContent) {
81
- property = 'textContent';
82
- }
83
-
84
- if (element[property]) {
85
- element[property] = wrapMath(unWrapMath(element[property]).unwrapped);
86
- // because mathquill doesn't understand line breaks, sometimes we end up with custom elements on prompts/rationale/etc.
87
- // we need to replace the custom embedded elements with valid latex that Mathjax can understand
88
- element[property] = element[property].replace(NEWLINE_BLOCK_REGEX, NEWLINE_LATEX);
89
- element.dataset.mathHandled = true;
90
- }
91
- };
92
-
93
- export const fixMathElements = (el = document) => {
94
- const mathElements = el.querySelectorAll('[data-latex]');
95
-
96
- mathElements.forEach((item) => fixMathElement(item));
97
- };
98
-
99
- const adjustMathMLStyle = (el = document) => {
100
- const nodes = el.querySelectorAll('math');
101
- nodes.forEach((node) => node.setAttribute('displaystyle', 'true'));
102
- };
103
-
104
- class myFindMathML extends FindMathML {
105
- processMath(set) {
106
- const adaptor = this.adaptor;
107
- for (const mml of Array.from(set)) {
108
- if (adaptor.kind(adaptor.parent(mml)) === 'mjx-assistive-mml') {
109
- set.delete(mml);
110
- }
111
- }
112
- return super.processMath(set);
113
- }
114
- }
115
-
116
- const createMathMLInstance = (opts, docProvided = document) => {
117
- opts = opts || defaultOpts();
118
-
119
- if (opts.useSingleDollar) {
120
- // eslint-disable-next-line
121
- console.warn('[math-rendering] using $ is not advisable, please use $$..$$ or \\(...\\)');
122
- }
123
-
124
- const packages = AllPackages.filter((name) => name !== 'bussproofs'); // Bussproofs needs an output jax
125
-
126
- // The autoload extension predefines all the macros from the extensions that haven't been loaded already
127
- // so that they automatically load the needed extension when they are first used
128
- packages.push('autoload');
129
-
130
- const macros = {
131
- parallelogram: '\\lower.2em{\\Huge\\unicode{x25B1}}',
132
- overarc: '\\overparen',
133
- napprox: '\\not\\approx',
134
- longdiv: '\\enclose{longdiv}',
135
- };
136
-
137
- const texConfig = opts.useSingleDollar
138
- ? {
139
- packages,
140
- macros,
141
- inlineMath: [
142
- ['$', '$'],
143
- ['\\(', '\\)'],
144
- ],
145
- processEscapes: true,
146
- }
147
- : {
148
- packages,
149
- macros,
150
- };
151
-
152
- const mmlConfig = {
153
- parseError: function (node) {
154
- // function to process parsing errors
155
- // eslint-disable-next-line no-console
156
- console.log('error:', node);
157
- this.error(this.adaptor.textContent(node).replace(/\n.*/g, ''));
158
- },
159
- FindMathML: new myFindMathML(),
160
- };
161
-
162
- let cachedMathjax;
163
-
164
- if (globalMathjax && globalMathjax.version !== mathjax.version) {
165
- // handling other MathJax version on the page
166
- // replacing it temporarily with the version we have
167
- window.MathJax._ = window.MathJax._ || {};
168
- window.MathJax.config = window.MathJax.config || {};
169
- cachedMathjax = window.MathJax;
170
- Object.assign(globalMathjax, mathjax);
171
- }
172
-
173
- const fontURL = `https://unpkg.com/mathjax-full@${mathjax.version}/ts/output/chtml/fonts/tex-woff-v2`;
174
- const htmlConfig = {
175
- fontURL,
176
-
177
- wrapperFactory: new CHTMLWrapperFactory({
178
- ...CHTMLWrapperFactory.defaultNodes,
179
- ...chtmlNodes,
180
- }),
181
- };
182
-
183
- const mml = new MathML(mmlConfig);
184
-
185
- const customMmlFactory = new MmlFactory({
186
- ...MmlFactory.defaultNodes,
187
- ...mmlNodes,
188
- });
189
- const classFactory = EnrichHandler(
190
- MenuHandler(AssistiveMmlHandler(mathjax.handlers.handlesDocument(docProvided))),
191
- mml,
192
- );
193
-
194
- const html = classFactory.create(docProvided, {
195
- compileError: (mj, math, err) => {
196
- // eslint-disable-next-line no-console
197
- console.log('bad math?:', math);
198
- // eslint-disable-next-line no-console
199
- console.error(err);
200
- },
201
- typesetError: function (doc, math, err) {
202
- // eslint-disable-next-line no-console
203
- console.log('typeset error');
204
- // eslint-disable-next-line no-console
205
- console.error(err);
206
- doc.typesetError(math, err);
207
- },
208
-
209
- sre: {
210
- speech: 'deep',
211
- },
212
- enrichSpeech: 'deep',
213
-
214
- InputJax: [new TeX(texConfig), mml],
215
- OutputJax: new CHTML(htmlConfig),
216
- DomStrings: new HTMLDomStrings({
217
- skipHtmlTags: [
218
- 'script',
219
- 'noscript',
220
- 'style',
221
- 'textarea',
222
- 'pre',
223
- 'code',
224
- 'annotation',
225
- 'annotation-xml',
226
- 'mjx-assistive-mml',
227
- 'mjx-container',
228
- ],
229
- }),
230
- });
231
-
232
- // Note: we must set this *after* mathjax.document (no idea why)
233
- mml.setMmlFactory(customMmlFactory);
234
-
235
- if (cachedMathjax) {
236
- // if we have a cached version, we replace it here
237
- window.MathJax = cachedMathjax;
238
- }
239
-
240
- return html;
241
- };
242
-
243
- let enrichSpeechInitialized = false;
244
-
245
- const bootstrap = (opts) => {
246
- if (typeof window === 'undefined') {
247
- return { Typeset: () => ({}) };
248
- }
249
-
250
- const html = createMathMLInstance(opts);
251
-
252
- return {
253
- version: mathjax.version,
254
- html: html,
255
- Typeset: function (...elements) {
256
- const attemptRender = (temporary = false) => {
257
- let updatedDocument = this.html.findMath(elements.length ? { elements } : {}).compile();
258
-
259
- if (!temporary && sreReady) {
260
- try {
261
- updatedDocument = updatedDocument.enrich();
262
- } catch (e) {
263
- // If enrich fails, speech-rule-engine isn't actually ready yet
264
- // eslint-disable-next-line no-console
265
- console.warn('[math-rendering] Speech-rule-engine not fully initialized, skipping enrichment');
266
- sreReady = false;
267
- }
268
- }
269
-
270
- updatedDocument = updatedDocument.getMetrics().typeset();
271
-
272
- // assistiveMml() is what produces the <mjx-assistive-mml> element that
273
- // screen readers (VoiceOver, JAWS, NVDA) use to read math semantically.
274
- // It only serializes MathJax's internal MathML tree via LimitedMmlVisitor
275
- // and does NOT depend on speech-rule-engine, so it must always run –
276
- // otherwise fractions, roots, etc. get flattened to raw digits in AT.
277
- try {
278
- updatedDocument = updatedDocument.assistiveMml();
279
- } catch (e) {
280
- // eslint-disable-next-line no-console
281
- console.warn('[math-rendering] Failed to attach assistive MathML:', e);
282
- }
283
-
284
- // attachSpeech() relies on speech strings computed during enrich(),
285
- // which requires speech-rule-engine to be ready. Skip it gracefully
286
- // if SRE isn't initialised yet – screen readers will still read the
287
- // MathML structure produced above.
288
- if (!temporary && sreReady) {
289
- try {
290
- updatedDocument = updatedDocument.attachSpeech();
291
- } catch (e) {
292
- // eslint-disable-next-line no-console
293
- console.warn('[math-rendering] Speech-rule-engine not fully initialized, skipping speech attachment');
294
- sreReady = false;
295
- }
296
- }
297
-
298
- updatedDocument = updatedDocument.addMenu().updateDocument();
299
-
300
- if (!enrichSpeechInitialized && typeof updatedDocument.math.list?.next?.data === 'object') {
301
- enrichSpeechInitialized = true;
302
- }
303
-
304
- try {
305
- const list = updatedDocument.math.list;
306
-
307
- if (list) {
308
- for (let item = list.next; typeof item.data !== 'symbol'; item = item.next) {
309
- const mathMl = toMMl(item.data.root);
310
- const parsedMathMl = mathMl.replaceAll('\n', '');
311
-
312
- item.data.typesetRoot.setAttribute('data-mathml', parsedMathMl);
313
- item.data.typesetRoot.setAttribute('tabindex', '-1');
314
- }
315
- }
316
- } catch (e) {
317
- // eslint-disable-next-line no-console
318
- console.error(e.toString());
319
- }
320
-
321
- updatedDocument.clear();
322
- };
323
-
324
- if (!enrichSpeechInitialized) {
325
- attemptRender(true);
326
- }
327
-
328
- mathjax.handleRetriesFor(() => {
329
- attemptRender();
330
- });
331
- },
332
- };
333
- };
334
-
335
- const renderMath = (el, renderOpts) => {
336
- if (
337
- window &&
338
- window.MathJax &&
339
- window.MathJax.customKey &&
340
- window.MathJax.customKey == '@pie-lib/math-rendering-accessible@1'
341
- ) {
342
- return;
343
- }
344
-
345
- const isString = typeof el === 'string';
346
- let executeOn = document.body;
347
-
348
- if (isString) {
349
- const div = document.createElement('div');
350
-
351
- div.innerHTML = el;
352
- executeOn = div;
353
- }
354
-
355
- fixMathElements(executeOn);
356
- adjustMathMLStyle(executeOn);
357
-
358
- if (isString) {
359
- const html = createMathMLInstance(undefined, executeOn);
360
-
361
- const updatedDocument = html.findMath().compile().getMetrics().typeset().updateDocument();
362
-
363
- const list = updatedDocument.math.list;
364
- const item = list.next;
365
-
366
- if (!item) {
367
- return '';
368
- }
369
-
370
- const mathMl = toMMl(item.data.root);
371
- const parsedMathMl = mathMl.replaceAll('\n', '');
372
-
373
- return parsedMathMl;
374
- }
375
-
376
- if (!getGlobal().instance) {
377
- getGlobal().instance = bootstrap(renderOpts);
378
- }
379
-
380
- if (!el) {
381
- log('el is undefined');
382
- return;
383
- }
384
-
385
- if (el instanceof Element && getGlobal().instance?.Typeset) {
386
- getGlobal().instance.Typeset(el);
387
- } else if (el.length && getGlobal().instance?.Typeset) {
388
- const arr = Array.from(el);
389
- getGlobal().instance.Typeset(...arr);
390
- }
391
- };
392
-
393
- /**
394
- * This style is added to overried default styling of mjx-mspace Mathjax tag
395
- * In mathjax src code \newline latex gets parsed to <mjx-mspace></mjx-mspace>,
396
- * but has the default style
397
- * 'mjx-mspace': {
398
- "display": 'in-line',
399
- "text-align": 'left'
400
- } which prevents it from showing as a newline value
401
- */
402
- CHTMLmspace.styles = {
403
- 'mjx-mspace': {
404
- display: 'block',
405
- 'text-align': 'center',
406
- height: '5px',
407
- },
408
- };
409
-
410
- export default renderMath;