@pie-lib/math-rendering-accessible 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.json +1 -0
- package/CHANGELOG.md +28 -0
- package/LICENSE.md +5 -0
- package/NEXT.CHANGELOG.json +1 -0
- package/package.json +24 -0
- package/src/__tests__/mml-to-latex.test.js +13 -0
- package/src/__tests__/normalization.test.js +51 -0
- package/src/__tests__/render-math.test.js +76 -0
- package/src/index.js +5 -0
- package/src/mathjax-script.js +0 -0
- package/src/mml-to-latex.js +2 -0
- package/src/mstack/__tests__/__snapshots__/chtml.test.js.snap +9 -0
- package/src/mstack/__tests__/chtml.test.js +104 -0
- package/src/mstack/chtml.js +220 -0
- package/src/mstack/index.js +13 -0
- package/src/mstack/mml.js +24 -0
- package/src/normalization.js +69 -0
- package/src/render-math.js +450 -0
package/CHANGELOG.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
[]
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
## 3.3.1-beta.0 (2025-07-20)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @pie-lib/math-rendering-accessible
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# 3.3.0-beta.0 (2025-07-20)
|
|
15
|
+
|
|
16
|
+
**Note:** Version bump only for package @pie-lib/math-rendering-accessible
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# 3.4.0-beta.0 (2025-07-15)
|
|
23
|
+
|
|
24
|
+
**Note:** Version bump only for package @pie-lib/math-rendering-accessible
|
|
25
|
+
|
|
26
|
+
# 3.3.0-beta.0 (2025-07-15)
|
|
27
|
+
|
|
28
|
+
**Note:** Version bump only for package @pie-lib/math-rendering-accessible
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
Copyright 2019 CoreSpring Inc
|
|
2
|
+
|
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
|
4
|
+
|
|
5
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
[]
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pie-lib/math-rendering-accessible",
|
|
3
|
+
"version": "3.3.1-beta.0",
|
|
4
|
+
"description": "math rendering utilities",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"main": "lib/index.js",
|
|
9
|
+
"module": "src/index.js",
|
|
10
|
+
"author": "pie-framework developers",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@pie-framework/mathml-to-latex": "1.4.4",
|
|
14
|
+
"@pie-lib/math-rendering": "beta",
|
|
15
|
+
"debug": "^4.1.1",
|
|
16
|
+
"lodash": "^4.17.11",
|
|
17
|
+
"mathjax-full": "3.2.2",
|
|
18
|
+
"mathml-to-latex": "1.2.0",
|
|
19
|
+
"react": "^16.8.1",
|
|
20
|
+
"slate": "^0.36.2"
|
|
21
|
+
},
|
|
22
|
+
"gitHead": "e2aa3ddac60f49bcb8c2562370f496323642f453",
|
|
23
|
+
"scripts": {}
|
|
24
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import mmlToLatex from '../mml-to-latex';
|
|
3
|
+
describe('mmlToLatex', () => {
|
|
4
|
+
it('should work', () => {
|
|
5
|
+
const mml =
|
|
6
|
+
'<math xmlns="http://www.w3.org/1998/Math/MathML"> <mn>2</mn> <mi>x</mi> <mtext> </mtext> <mo>≤</mo> <mn>4</mn> <mi>y</mi> <mtext> </mtext> <mo>+</mo> <mtext> </mtext> <mn>8</mn> <msqrt> <mi>h</mi> </msqrt></math>';
|
|
7
|
+
// todo revisit this
|
|
8
|
+
// const latex = '2x\\text{ }\\leq4y\\text{ }+\\text{ }8\\sqrt{h}';
|
|
9
|
+
const latex = '2 x \\textrm{ } \\leq 4 y \\textrm{ } + \\textrm{ } 8 \\sqrt{h}';
|
|
10
|
+
|
|
11
|
+
expect(mmlToLatex(mml)).toEqual(latex);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import debug from 'debug';
|
|
3
|
+
import { Data } from 'slate';
|
|
4
|
+
import { BracketTypes, wrapMath, unWrapMath } from '../normalization';
|
|
5
|
+
|
|
6
|
+
const log = debug('@pie-lib:math-rendering:test:normalization');
|
|
7
|
+
|
|
8
|
+
describe('normalization', () => {
|
|
9
|
+
describe('unWrapMath', () => {
|
|
10
|
+
const assertUnWrap = (html, expected, wrapType) => {
|
|
11
|
+
it(`innerHTML: ${html} is unWrapped to: ${expected} with wrapType: ${wrapType}`, () => {
|
|
12
|
+
const out = unWrapMath(html);
|
|
13
|
+
|
|
14
|
+
expect(out).toEqual({
|
|
15
|
+
unwrapped: expected,
|
|
16
|
+
wrapType: wrapType,
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
assertUnWrap('$$<$$', '<', BracketTypes.DOLLAR);
|
|
22
|
+
assertUnWrap('$<$', '<', BracketTypes.DOLLAR);
|
|
23
|
+
assertUnWrap('\\(<\\)', '<', BracketTypes.ROUND_BRACKETS);
|
|
24
|
+
assertUnWrap('\\[<\\]', '<', BracketTypes.ROUND_BRACKETS);
|
|
25
|
+
assertUnWrap('latex', 'latex', BracketTypes.ROUND_BRACKETS);
|
|
26
|
+
assertUnWrap('\\displaystyle foo', 'foo', BracketTypes.ROUND_BRACKETS);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('wrapMath', () => {
|
|
30
|
+
const assertWrap = (latex, expectedHtml, wrapper) => {
|
|
31
|
+
wrapper = wrapper || BracketTypes.ROUND_BRACKETS;
|
|
32
|
+
it(`${latex} is wrapped to: ${expectedHtml}`, () => {
|
|
33
|
+
const out = wrapMath(latex, wrapper);
|
|
34
|
+
|
|
35
|
+
log('out: ', out);
|
|
36
|
+
|
|
37
|
+
expect(out).toEqual(expectedHtml);
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
assertWrap('latex', '\\(latex\\)', BracketTypes.ROUND_BRACKETS);
|
|
42
|
+
assertWrap('latex', '\\(latex\\)', BracketTypes.SQUARE_BRACKETS);
|
|
43
|
+
assertWrap('latex', '$latex$', BracketTypes.DOLLAR);
|
|
44
|
+
assertWrap('latex', '$latex$', BracketTypes.DOUBLE_DOLLAR);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Note that when this is converted to html it get's escaped - but that's an issue with the slate html-serializer.
|
|
48
|
+
*/
|
|
49
|
+
assertWrap('<', '\\(<\\)');
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { mount } from 'enzyme';
|
|
3
|
+
import renderMath, { fixMathElement } from '../render-math';
|
|
4
|
+
// import * as MathJaxModule from '../mathjax-script';
|
|
5
|
+
import _ from 'lodash';
|
|
6
|
+
|
|
7
|
+
jest.mock('../mathjax-script', () => ({
|
|
8
|
+
initializeMathJax: jest.fn(),
|
|
9
|
+
}));
|
|
10
|
+
|
|
11
|
+
describe('render-math', () => {
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
// Reset the mocks before each test
|
|
14
|
+
jest.resetAllMocks();
|
|
15
|
+
// Mock window.MathJax and window.mathjaxLoadedP
|
|
16
|
+
global.window.MathJax = {
|
|
17
|
+
typeset: jest.fn(),
|
|
18
|
+
texReset: jest.fn(),
|
|
19
|
+
typesetClear: jest.fn(),
|
|
20
|
+
typeset: jest.fn(),
|
|
21
|
+
typesetPromise: jest.fn(() => Promise.resolve()),
|
|
22
|
+
};
|
|
23
|
+
global.window.mathjaxLoadedP = Promise.resolve();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('calls initializeMathJax once without @pie-lib/math-rendering@2', () => {
|
|
27
|
+
jest.useFakeTimers();
|
|
28
|
+
const div = document.createElement('div');
|
|
29
|
+
|
|
30
|
+
delete window['@pie-lib/math-rendering@2'];
|
|
31
|
+
|
|
32
|
+
// Initialize as undefined for the first call
|
|
33
|
+
global.window.MathJax = undefined;
|
|
34
|
+
global.window.mathjaxLoadedP = undefined;
|
|
35
|
+
|
|
36
|
+
// Call renderMath once to initialize MathJax
|
|
37
|
+
renderMath(div);
|
|
38
|
+
|
|
39
|
+
// Subsequent calls should not re-initialize MathJax
|
|
40
|
+
global.window.MathJax = {
|
|
41
|
+
typeset: jest.fn(),
|
|
42
|
+
texReset: jest.fn(),
|
|
43
|
+
typesetClear: jest.fn(),
|
|
44
|
+
typeset: jest.fn(),
|
|
45
|
+
typesetPromise: jest.fn(() => Promise.resolve()),
|
|
46
|
+
};
|
|
47
|
+
global.window.mathjaxLoadedP = Promise.resolve();
|
|
48
|
+
|
|
49
|
+
// Call renderMath 9 more times
|
|
50
|
+
_.times(9).forEach((i) => renderMath(div));
|
|
51
|
+
|
|
52
|
+
// setTimeout(() => {
|
|
53
|
+
// expect(MathJaxModule.initializeMathJax).toHaveBeenCalledTimes(1);
|
|
54
|
+
// }, 500);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('does not call initializeMathJax when @pie-lib/math-rendering@2 is present', () => {
|
|
58
|
+
const div = document.createElement('div');
|
|
59
|
+
|
|
60
|
+
renderMath(div);
|
|
61
|
+
// expect(MathJaxModule.initializeMathJax).not.toHaveBeenCalled();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('wraps the math containing element the right way', () => {
|
|
65
|
+
const wrapper = mount(
|
|
66
|
+
<div>
|
|
67
|
+
<span data-latex="">{'420\\text{ cm}=4.2\\text{ meters}'}</span>
|
|
68
|
+
</div>,
|
|
69
|
+
);
|
|
70
|
+
const spanElem = wrapper.instance();
|
|
71
|
+
|
|
72
|
+
fixMathElement(spanElem);
|
|
73
|
+
|
|
74
|
+
expect(spanElem.textContent).toEqual('\\(420\\text{ cm}=4.2\\text{ meters}\\)');
|
|
75
|
+
});
|
|
76
|
+
});
|
package/src/index.js
ADDED
|
File without changes
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`chtml implicit one row 1`] = `"<table><tr><td></td><td>1</td></tr></table>"`;
|
|
4
|
+
|
|
5
|
+
exports[`chtml one row 1`] = `"<table><tr><td></td><td>1</td></tr></table>"`;
|
|
6
|
+
|
|
7
|
+
exports[`chtml two rows 1`] = `"<table><tr><td></td><td>1</td></tr><tr><td></td><td>2</td></tr></table>"`;
|
|
8
|
+
|
|
9
|
+
exports[`chtml two rows with operator 1`] = `"<table><tr><td></td><td>1</td></tr><tr><td class=\\"inner\\">mo:+</td><td>2</td></tr></table>"`;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { getStackData, Line, Row, CHTMLmstack } from '../chtml';
|
|
2
|
+
// import { CHTMLWrapper, instance } from 'mathjax-full/js/output/chtml/Wrapper';
|
|
3
|
+
import { JSDOM } from 'jsdom';
|
|
4
|
+
|
|
5
|
+
jest.mock('mathjax-full/js/output/chtml/Wrapper', () => {
|
|
6
|
+
const instance = {
|
|
7
|
+
adaptor: {
|
|
8
|
+
document: {
|
|
9
|
+
createElement: jest.fn(),
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
standardCHTMLnode: jest.fn(),
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
instance,
|
|
17
|
+
CHTMLWrapper: class {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.adaptor = { document: { createElement: jest.fn() } };
|
|
20
|
+
this.document = {};
|
|
21
|
+
// return instance;
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const node = (kind, extras) => ({ kind, childNodes: [], ...extras });
|
|
28
|
+
|
|
29
|
+
const textNode = (text) => node('text', { text, node: { kind: 'text', text } });
|
|
30
|
+
const mn = (text) => node('mn', { childNodes: [textNode(text)] });
|
|
31
|
+
const mo = (text) =>
|
|
32
|
+
node('mo', {
|
|
33
|
+
childNodes: [textNode(text)],
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const mco = (text) => ({
|
|
37
|
+
...mo(text),
|
|
38
|
+
|
|
39
|
+
toCHTML: (n) => {
|
|
40
|
+
const t = `mo:${text}`;
|
|
41
|
+
n.textContent = t;
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const msrow = (...childNodes) => node('msrow', { childNodes });
|
|
46
|
+
const mstack = (...rows) => node('mstack', { childNodes: rows });
|
|
47
|
+
const msline = () => node('msline');
|
|
48
|
+
describe('getStackData', () => {
|
|
49
|
+
it.each`
|
|
50
|
+
input | expected
|
|
51
|
+
${mstack(msrow(mo('+'), mn('111')))} | ${[new Row(['1', '1', '1'], mo('+'))]}
|
|
52
|
+
${mstack(msrow(mn('111')))} | ${[new Row(['1', '1', '1'])]}
|
|
53
|
+
${mstack(msrow(mn('1'), mn('1')))} | ${[new Row(['1', '1'])]}
|
|
54
|
+
${mstack(msrow(mn('1')), mn('1'))} | ${[new Row(['1']), new Row(['1'])]}
|
|
55
|
+
${mstack(msline())} | ${[new Line()]}
|
|
56
|
+
${mstack(mn('1'), msline(), msrow(mo('+'), mn('1')))} | ${[new Row(['1']), new Line(), new Row(['1'], mo('+'))]}
|
|
57
|
+
${mstack(mn('1'), mn('1'))} | ${[new Row(['1']), new Row(['1'])]}
|
|
58
|
+
`('$input => $expected', ({ input, expected }) => {
|
|
59
|
+
const d = getStackData(input);
|
|
60
|
+
// console.log('d:', d);
|
|
61
|
+
// console.log('e:', expected);
|
|
62
|
+
expect({ ...d }).toEqual({ ...expected });
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('Row', () => {
|
|
67
|
+
describe('pad', () => {
|
|
68
|
+
it.each`
|
|
69
|
+
cols | count | expected
|
|
70
|
+
${[]} | ${0} | ${[]}
|
|
71
|
+
${[1]} | ${1} | ${[1]}
|
|
72
|
+
${[1]} | ${2} | ${['__pad__', 1]}
|
|
73
|
+
${[1]} | ${3} | ${['__pad__', '__pad__', 1]}
|
|
74
|
+
`('pads to the right', ({ cols, count, expected }) => {
|
|
75
|
+
const r = new Row(cols);
|
|
76
|
+
const p = r.pad(count, 'right');
|
|
77
|
+
expect(p).toEqual(expected);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe.each`
|
|
83
|
+
label | tree
|
|
84
|
+
${'one row'} | ${[msrow(mn('1'))]}
|
|
85
|
+
${'implicit one row'} | ${[mn('1')]}
|
|
86
|
+
${'two rows'} | ${[msrow(mn('1')), msrow(mn('2'))]}
|
|
87
|
+
${'two rows with operator'} | ${[msrow(mn('1')), msrow(mco('+'), mn('2'))]}
|
|
88
|
+
`('chtml', ({ label, tree }) => {
|
|
89
|
+
let html;
|
|
90
|
+
|
|
91
|
+
beforeEach(() => {
|
|
92
|
+
const chtml = new CHTMLmstack({}, {});
|
|
93
|
+
const dom = new JSDOM(`<!DOCTYPE html><body></body>`);
|
|
94
|
+
chtml.standardCHTMLnode = (parent) => parent;
|
|
95
|
+
chtml.ce = dom.window.document.createElement.bind(dom.window.document);
|
|
96
|
+
chtml.childNodes = tree;
|
|
97
|
+
chtml.toCHTML(dom.window.document.body);
|
|
98
|
+
html = dom.window.document.body.innerHTML;
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it(label, () => {
|
|
102
|
+
expect(html).toMatchSnapshot();
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { CHTMLWrapper } from 'mathjax-full/js/output/chtml/Wrapper';
|
|
2
|
+
import _ from 'lodash';
|
|
3
|
+
|
|
4
|
+
const reduceText = (acc, n) => {
|
|
5
|
+
if (n.node && n.node.kind === 'text') {
|
|
6
|
+
acc += n.node.text;
|
|
7
|
+
}
|
|
8
|
+
return acc;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export class Line {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.kind = 'line';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
get columns() {
|
|
17
|
+
return [];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class Row {
|
|
22
|
+
constructor(columns, operator) {
|
|
23
|
+
this.kind = 'row';
|
|
24
|
+
this.operator = operator;
|
|
25
|
+
this.columns = columns;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
pad(count, direction = 'right') {
|
|
29
|
+
if (count < this.columns.length) {
|
|
30
|
+
throw new Error('no');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const diff = count - this.columns.length;
|
|
34
|
+
|
|
35
|
+
const padding = _.times(diff).map(() => '__pad__');
|
|
36
|
+
return direction === 'right' ? [...padding, ...this.columns] : [...this.columns, ...padding];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const mathNodeToCharArray = (mn) => {
|
|
41
|
+
const text = mn.childNodes.reduce(reduceText, '');
|
|
42
|
+
return text.split('');
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Convert child a column entry
|
|
47
|
+
* @param {*} child
|
|
48
|
+
* @return an array of column content
|
|
49
|
+
*/
|
|
50
|
+
const toColumnArray = (child) => {
|
|
51
|
+
if (!child || !child.kind) {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (child.kind === 'msrow') {
|
|
56
|
+
throw new Error('msrow in msrow?');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (child.kind === 'mo') {
|
|
60
|
+
// We are going to treat this operator as a text array.
|
|
61
|
+
// It's probably going to be a decimal point
|
|
62
|
+
// eslint-disable-next-line no-console
|
|
63
|
+
console.warn('mo that is not 1st node in msrow?');
|
|
64
|
+
return mathNodeToCharArray(child);
|
|
65
|
+
// throw new Error('mo must be first child of msrow');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (child.kind === 'mn') {
|
|
69
|
+
return mathNodeToCharArray(child);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (child.toCHTML) {
|
|
73
|
+
return child;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* convert mstack chtml childNodes into a Row
|
|
79
|
+
* @param child chtml child node of mstack
|
|
80
|
+
* @return Row | Line
|
|
81
|
+
*/
|
|
82
|
+
const rowStack = (child) => {
|
|
83
|
+
if (!child || !child.kind) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (child.kind === 'msrow') {
|
|
88
|
+
if (!child.childNodes || child.childNodes.length === 0) {
|
|
89
|
+
return new Row([]);
|
|
90
|
+
}
|
|
91
|
+
const f = _.first(child.childNodes);
|
|
92
|
+
const nodes = f && f.kind === 'mo' ? _.tail(child.childNodes) : child.childNodes;
|
|
93
|
+
|
|
94
|
+
const columns = _.flatten(nodes.map(toColumnArray));
|
|
95
|
+
|
|
96
|
+
return new Row(columns, f.kind === 'mo' ? f : undefined);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (child.kind === 'mn') {
|
|
100
|
+
const columns = mathNodeToCharArray(child);
|
|
101
|
+
return new Row(columns, undefined);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (child.kind === 'mo') {
|
|
105
|
+
// eslint-disable-next-line no-console
|
|
106
|
+
console.warn('mo on its own row?');
|
|
107
|
+
return new Row([], child);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (child.kind === 'msline') {
|
|
111
|
+
return new Line();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (child.toCHTML) {
|
|
115
|
+
return new Row([child]);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/** convert MathJax chtml tree to Row[]
|
|
120
|
+
* @param mstack the root of the mathjax chtml tree
|
|
121
|
+
* @return Row[]
|
|
122
|
+
*/
|
|
123
|
+
|
|
124
|
+
export const getStackData = (mstack) => {
|
|
125
|
+
if (!mstack || !mstack.childNodes) {
|
|
126
|
+
return [];
|
|
127
|
+
}
|
|
128
|
+
return _.compact(mstack.childNodes.map(rowStack));
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export class CHTMLmstack extends CHTMLWrapper {
|
|
132
|
+
constructor(factory, node, parent = null) {
|
|
133
|
+
super(factory, node, parent);
|
|
134
|
+
|
|
135
|
+
this.ce = this.adaptor.document.createElement.bind(this.adaptor.document);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
toCHTML(parent) {
|
|
139
|
+
const chtml = this.standardCHTMLnode(parent);
|
|
140
|
+
|
|
141
|
+
const stackData = getStackData(this);
|
|
142
|
+
|
|
143
|
+
// console.log('stackData', stackData);
|
|
144
|
+
const maxCols = stackData.reduce((acc, r) => {
|
|
145
|
+
if (r && r.columns.length > acc) {
|
|
146
|
+
acc = r.columns.length;
|
|
147
|
+
}
|
|
148
|
+
return acc;
|
|
149
|
+
}, 0);
|
|
150
|
+
|
|
151
|
+
const table = this.ce('table');
|
|
152
|
+
chtml.appendChild(table);
|
|
153
|
+
|
|
154
|
+
stackData.forEach((row) => {
|
|
155
|
+
const tr = this.ce('tr');
|
|
156
|
+
table.appendChild(tr);
|
|
157
|
+
|
|
158
|
+
if (row.kind === 'row') {
|
|
159
|
+
const td = this.ce('td');
|
|
160
|
+
tr.appendChild(td);
|
|
161
|
+
if (row.operator && row.operator.toCHTML) {
|
|
162
|
+
td.setAttribute('class', 'inner');
|
|
163
|
+
row.operator.toCHTML(td);
|
|
164
|
+
} else {
|
|
165
|
+
td.textContent = '';
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// align right for now:
|
|
169
|
+
const cols = row.pad(maxCols, 'right');
|
|
170
|
+
cols.forEach((c) => {
|
|
171
|
+
const t = this.ce('td');
|
|
172
|
+
tr.appendChild(t);
|
|
173
|
+
if (c === '__pad__') {
|
|
174
|
+
t.textContent = '';
|
|
175
|
+
} else if (typeof c === 'string') {
|
|
176
|
+
t.textContent = c;
|
|
177
|
+
} else if (c.kind === 'none') {
|
|
178
|
+
t.textContent = '';
|
|
179
|
+
} else if (c.toCHTML) {
|
|
180
|
+
t.setAttribute('class', 'inner');
|
|
181
|
+
c.toCHTML(t);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
} else if (row.kind === 'line') {
|
|
185
|
+
const td = this.ce('td');
|
|
186
|
+
tr.appendChild(td);
|
|
187
|
+
td.setAttribute('colspan', maxCols + 1);
|
|
188
|
+
td.setAttribute('class', 'mjx-line');
|
|
189
|
+
td.textContent = '';
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
CHTMLmstack.styles = {
|
|
195
|
+
'mjx-mstack > table': {
|
|
196
|
+
'line-height': 'initial',
|
|
197
|
+
border: 'solid 0px red',
|
|
198
|
+
'border-spacing': '0em',
|
|
199
|
+
'border-collapse': 'separate',
|
|
200
|
+
},
|
|
201
|
+
'mjx-mstack > table > tr': {
|
|
202
|
+
'line-height': 'initial',
|
|
203
|
+
},
|
|
204
|
+
'mjx-mstack > table > tr > td': {
|
|
205
|
+
// padding: '1.2rem',
|
|
206
|
+
border: 'solid 0px blue',
|
|
207
|
+
'font-family': 'sans-serif',
|
|
208
|
+
'line-height': 'initial',
|
|
209
|
+
},
|
|
210
|
+
'mjx-mstack > table > tr > td.inner': {
|
|
211
|
+
'font-family': 'inherit',
|
|
212
|
+
},
|
|
213
|
+
'mjx-mstack > table > tr > .mjx-line': {
|
|
214
|
+
padding: 0,
|
|
215
|
+
'border-top': 'solid 1px black',
|
|
216
|
+
},
|
|
217
|
+
'.TEX-A': {
|
|
218
|
+
'font-family': 'MJXZERO, MJXTEX !important',
|
|
219
|
+
},
|
|
220
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CHTMLmstack } from './chtml';
|
|
2
|
+
import { MmlNone, MmlMsline, MmlMstack, MmlMsrow } from './mml';
|
|
3
|
+
|
|
4
|
+
export const chtmlNodes = {
|
|
5
|
+
mstack: CHTMLmstack,
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const mmlNodes = {
|
|
9
|
+
mstack: MmlMstack,
|
|
10
|
+
msline: MmlMsline,
|
|
11
|
+
msrow: MmlMsrow,
|
|
12
|
+
none: MmlNone,
|
|
13
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { AbstractMmlNode } from 'mathjax-full/js/core/MmlTree/MmlNode';
|
|
2
|
+
|
|
3
|
+
export class MmlNone extends AbstractMmlNode {
|
|
4
|
+
get kind() {
|
|
5
|
+
return 'none';
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class MmlMstack extends AbstractMmlNode {
|
|
10
|
+
get kind() {
|
|
11
|
+
return 'mstack';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class MmlMsrow extends AbstractMmlNode {
|
|
16
|
+
get kind() {
|
|
17
|
+
return 'msrow';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class MmlMsline extends AbstractMmlNode {
|
|
21
|
+
get kind() {
|
|
22
|
+
return 'msline';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export const BracketTypes = {};
|
|
2
|
+
|
|
3
|
+
BracketTypes.ROUND_BRACKETS = 'round_brackets';
|
|
4
|
+
BracketTypes.SQUARE_BRACKETS = 'square_brackets';
|
|
5
|
+
BracketTypes.DOLLAR = 'dollar';
|
|
6
|
+
BracketTypes.DOUBLE_DOLLAR = 'double_dollar';
|
|
7
|
+
|
|
8
|
+
const PAIRS = {
|
|
9
|
+
[BracketTypes.ROUND_BRACKETS]: ['\\(', '\\)'],
|
|
10
|
+
[BracketTypes.SQUARE_BRACKETS]: ['\\[', '\\]'],
|
|
11
|
+
[BracketTypes.DOLLAR]: ['$', '$'],
|
|
12
|
+
[BracketTypes.DOUBLE_DOLLAR]: ['$$', '$$'],
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const wrapMath = (content, wrapType) => {
|
|
16
|
+
if (wrapType === BracketTypes.SQUARE_BRACKETS) {
|
|
17
|
+
console.warn('\\[...\\] is not supported yet'); // eslint-disable-line
|
|
18
|
+
wrapType = BracketTypes.ROUND_BRACKETS;
|
|
19
|
+
}
|
|
20
|
+
if (wrapType === BracketTypes.DOUBLE_DOLLAR) {
|
|
21
|
+
console.warn('$$...$$ is not supported yet'); // eslint-disable-line
|
|
22
|
+
wrapType = BracketTypes.DOLLAR;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const [start, end] = PAIRS[wrapType] || PAIRS[BracketTypes.ROUND_BRACKETS];
|
|
26
|
+
return `${start}${content}${end}`;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const unWrapMath = (content) => {
|
|
30
|
+
const displayStyleIndex = content.indexOf('\\displaystyle');
|
|
31
|
+
if (displayStyleIndex !== -1) {
|
|
32
|
+
console.warn('\\displaystyle is not supported - removing'); // eslint-disable-line
|
|
33
|
+
content = content.replace('\\displaystyle', '').trim();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (content.startsWith('$$') && content.endsWith('$$')) {
|
|
37
|
+
console.warn('$$ syntax is not yet supported'); // eslint-disable-line
|
|
38
|
+
return {
|
|
39
|
+
unwrapped: content.substring(2, content.length - 2),
|
|
40
|
+
wrapType: BracketTypes.DOLLAR,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (content.startsWith('$') && content.endsWith('$')) {
|
|
44
|
+
return {
|
|
45
|
+
unwrapped: content.substring(1, content.length - 1),
|
|
46
|
+
wrapType: BracketTypes.DOLLAR,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (content.startsWith('\\[') && content.endsWith('\\]')) {
|
|
51
|
+
console.warn('\\[..\\] syntax is not yet supported'); // eslint-disable-line
|
|
52
|
+
return {
|
|
53
|
+
unwrapped: content.substring(2, content.length - 2),
|
|
54
|
+
wrapType: BracketTypes.ROUND_BRACKETS,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (content.startsWith('\\(') && content.endsWith('\\)')) {
|
|
59
|
+
return {
|
|
60
|
+
unwrapped: content.substring(2, content.length - 2),
|
|
61
|
+
wrapType: BracketTypes.ROUND_BRACKETS,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
unwrapped: content,
|
|
67
|
+
wrapType: BracketTypes.ROUND_BRACKETS,
|
|
68
|
+
};
|
|
69
|
+
};
|
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
import { wrapMath, unWrapMath } from './normalization';
|
|
2
|
+
import { SerializedMmlVisitor } from 'mathjax-full/js/core/MmlTree/SerializedMmlVisitor';
|
|
3
|
+
import TexError from 'mathjax-full/js/input/tex/TexError';
|
|
4
|
+
|
|
5
|
+
const visitor = new SerializedMmlVisitor();
|
|
6
|
+
const toMMl = (node) => visitor.visitTree(node);
|
|
7
|
+
|
|
8
|
+
const NEWLINE_BLOCK_REGEX = /\\embed\{newLine\}\[\]/g;
|
|
9
|
+
const NEWLINE_LATEX = '\\newline ';
|
|
10
|
+
|
|
11
|
+
const mathRenderingKEY = '@pie-lib/math-rendering@2';
|
|
12
|
+
const mathRenderingAccessibleKEY = '@pie-lib/math-rendering-accessible@1';
|
|
13
|
+
|
|
14
|
+
export const MathJaxVersion = '3.2.2';
|
|
15
|
+
|
|
16
|
+
export const getGlobal = () => {
|
|
17
|
+
// TODO does it make sense to use version?
|
|
18
|
+
// const key = `${pkg.name}@${pkg.version.split('.')[0]}`;
|
|
19
|
+
// It looks like Ed made this change when he switched from mathjax3 to mathjax-full
|
|
20
|
+
// I think it was supposed to make sure version 1 (using mathjax3) is not used
|
|
21
|
+
// in combination with version 2 (using mathjax-full)
|
|
22
|
+
// TODO higher level wrappers use this instance of math-rendering, and if 2 different instances are used, math rendering is not working
|
|
23
|
+
// so I will hardcode this for now until a better solution is found
|
|
24
|
+
if (typeof window !== 'undefined') {
|
|
25
|
+
if (!window[mathRenderingAccessibleKEY]) {
|
|
26
|
+
window[mathRenderingAccessibleKEY] = {};
|
|
27
|
+
}
|
|
28
|
+
return window[mathRenderingAccessibleKEY];
|
|
29
|
+
} else {
|
|
30
|
+
return {};
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const fixMathElement = (element) => {
|
|
35
|
+
if (element.dataset.mathHandled) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let property = 'innerText';
|
|
40
|
+
|
|
41
|
+
if (element.textContent) {
|
|
42
|
+
property = 'textContent';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (element[property]) {
|
|
46
|
+
element[property] = wrapMath(unWrapMath(element[property]).unwrapped);
|
|
47
|
+
// because mathquill doesn't understand line breaks, sometimes we end up with custom elements on prompts/rationale/etc.
|
|
48
|
+
// we need to replace the custom embedded elements with valid latex that Mathjax can understand
|
|
49
|
+
element[property] = element[property].replace(NEWLINE_BLOCK_REGEX, NEWLINE_LATEX);
|
|
50
|
+
element.dataset.mathHandled = true;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const fixMathElements = (el = document) => {
|
|
55
|
+
const mathElements = el.querySelectorAll('[data-latex]');
|
|
56
|
+
|
|
57
|
+
mathElements.forEach((item) => fixMathElement(item));
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const adjustMathMLStyle = (el = document) => {
|
|
61
|
+
const nodes = el.querySelectorAll('math');
|
|
62
|
+
nodes.forEach((node) => node.setAttribute('displaystyle', 'true'));
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const createPlaceholder = (element) => {
|
|
66
|
+
if (!element.previousSibling || !element.previousSibling.classList?.contains('math-placeholder')) {
|
|
67
|
+
// Store the original display style before setting it to 'none'
|
|
68
|
+
element.dataset.originalDisplay = element.style.display || '';
|
|
69
|
+
element.style.display = 'none';
|
|
70
|
+
|
|
71
|
+
const placeholder = document.createElement('span');
|
|
72
|
+
placeholder.style.cssText =
|
|
73
|
+
'height: 10px; width: 50px; display: inline-block; vertical-align: middle; justify-content: center; background: #fafafa; border-radius: 4px;';
|
|
74
|
+
placeholder.classList.add('math-placeholder');
|
|
75
|
+
element.parentNode?.insertBefore(placeholder, element);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const removePlaceholdersAndRestoreDisplay = () => {
|
|
80
|
+
document.querySelectorAll('.math-placeholder').forEach((placeholder) => {
|
|
81
|
+
const targetElement = placeholder.nextElementSibling;
|
|
82
|
+
|
|
83
|
+
if (targetElement && targetElement.dataset?.originalDisplay !== undefined) {
|
|
84
|
+
targetElement.style.display = targetElement.dataset.originalDisplay;
|
|
85
|
+
delete targetElement.dataset.originalDisplay;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
placeholder.remove();
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
const removeExcessMjxContainers = (content) => {
|
|
92
|
+
const elements = content.querySelectorAll('[data-latex][data-math-handled="true"]');
|
|
93
|
+
|
|
94
|
+
elements.forEach((element) => {
|
|
95
|
+
const mjxContainers = element.querySelectorAll('mjx-container');
|
|
96
|
+
|
|
97
|
+
// Check if there are more than one mjx-container children.
|
|
98
|
+
if (mjxContainers.length > 1) {
|
|
99
|
+
for (let i = 1; i < mjxContainers.length; i++) {
|
|
100
|
+
mjxContainers[i].parentNode.removeChild(mjxContainers[i]);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const renderContentsWithMathJax = (el) => {
|
|
107
|
+
// el sometimes is an array
|
|
108
|
+
// renderMath is used like that in pie-print-support and pie-element-extensions
|
|
109
|
+
// there seems to be no reason for that, however, it's better to handle the case here
|
|
110
|
+
if (el instanceof Array) {
|
|
111
|
+
el.forEach((elNode) => renderContentWithMathJax(elNode));
|
|
112
|
+
} else {
|
|
113
|
+
renderContentWithMathJax(el);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const renderContentWithMathJax = (executeOn) => {
|
|
118
|
+
executeOn = executeOn || document.body;
|
|
119
|
+
|
|
120
|
+
// this happens for charting - mark-label; we receive a ref which is not yet ready ( el = { current: null })
|
|
121
|
+
// we have to fix this in charting
|
|
122
|
+
if (!(executeOn instanceof HTMLElement)) return;
|
|
123
|
+
|
|
124
|
+
fixMathElements(executeOn);
|
|
125
|
+
adjustMathMLStyle(executeOn);
|
|
126
|
+
|
|
127
|
+
const mathJaxInstance = getGlobal().instance;
|
|
128
|
+
|
|
129
|
+
if (mathJaxInstance) {
|
|
130
|
+
// Reset and clear typesetting before processing the new content
|
|
131
|
+
// Reset the tex labels (and automatic equation number).
|
|
132
|
+
|
|
133
|
+
mathJaxInstance.texReset();
|
|
134
|
+
|
|
135
|
+
// Reset the typesetting system (font caches, etc.)
|
|
136
|
+
mathJaxInstance.typesetClear();
|
|
137
|
+
|
|
138
|
+
// Use typesetPromise for asynchronous typesetting
|
|
139
|
+
// Using MathJax.typesetPromise() for asynchronous typesetting to handle situations where additional code needs to be loaded (e.g., for certain TeX commands or characters).
|
|
140
|
+
// This ensures typesetting waits for any needed resources to load and complete processing, unlike the synchronous MathJax.typeset() which can't handle such dynamic loading.
|
|
141
|
+
mathJaxInstance
|
|
142
|
+
.typesetPromise([executeOn])
|
|
143
|
+
.then(() => {
|
|
144
|
+
try {
|
|
145
|
+
removePlaceholdersAndRestoreDisplay();
|
|
146
|
+
removeExcessMjxContainers(executeOn);
|
|
147
|
+
|
|
148
|
+
const updatedDocument = mathJaxInstance.startup.document;
|
|
149
|
+
const list = updatedDocument.math.list;
|
|
150
|
+
|
|
151
|
+
for (let item = list.next; typeof item.data !== 'symbol'; item = item.next) {
|
|
152
|
+
const mathMl = toMMl(item.data.root);
|
|
153
|
+
const parsedMathMl = mathMl.replaceAll('\n', '');
|
|
154
|
+
|
|
155
|
+
item.data.typesetRoot.setAttribute('data-mathml', parsedMathMl);
|
|
156
|
+
item.data.typesetRoot.setAttribute('tabindex', '-1');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// If the original input was a string, return the parsed MathML
|
|
160
|
+
} catch (e) {
|
|
161
|
+
console.error('Error post-processing MathJax typesetting:', e.toString());
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Clearing the document if needed
|
|
165
|
+
mathJaxInstance.startup.document.clear();
|
|
166
|
+
})
|
|
167
|
+
.catch((error) => {
|
|
168
|
+
// If there was an internal error, put the message into the output instead
|
|
169
|
+
|
|
170
|
+
console.error('Error in typesetting with MathJax:', error);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const convertMathJax2ToMathJax3 = () => {
|
|
176
|
+
// Make MathJax v2 compatible with v3
|
|
177
|
+
// https://docs.mathjax.org/en/v3.2-latest/upgrading/v2.html#version-2-compatibility-example
|
|
178
|
+
// Replace the require command map with a new one that checks for
|
|
179
|
+
// renamed extensions and converts them to the new names.
|
|
180
|
+
const CommandMap = MathJax._.input.tex.SymbolMap.CommandMap;
|
|
181
|
+
const requireMap = MathJax.config.startup.requireMap;
|
|
182
|
+
const RequireLoad = MathJax._.input.tex.require.RequireConfiguration.RequireLoad;
|
|
183
|
+
const RequireMethods = {
|
|
184
|
+
Require: function(parser, name) {
|
|
185
|
+
let required = parser.GetArgument(name);
|
|
186
|
+
if (required.match(/[^_a-zA-Z0-9]/) || required === '') {
|
|
187
|
+
throw new TexError('BadPackageName', 'Argument for %1 is not a valid package name', name);
|
|
188
|
+
}
|
|
189
|
+
if (requireMap.hasOwnProperty(required)) {
|
|
190
|
+
required = requireMap[required];
|
|
191
|
+
}
|
|
192
|
+
RequireLoad(parser, required);
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
new CommandMap('require', { require: 'Require' }, RequireMethods);
|
|
197
|
+
|
|
198
|
+
//
|
|
199
|
+
// Add a replacement for MathJax.Callback command
|
|
200
|
+
//
|
|
201
|
+
MathJax.Callback = function(args) {
|
|
202
|
+
if (Array.isArray(args)) {
|
|
203
|
+
if (args.length === 1 && typeof args[0] === 'function') {
|
|
204
|
+
return args[0];
|
|
205
|
+
} else if (typeof args[0] === 'string' && args[1] instanceof Object && typeof args[1][args[0]] === 'function') {
|
|
206
|
+
return Function.bind.apply(args[1][args[0]], args.slice(1));
|
|
207
|
+
} else if (typeof args[0] === 'function') {
|
|
208
|
+
return Function.bind.apply(args[0], [window].concat(args.slice(1)));
|
|
209
|
+
} else if (typeof args[1] === 'function') {
|
|
210
|
+
return Function.bind.apply(args[1], [args[0]].concat(args.slice(2)));
|
|
211
|
+
}
|
|
212
|
+
} else if (typeof args === 'function') {
|
|
213
|
+
return args;
|
|
214
|
+
}
|
|
215
|
+
throw Error("Can't make callback from given data");
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
//
|
|
219
|
+
// Add a replacement for MathJax.Hub commands
|
|
220
|
+
//
|
|
221
|
+
MathJax.Hub = {
|
|
222
|
+
Queue: function() {
|
|
223
|
+
for (let i = 0, m = arguments.length; i < m; i++) {
|
|
224
|
+
const fn = MathJax.Callback(arguments[i]);
|
|
225
|
+
MathJax.startup.promise = MathJax.startup.promise.then(fn);
|
|
226
|
+
}
|
|
227
|
+
return MathJax.startup.promise;
|
|
228
|
+
},
|
|
229
|
+
Typeset: function(elements, callback) {
|
|
230
|
+
let promise = MathJax.typesetPromise(elements);
|
|
231
|
+
|
|
232
|
+
if (callback) {
|
|
233
|
+
promise = promise.then(callback);
|
|
234
|
+
}
|
|
235
|
+
return promise;
|
|
236
|
+
},
|
|
237
|
+
Register: {
|
|
238
|
+
MessageHook: function() {
|
|
239
|
+
console.log('MessageHooks are not supported in version 3');
|
|
240
|
+
},
|
|
241
|
+
StartupHook: function() {
|
|
242
|
+
console.log('StartupHooks are not supported in version 3');
|
|
243
|
+
},
|
|
244
|
+
LoadHook: function() {
|
|
245
|
+
console.log('LoadHooks are not supported in version 3');
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
Config: function() {
|
|
249
|
+
console.log('MathJax configurations should be converted for version 3');
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
};
|
|
253
|
+
export const initializeMathJax = (callback) => {
|
|
254
|
+
if (window.mathjaxLoadedP) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const PreviousMathJaxIsUsed =
|
|
259
|
+
window.MathJax &&
|
|
260
|
+
window.MathJax.version &&
|
|
261
|
+
window.MathJax.version !== MathJaxVersion &&
|
|
262
|
+
window.MathJax.version[0] === '2';
|
|
263
|
+
|
|
264
|
+
const texConfig = {
|
|
265
|
+
macros: {
|
|
266
|
+
parallelogram: '\\lower.2em{\\Huge\\unicode{x25B1}}',
|
|
267
|
+
overarc: '\\overparen',
|
|
268
|
+
napprox: '\\not\\approx',
|
|
269
|
+
longdiv: '\\enclose{longdiv}',
|
|
270
|
+
},
|
|
271
|
+
displayMath: [
|
|
272
|
+
['$$', '$$'],
|
|
273
|
+
['\\[', '\\]'],
|
|
274
|
+
],
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
if (PreviousMathJaxIsUsed) {
|
|
278
|
+
texConfig.autoload = {
|
|
279
|
+
color: [], // don't autoload the color extension
|
|
280
|
+
colorv2: ['color'], // do autoload the colorv2 extension
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Create a new promise that resolves when MathJax is ready
|
|
285
|
+
window.mathjaxLoadedP = new Promise((resolve) => {
|
|
286
|
+
// Set up the MathJax configuration
|
|
287
|
+
window.MathJax = {
|
|
288
|
+
startup: {
|
|
289
|
+
//
|
|
290
|
+
// Mapping of old extension names to new ones
|
|
291
|
+
//
|
|
292
|
+
requireMap: PreviousMathJaxIsUsed
|
|
293
|
+
? {
|
|
294
|
+
AMSmath: 'ams',
|
|
295
|
+
AMSsymbols: 'ams',
|
|
296
|
+
AMScd: 'amscd',
|
|
297
|
+
HTML: 'html',
|
|
298
|
+
noErrors: 'noerrors',
|
|
299
|
+
noUndefined: 'noundefined',
|
|
300
|
+
}
|
|
301
|
+
: {},
|
|
302
|
+
typeset: false,
|
|
303
|
+
ready: () => {
|
|
304
|
+
if (PreviousMathJaxIsUsed) {
|
|
305
|
+
convertMathJax2ToMathJax3();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const { mathjax } = MathJax._.mathjax;
|
|
309
|
+
const { STATE } = MathJax._.core.MathItem;
|
|
310
|
+
const { Menu } = MathJax._.ui.menu.Menu;
|
|
311
|
+
const rerender = Menu.prototype.rerender;
|
|
312
|
+
Menu.prototype.rerender = function(start = STATE.TYPESET) {
|
|
313
|
+
mathjax.handleRetriesFor(() => rerender.call(this, start));
|
|
314
|
+
};
|
|
315
|
+
MathJax.startup.defaultReady();
|
|
316
|
+
// Set the MathJax instance in the global object
|
|
317
|
+
|
|
318
|
+
const globalObj = getGlobal();
|
|
319
|
+
globalObj.instance = MathJax;
|
|
320
|
+
|
|
321
|
+
window.mathjaxLoadedComplete = true;
|
|
322
|
+
console.log('MathJax has initialised!', new Date().toString());
|
|
323
|
+
|
|
324
|
+
// in this file, initializeMathJax is called with a callback that has to be executed when MathJax was loaded
|
|
325
|
+
if (callback) {
|
|
326
|
+
callback();
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// but previous versions of math-rendering-accessible they're expecting window.mathjaxLoadedP to be a Promise, so we also keep the
|
|
330
|
+
// resolve here;
|
|
331
|
+
resolve();
|
|
332
|
+
},
|
|
333
|
+
},
|
|
334
|
+
loader: {
|
|
335
|
+
load: ['input/mml'],
|
|
336
|
+
// I just added preLoad: () => {} to prevent the console error: "MathJax.loader.preLoad is not a function",
|
|
337
|
+
// which is being called because in math-rendering-accessible/render-math we're having this line:
|
|
338
|
+
// import * as mr from '@pie-lib/math-rendering';
|
|
339
|
+
// which takes us to: import { AllPackages } from 'mathjax-full/js/input/tex/AllPackages';
|
|
340
|
+
// which tries to call MathJax.loader.preLoad.
|
|
341
|
+
// Understand that AllPackages is NOT needed in math-rendering-accessible, so it is not a problem if we hardcode this function.
|
|
342
|
+
// The better solution would be for math-rendering-accessible to import math-rendering only IF needed,
|
|
343
|
+
// but that's actually complicated and could cause other issues.
|
|
344
|
+
preLoad: () => {},
|
|
345
|
+
// function to call if a component fails to load
|
|
346
|
+
// eslint-disable-next-line no-console
|
|
347
|
+
failed: (error) => console.log(`MathJax(${error.package || '?'}): ${error.message}`),
|
|
348
|
+
},
|
|
349
|
+
tex: texConfig,
|
|
350
|
+
chtml: {
|
|
351
|
+
fontURL: 'https://unpkg.com/mathjax-full@3.2.2/ts/output/chtml/fonts/tex-woff-v2',
|
|
352
|
+
displayAlign: 'center',
|
|
353
|
+
},
|
|
354
|
+
customKey: '@pie-lib/math-rendering-accessible@1',
|
|
355
|
+
options: {
|
|
356
|
+
enableEnrichment: true,
|
|
357
|
+
sre: {
|
|
358
|
+
speech: 'deep',
|
|
359
|
+
},
|
|
360
|
+
menuOptions: {
|
|
361
|
+
settings: {
|
|
362
|
+
assistiveMml: true,
|
|
363
|
+
collapsible: false,
|
|
364
|
+
explorer: false,
|
|
365
|
+
},
|
|
366
|
+
},
|
|
367
|
+
},
|
|
368
|
+
};
|
|
369
|
+
// Load the MathJax script
|
|
370
|
+
const script = document.createElement('script');
|
|
371
|
+
script.type = 'text/javascript';
|
|
372
|
+
script.src = `https://cdn.jsdelivr.net/npm/mathjax@${MathJaxVersion}/es5/tex-chtml-full.js`;
|
|
373
|
+
script.async = true;
|
|
374
|
+
document.head.appendChild(script);
|
|
375
|
+
|
|
376
|
+
// at this time of the execution, there's no document.body; setTimeout does the trick
|
|
377
|
+
setTimeout(() => {
|
|
378
|
+
if (!window.mathjaxLoadedComplete) {
|
|
379
|
+
const mathElements = document?.body?.querySelectorAll('[data-latex]');
|
|
380
|
+
(mathElements || []).forEach(createPlaceholder);
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
const renderMath = (el, renderOpts) => {
|
|
387
|
+
const usedForMmlOutput = typeof el === 'string';
|
|
388
|
+
let executeOn = document.body;
|
|
389
|
+
|
|
390
|
+
if (!usedForMmlOutput) {
|
|
391
|
+
// If math-rendering was not available, then:
|
|
392
|
+
// If window.mathjaxLoadedComplete, it means that we initialised MathJax using the function from this file,
|
|
393
|
+
// and it means MathJax is successfully completed, so we can already use it
|
|
394
|
+
if (window.mathjaxLoadedComplete) {
|
|
395
|
+
renderContentsWithMathJax(el);
|
|
396
|
+
} else if (window.mathjaxLoadedP) {
|
|
397
|
+
// However, because there is a small chance that MathJax was initialised by a previous version of math-rendering-accessible,
|
|
398
|
+
// we need to keep the old handling method, which means adding the .then.catch on window.mathjaxLoadedP Promise.
|
|
399
|
+
// We still want to set window.mathjaxLoadedComplete, to prevent adding .then.catch after the first initialization
|
|
400
|
+
// (again, in case MathJax was initialised by a previous math-rendering-accessible version)
|
|
401
|
+
window.mathjaxLoadedP
|
|
402
|
+
.then(() => {
|
|
403
|
+
window.mathjaxLoadedComplete = true;
|
|
404
|
+
renderContentsWithMathJax(el);
|
|
405
|
+
})
|
|
406
|
+
.catch((error) => console.error('Error in initializing MathJax:', error));
|
|
407
|
+
}
|
|
408
|
+
} else {
|
|
409
|
+
// Here we're handling the case when renderMath is being called for mmlOutput
|
|
410
|
+
if (window.MathJax && window.mathjaxLoadedP) {
|
|
411
|
+
const div = document.createElement('div');
|
|
412
|
+
|
|
413
|
+
div.innerHTML = el;
|
|
414
|
+
executeOn = div;
|
|
415
|
+
|
|
416
|
+
try {
|
|
417
|
+
MathJax.texReset();
|
|
418
|
+
MathJax.typesetClear();
|
|
419
|
+
window.MathJax.typeset([executeOn]);
|
|
420
|
+
const updatedDocument = window.MathJax.startup.document;
|
|
421
|
+
const list = updatedDocument.math.list;
|
|
422
|
+
const item = list.next;
|
|
423
|
+
const mathMl = toMMl(item.data.root);
|
|
424
|
+
|
|
425
|
+
const parsedMathMl = mathMl.replaceAll('\n', '');
|
|
426
|
+
|
|
427
|
+
return parsedMathMl;
|
|
428
|
+
} catch (error) {
|
|
429
|
+
console.error('Error rendering math:', error.message);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return el;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
return el;
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
// this function calls itself
|
|
440
|
+
(function() {
|
|
441
|
+
initializeMathJax(renderContentWithMathJax);
|
|
442
|
+
|
|
443
|
+
window[mathRenderingKEY] = {
|
|
444
|
+
instance: {
|
|
445
|
+
Typeset: renderMath,
|
|
446
|
+
},
|
|
447
|
+
};
|
|
448
|
+
})();
|
|
449
|
+
|
|
450
|
+
export default renderMath;
|