@pie-lib/math-rendering 3.2.2-next.4 → 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.md CHANGED
@@ -3,104 +3,84 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
- ## [3.2.1](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@3.2.0...@pie-lib/math-rendering@3.2.1) (2023-10-01)
6
+ ## [3.3.1-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@3.2.1...@pie-lib/math-rendering@3.3.1-beta.0) (2025-07-20)
7
7
 
8
+ **Note:** Version bump only for package @pie-lib/math-rendering
8
9
 
9
- ### Bug Fixes
10
10
 
11
- * revert mathml-to-latex version as it seems to break math rendering ([46e841c](https://github.com/pie-framework/pie-lib/commit/46e841c00c2a04ff0e3442625870b3b457571a8e))
12
11
 
13
12
 
14
13
 
14
+ # [3.3.0-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@3.2.1...@pie-lib/math-rendering@3.3.0-beta.0) (2025-07-20)
15
15
 
16
+ **Note:** Version bump only for package @pie-lib/math-rendering
16
17
 
17
- # [3.2.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@3.1.2...@pie-lib/math-rendering@3.2.0) (2023-09-27)
18
18
 
19
19
 
20
- ### Features
21
-
22
- * update version ([f716545](https://github.com/pie-framework/pie-lib/commit/f716545d526dc856e2160619650787273a993f27))
23
20
 
24
21
 
22
+ # [3.4.0-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@3.2.1...@pie-lib/math-rendering@3.4.0-beta.0) (2025-07-15)
25
23
 
24
+ **Note:** Version bump only for package @pie-lib/math-rendering
26
25
 
26
+ # [3.3.0-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@3.2.1...@pie-lib/math-rendering@3.3.0-beta.0) (2025-07-15)
27
27
 
28
- ## [3.1.2](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@3.1.1...@pie-lib/math-rendering@3.1.2) (2023-09-27)
28
+ **Note:** Version bump only for package @pie-lib/math-rendering
29
29
 
30
+ ## [3.2.1](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@3.2.0...@pie-lib/math-rendering@3.2.1) (2023-10-01)
30
31
 
31
32
  ### Bug Fixes
32
33
 
33
- * force publish ([489a242](https://github.com/pie-framework/pie-lib/commit/489a2421c109fb202b778680f1b57b942e49c86a))
34
-
35
-
34
+ - revert mathml-to-latex version as it seems to break math rendering ([46e841c](https://github.com/pie-framework/pie-lib/commit/46e841c00c2a04ff0e3442625870b3b457571a8e))
36
35
 
36
+ # [3.2.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@3.1.2...@pie-lib/math-rendering@3.2.0) (2023-09-27)
37
37
 
38
+ ### Features
38
39
 
39
- ## [3.1.1](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@3.1.0...@pie-lib/math-rendering@3.1.1) (2023-09-25)
40
+ - update version ([f716545](https://github.com/pie-framework/pie-lib/commit/f716545d526dc856e2160619650787273a993f27))
40
41
 
42
+ ## [3.1.2](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@3.1.1...@pie-lib/math-rendering@3.1.2) (2023-09-27)
41
43
 
42
44
  ### Bug Fixes
43
45
 
44
- * update mathml-to-latex version PD-3182, PD-3184 ([c019296](https://github.com/pie-framework/pie-lib/commit/c0192965a3a045e13993a3301dfc35e7e314a592))
45
-
46
+ - force publish ([489a242](https://github.com/pie-framework/pie-lib/commit/489a2421c109fb202b778680f1b57b942e49c86a))
46
47
 
48
+ ## [3.1.1](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@3.1.0...@pie-lib/math-rendering@3.1.1) (2023-09-25)
47
49
 
50
+ ### Bug Fixes
48
51
 
52
+ - update mathml-to-latex version PD-3182, PD-3184 ([c019296](https://github.com/pie-framework/pie-lib/commit/c0192965a3a045e13993a3301dfc35e7e314a592))
49
53
 
50
54
  # [3.1.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@3.0.0...@pie-lib/math-rendering@3.1.0) (2023-09-20)
51
55
 
52
-
53
56
  ### Features
54
57
 
55
- * use forked mathml-to-latex library, as it contains fixes PD-3011 ([8949ac2](https://github.com/pie-framework/pie-lib/commit/8949ac2ddf6664bf437a35dbe21555f18ac77426))
56
-
57
-
58
-
59
-
58
+ - use forked mathml-to-latex library, as it contains fixes PD-3011 ([8949ac2](https://github.com/pie-framework/pie-lib/commit/8949ac2ddf6664bf437a35dbe21555f18ac77426))
60
59
 
61
60
  # [3.0.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@2.5.18...@pie-lib/math-rendering@3.0.0) (2023-09-20)
62
61
 
63
-
64
62
  ### Bug Fixes
65
63
 
66
- * update mathml-to-latex version ([1d101d2](https://github.com/pie-framework/pie-lib/commit/1d101d22298bd480a5aec638e4c9708d5aa52ce4))
67
-
64
+ - update mathml-to-latex version ([1d101d2](https://github.com/pie-framework/pie-lib/commit/1d101d22298bd480a5aec638e4c9708d5aa52ce4))
68
65
 
69
66
  ### BREAKING CHANGES
70
67
 
71
- * updated mathml-to-latex version.
72
-
73
-
74
-
75
-
68
+ - updated mathml-to-latex version.
76
69
 
77
70
  ## [2.5.18](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@2.5.17...@pie-lib/math-rendering@2.5.18) (2023-09-14)
78
71
 
79
-
80
72
  ### Bug Fixes
81
73
 
82
- * force v1.2.0 of mathml-to-latex, as v1.3.0 contains a braking change ([4792ed9](https://github.com/pie-framework/pie-lib/commit/4792ed965608ee5ff4c872e69dee9b3b59261cf0))
83
-
84
-
85
-
86
-
74
+ - force v1.2.0 of mathml-to-latex, as v1.3.0 contains a braking change ([4792ed9](https://github.com/pie-framework/pie-lib/commit/4792ed965608ee5ff4c872e69dee9b3b59261cf0))
87
75
 
88
76
  ## [2.5.17](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@2.5.16...@pie-lib/math-rendering@2.5.17) (2023-06-24)
89
77
 
90
78
  **Note:** Version bump only for package @pie-lib/math-rendering
91
79
 
92
-
93
-
94
-
95
-
96
80
  ## [2.5.16](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@2.5.15...@pie-lib/math-rendering@2.5.16) (2023-06-13)
97
81
 
98
82
  **Note:** Version bump only for package @pie-lib/math-rendering
99
83
 
100
-
101
-
102
-
103
-
104
84
  ## [2.5.15](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-rendering@2.5.14...@pie-lib/math-rendering@2.5.15) (2023-06-12)
105
85
 
106
86
  ### Bug Fixes
@@ -0,0 +1 @@
1
+ []
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-lib/math-rendering",
3
- "version": "3.2.2-next.4+23991a09",
3
+ "version": "3.3.1-beta.0",
4
4
  "description": "math rendering utilities",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -10,11 +10,13 @@
10
10
  "author": "pie-framework developers",
11
11
  "license": "ISC",
12
12
  "dependencies": {
13
+ "@pie-framework/mathml-to-latex": "1.4.4",
13
14
  "debug": "^4.1.1",
14
15
  "lodash": "^4.17.11",
15
16
  "mathjax-full": "3.2.2",
16
- "mathml-to-latex": "1.2.0"
17
+ "react": "^16.8.1",
18
+ "slate": "^0.36.2"
17
19
  },
18
- "gitHead": "23991a09b77492c24dcb5235049667cdcc211d80",
20
+ "gitHead": "e2aa3ddac60f49bcb8c2562370f496323642f453",
19
21
  "scripts": {}
20
22
  }
@@ -0,0 +1,14 @@
1
+ import React from 'react';
2
+ import mmlToLatex from '../mml-to-latex';
3
+
4
+ describe('mmlToLatex', () => {
5
+ it('should work', () => {
6
+ const mml =
7
+ '<math xmlns="http://www.w3.org/1998/Math/MathML"> <mn>2</mn> <mi>x</mi> <mtext>&#xA0;</mtext> <mo>&#x2264;</mo> <mn>4</mn> <mi>y</mi> <mtext>&#xA0;</mtext> <mo>+</mo> <mtext>&#xA0;</mtext> <mn>8</mn> <msqrt> <mi>h</mi> </msqrt></math>';
8
+ // todo revisit this
9
+ // const latex = '2x\\text{ }\\leq4y\\text{ }+\\text{ }8\\sqrt{h}';
10
+ const latex = '2 x \\textrm{ } \\leq 4 y \\textrm{ } + \\textrm{ } 8 \\sqrt{h}';
11
+
12
+ expect(mmlToLatex(mml)).toEqual(latex);
13
+ });
14
+ });
@@ -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
+ });
@@ -1,2 +1,2 @@
1
- import Mathml2latex from 'mathml-to-latex';
2
- export default (mathml) => Mathml2latex.convert(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
+ });