@pie-lib/math-rendering 3.2.1 → 3.3.0-beta.1
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/lib/mml-to-latex.js +2 -4
- package/lib/mml-to-latex.js.map +1 -1
- package/lib/render-math.js +156 -76
- package/lib/render-math.js.map +1 -1
- 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/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
|
@@ -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,155 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { mount } from 'enzyme';
|
|
3
|
+
import renderMath, { fixMathElement } from '../render-math';
|
|
4
|
+
import _ from 'lodash';
|
|
5
|
+
|
|
6
|
+
jest.mock(
|
|
7
|
+
'mathjax-full/js/mathjax',
|
|
8
|
+
() => ({
|
|
9
|
+
mathjax: {
|
|
10
|
+
document: jest.fn().mockReturnThis(),
|
|
11
|
+
findMath: jest.fn().mockReturnThis(),
|
|
12
|
+
compile: jest.fn().mockReturnThis(),
|
|
13
|
+
getMetrics: jest.fn().mockReturnThis(),
|
|
14
|
+
typeset: jest.fn().mockReturnThis(),
|
|
15
|
+
updateDocument: jest.fn().mockReturnThis(),
|
|
16
|
+
clear: jest.fn().mockReturnThis(),
|
|
17
|
+
handlers: {
|
|
18
|
+
handlesDocument: jest.fn().mockReturnThis(),
|
|
19
|
+
},
|
|
20
|
+
handleRetriesFor: jest.fn().mockImplementation((callback) => callback()),
|
|
21
|
+
},
|
|
22
|
+
}),
|
|
23
|
+
{
|
|
24
|
+
virtual: false,
|
|
25
|
+
},
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
jest.mock('mathjax-full/js/input/mathml', () => {
|
|
29
|
+
const mock = jest.fn().mockReturnThis();
|
|
30
|
+
mock.setMmlFactory = jest.fn();
|
|
31
|
+
return {
|
|
32
|
+
MathML: () => mock,
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
jest.mock('mathjax-full/js/input/tex', () => ({
|
|
37
|
+
TeX: jest.fn(),
|
|
38
|
+
}));
|
|
39
|
+
jest.mock('mathjax-full/js/core/MmlTree/MmlFactory', () => {
|
|
40
|
+
const instance = {
|
|
41
|
+
setMmlFactory: jest.fn(),
|
|
42
|
+
defaultNodes: {},
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
MmlFactory: () => instance,
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const mockMathInstance = {
|
|
50
|
+
document: jest.fn().mockReturnThis(),
|
|
51
|
+
findMath: jest.fn().mockReturnThis(),
|
|
52
|
+
compile: jest.fn().mockReturnThis(),
|
|
53
|
+
enrich: jest.fn().mockReturnThis(),
|
|
54
|
+
addMenu: jest.fn().mockReturnThis(),
|
|
55
|
+
attachSpeech: jest.fn().mockReturnThis(),
|
|
56
|
+
assistiveMml: jest.fn().mockReturnThis(),
|
|
57
|
+
getMetrics: jest.fn().mockReturnThis(),
|
|
58
|
+
typeset: jest.fn().mockReturnThis(),
|
|
59
|
+
updateDocument: jest.fn().mockReturnValue({
|
|
60
|
+
math: {
|
|
61
|
+
list: undefined,
|
|
62
|
+
},
|
|
63
|
+
clear: jest.fn().mockReturnThis(),
|
|
64
|
+
}),
|
|
65
|
+
clear: jest.fn().mockReturnThis(),
|
|
66
|
+
handlers: {
|
|
67
|
+
handlesDocument: jest.fn().mockReturnThis(),
|
|
68
|
+
},
|
|
69
|
+
handleRetriesFor: jest.fn().mockImplementation((callback) => callback()),
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const mockHtml = {
|
|
73
|
+
findMath: jest.fn().mockReturnValue(mockMathInstance),
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const mockEnrichHandlerInstance = {
|
|
77
|
+
create: jest.fn().mockImplementation(() => mockHtml),
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
jest.mock('mathjax-full/js/a11y/semantic-enrich', () => {
|
|
81
|
+
return {
|
|
82
|
+
EnrichHandler: () => mockEnrichHandlerInstance,
|
|
83
|
+
};
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
jest.mock('mathjax-full/js/a11y/assistive-mml', () => {
|
|
87
|
+
return {
|
|
88
|
+
AssistiveMmlHandler: () => {},
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
jest.mock('mathjax-full/js/ui/menu/MenuHandler', () => {
|
|
93
|
+
return {
|
|
94
|
+
MenuHandler: () => {},
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
jest.mock('mathjax-full/js/output/chtml', () => ({
|
|
99
|
+
CHTML: jest.fn(),
|
|
100
|
+
}));
|
|
101
|
+
|
|
102
|
+
jest.mock('mathjax-full/js/adaptors/browserAdaptor', () => ({
|
|
103
|
+
browserAdaptor: jest.fn(),
|
|
104
|
+
}));
|
|
105
|
+
|
|
106
|
+
jest.mock('mathjax-full/js/handlers/html', () => ({
|
|
107
|
+
RegisterHTMLHandler: jest.fn(),
|
|
108
|
+
}));
|
|
109
|
+
|
|
110
|
+
jest.mock('mathjax-full/js/core/MmlTree/SerializedMmlVisitor', () => ({
|
|
111
|
+
SerializedMmlVisitor: jest.fn(),
|
|
112
|
+
}));
|
|
113
|
+
|
|
114
|
+
describe('render-math', () => {
|
|
115
|
+
it('calls classFactory.create once', () => {
|
|
116
|
+
const div = document.createElement('div');
|
|
117
|
+
|
|
118
|
+
_.times(10).forEach((i) => renderMath(div));
|
|
119
|
+
|
|
120
|
+
expect(mockEnrichHandlerInstance.create).toHaveBeenCalledTimes(1);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('calls MathJax render', () => {
|
|
124
|
+
const div = document.createElement('div');
|
|
125
|
+
|
|
126
|
+
renderMath(div);
|
|
127
|
+
expect(mockEnrichHandlerInstance.create).toHaveBeenCalledTimes(1);
|
|
128
|
+
expect(mockHtml.findMath).toHaveBeenCalledWith({ elements: [div] });
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('call render math for an array of elements', () => {
|
|
132
|
+
const divOne = document.createElement('div');
|
|
133
|
+
const divTwo = document.createElement('div');
|
|
134
|
+
|
|
135
|
+
renderMath([divOne, divTwo]);
|
|
136
|
+
|
|
137
|
+
expect(mockEnrichHandlerInstance.create).toHaveBeenCalledTimes(1);
|
|
138
|
+
expect(mockHtml.findMath).toHaveBeenCalledWith({
|
|
139
|
+
elements: [divOne, divTwo],
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('wraps the math containing element the right way', () => {
|
|
144
|
+
const wrapper = mount(
|
|
145
|
+
<div>
|
|
146
|
+
<span data-latex="">{'420\\text{ cm}=4.2\\text{ meters}'}</span>
|
|
147
|
+
</div>,
|
|
148
|
+
);
|
|
149
|
+
const spanElem = wrapper.instance();
|
|
150
|
+
|
|
151
|
+
fixMathElement(spanElem);
|
|
152
|
+
|
|
153
|
+
expect(spanElem.textContent).toEqual('\\(420\\text{ cm}=4.2\\text{ meters}\\)');
|
|
154
|
+
});
|
|
155
|
+
});
|
package/src/mml-to-latex.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
export default (mathml) =>
|
|
1
|
+
import { MathMLToLaTeX } from '@pie-framework/mathml-to-latex';
|
|
2
|
+
export default (mathml) => MathMLToLaTeX.convert(mathml);
|
|
@@ -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
|
+
});
|
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
|
}
|