@pie-lib/math-rendering-accessible 3.4.0-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.
@@ -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
@@ -0,0 +1,5 @@
1
+ import renderMath from './render-math';
2
+ import mmlToLatex from './mml-to-latex';
3
+ import { wrapMath, unWrapMath } from './normalization';
4
+
5
+ export { renderMath, wrapMath, unWrapMath, mmlToLatex };
File without changes
@@ -0,0 +1,2 @@
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
+ });
@@ -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
+ };