@pie-lib/math-rendering 5.0.3-next.2 → 5.1.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,411 @@
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
+ abs: ['\\left|#1\\right|', 1],
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
+ try {
262
+ updatedDocument = updatedDocument.enrich();
263
+ } catch (e) {
264
+ // If enrich fails, speech-rule-engine isn't actually ready yet
265
+ // eslint-disable-next-line no-console
266
+ console.warn('[math-rendering] Speech-rule-engine not fully initialized, skipping enrichment');
267
+ sreReady = false;
268
+ }
269
+ }
270
+
271
+ updatedDocument = updatedDocument.getMetrics().typeset();
272
+
273
+ // assistiveMml() is what produces the <mjx-assistive-mml> element that
274
+ // screen readers (VoiceOver, JAWS, NVDA) use to read math semantically.
275
+ // It only serializes MathJax's internal MathML tree via LimitedMmlVisitor
276
+ // and does NOT depend on speech-rule-engine, so it must always run –
277
+ // otherwise fractions, roots, etc. get flattened to raw digits in AT.
278
+ try {
279
+ updatedDocument = updatedDocument.assistiveMml();
280
+ } catch (e) {
281
+ // eslint-disable-next-line no-console
282
+ console.warn('[math-rendering] Failed to attach assistive MathML:', e);
283
+ }
284
+
285
+ // attachSpeech() relies on speech strings computed during enrich(),
286
+ // which requires speech-rule-engine to be ready. Skip it gracefully
287
+ // if SRE isn't initialised yet – screen readers will still read the
288
+ // MathML structure produced above.
289
+ if (!temporary && sreReady) {
290
+ try {
291
+ updatedDocument = updatedDocument.attachSpeech();
292
+ } catch (e) {
293
+ // eslint-disable-next-line no-console
294
+ console.warn('[math-rendering] Speech-rule-engine not fully initialized, skipping speech attachment');
295
+ sreReady = false;
296
+ }
297
+ }
298
+
299
+ updatedDocument = updatedDocument.addMenu().updateDocument();
300
+
301
+ if (!enrichSpeechInitialized && typeof updatedDocument.math.list?.next?.data === 'object') {
302
+ enrichSpeechInitialized = true;
303
+ }
304
+
305
+ try {
306
+ const list = updatedDocument.math.list;
307
+
308
+ if (list) {
309
+ for (let item = list.next; typeof item.data !== 'symbol'; item = item.next) {
310
+ const mathMl = toMMl(item.data.root);
311
+ const parsedMathMl = mathMl.replaceAll('\n', '');
312
+
313
+ item.data.typesetRoot.setAttribute('data-mathml', parsedMathMl);
314
+ item.data.typesetRoot.setAttribute('tabindex', '-1');
315
+ }
316
+ }
317
+ } catch (e) {
318
+ // eslint-disable-next-line no-console
319
+ console.error(e.toString());
320
+ }
321
+
322
+ updatedDocument.clear();
323
+ };
324
+
325
+ if (!enrichSpeechInitialized) {
326
+ attemptRender(true);
327
+ }
328
+
329
+ mathjax.handleRetriesFor(() => {
330
+ attemptRender();
331
+ });
332
+ },
333
+ };
334
+ };
335
+
336
+ const renderMath = (el, renderOpts) => {
337
+ if (
338
+ window &&
339
+ window.MathJax &&
340
+ window.MathJax.customKey &&
341
+ window.MathJax.customKey == '@pie-lib/math-rendering-accessible@1'
342
+ ) {
343
+ return;
344
+ }
345
+
346
+ const isString = typeof el === 'string';
347
+ let executeOn = document.body;
348
+
349
+ if (isString) {
350
+ const div = document.createElement('div');
351
+
352
+ div.innerHTML = el;
353
+ executeOn = div;
354
+ }
355
+
356
+ fixMathElements(executeOn);
357
+ adjustMathMLStyle(executeOn);
358
+
359
+ if (isString) {
360
+ const html = createMathMLInstance(undefined, executeOn);
361
+
362
+ const updatedDocument = html.findMath().compile().getMetrics().typeset().updateDocument();
363
+
364
+ const list = updatedDocument.math.list;
365
+ const item = list.next;
366
+
367
+ if (!item) {
368
+ return '';
369
+ }
370
+
371
+ const mathMl = toMMl(item.data.root);
372
+ const parsedMathMl = mathMl.replaceAll('\n', '');
373
+
374
+ return parsedMathMl;
375
+ }
376
+
377
+ if (!getGlobal().instance) {
378
+ getGlobal().instance = bootstrap(renderOpts);
379
+ }
380
+
381
+ if (!el) {
382
+ log('el is undefined');
383
+ return;
384
+ }
385
+
386
+ if (el instanceof Element && getGlobal().instance?.Typeset) {
387
+ getGlobal().instance.Typeset(el);
388
+ } else if (el.length && getGlobal().instance?.Typeset) {
389
+ const arr = Array.from(el);
390
+ getGlobal().instance.Typeset(...arr);
391
+ }
392
+ };
393
+
394
+ /**
395
+ * This style is added to overried default styling of mjx-mspace Mathjax tag
396
+ * In mathjax src code \newline latex gets parsed to <mjx-mspace></mjx-mspace>,
397
+ * but has the default style
398
+ * 'mjx-mspace': {
399
+ "display": 'in-line',
400
+ "text-align": 'left'
401
+ } which prevents it from showing as a newline value
402
+ */
403
+ CHTMLmspace.styles = {
404
+ 'mjx-mspace': {
405
+ display: 'block',
406
+ 'text-align': 'center',
407
+ height: '5px',
408
+ },
409
+ };
410
+
411
+ export default renderMath;
package/dist/index.d.ts DELETED
@@ -1,8 +0,0 @@
1
- /**
2
- * @synced-from pie-lib/packages/math-rendering
3
- * @auto-generated
4
- *
5
- * This is a thin wrapper that re-exports from @pie-element/shared-math-rendering-mathjax.
6
- * The actual implementation is in packages/shared/math-rendering-mathjax.
7
- */
8
- export { renderMath, wrapMath, unWrapMath, mmlToLatex } from '@pie-element/shared-math-rendering-mathjax';
package/dist/index.js DELETED
@@ -1,2 +0,0 @@
1
- import { mmlToLatex as e, renderMath as t, unWrapMath as n, wrapMath as r } from "@pie-element/shared-math-rendering-mathjax";
2
- export { e as mmlToLatex, t as renderMath, n as unWrapMath, r as wrapMath };