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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +0,0 @@
1
- import { MathMLToLaTeX } from '@pie-framework/mathml-to-latex';
2
-
3
- export default (mathml) => MathMLToLaTeX.convert(mathml);
@@ -1,106 +0,0 @@
1
- import { CHTMLmstack, getStackData, Line, Row } 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
- expect({ ...d }).toEqual({ ...expected });
61
- });
62
- });
63
-
64
- describe('Row', () => {
65
- describe('pad', () => {
66
- it.each`
67
- cols | count | expected
68
- ${[]} | ${0} | ${[]}
69
- ${[1]} | ${1} | ${[1]}
70
- ${[1]} | ${2} | ${['__pad__', 1]}
71
- ${[1]} | ${3} | ${['__pad__', '__pad__', 1]}
72
- `('pads to the right', ({ cols, count, expected }) => {
73
- const r = new Row(cols);
74
- const p = r.pad(count, 'right');
75
- expect(p).toEqual(expected);
76
- });
77
- });
78
- });
79
-
80
- describe.each`
81
- label | tree
82
- ${'one row'} | ${[msrow(mn('1'))]}
83
- ${'implicit one row'} | ${[mn('1')]}
84
- ${'two rows'} | ${[msrow(mn('1')), msrow(mn('2'))]}
85
- ${'two rows with operator'} | ${[msrow(mn('1')), msrow(mco('+'), mn('2'))]}
86
- `('chtml', ({ label, tree }) => {
87
- let html;
88
-
89
- beforeEach(() => {
90
- const chtml = new CHTMLmstack({}, {});
91
- const dom = new JSDOM(`<!DOCTYPE html><body></body>`);
92
- chtml.standardCHTMLnode = (parent) => parent;
93
- chtml.ce = dom.window.document.createElement.bind(dom.window.document);
94
- chtml.childNodes = tree;
95
- chtml.toCHTML(dom.window.document.body);
96
- html = dom.window.document.body.innerHTML;
97
- });
98
-
99
- it(label, () => {
100
- // Verify HTML is generated (not empty)
101
- expect(html).toBeTruthy();
102
- expect(typeof html).toBe('string');
103
- // Verify it contains expected structure elements
104
- expect(html.length).toBeGreaterThan(0);
105
- });
106
- });
@@ -1,220 +0,0 @@
1
- import { CHTMLWrapper } from 'mathjax-full/js/output/chtml/Wrapper';
2
- import { compact, first, flatten, tail, times } from 'lodash-es';
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
- };
@@ -1,13 +0,0 @@
1
- import { CHTMLmstack } from './chtml';
2
- import { MmlMsline, MmlMsrow, MmlMstack, MmlNone } 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
- };
package/src/mstack/mml.js DELETED
@@ -1,24 +0,0 @@
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
- }
@@ -1,69 +0,0 @@
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
- };