@wordpress/latex-to-mathml 1.23.1-next.v.202609171837.0 → 1.25.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
@@ -2,6 +2,14 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 1.25.0 (2026-10-07)
6
+
7
+ ### Bug Fixes
8
+
9
+ - Write `columnalign` on the cells of `aligned`, `cases` and tagged equations, so the alignment is part of the MathML, and strip the classes temml adds for its own stylesheet ([#83164](https://github.com/WordPress/gutenberg/pull/83164)).
10
+
11
+ ## 1.24.0 (2026-09-23)
12
+
5
13
  ## 1.23.0 (2026-09-10)
6
14
 
7
15
  ## 1.22.0 (2026-08-26)
package/build/index.cjs CHANGED
@@ -42,6 +42,21 @@ function latexToMathML(latex, { displayMode = true } = {}) {
42
42
  });
43
43
  const doc = document.implementation.createHTMLDocument("");
44
44
  doc.body.innerHTML = mathML;
45
- return doc.body.querySelector("math")?.innerHTML ?? "";
45
+ const math = doc.body.querySelector("math");
46
+ if (!math) {
47
+ return "";
48
+ }
49
+ for (const cell of math.querySelectorAll(
50
+ "mtd.tml-right, mtd.tml-left"
51
+ )) {
52
+ cell.setAttribute(
53
+ "columnalign",
54
+ cell.classList.contains("tml-right") ? "right" : "left"
55
+ );
56
+ }
57
+ for (const element of math.querySelectorAll("[class]")) {
58
+ element.removeAttribute("class");
59
+ }
60
+ return math.innerHTML;
46
61
  }
47
62
  //# sourceMappingURL=index.cjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts"],
4
- "sourcesContent": ["import temml from 'temml';\n\n/**\n * Options for LaTeX to MathML conversion.\n */\nexport interface LatexToMathMLOptions {\n\t/**\n\t * Whether to render in display mode (block) or inline mode.\n\t * @default true\n\t */\n\tdisplayMode?: boolean;\n}\n\n/**\n * Converts LaTeX math syntax to MathML.\n *\n * @param latex - The LaTeX string to convert.\n * @param options - Conversion options.\n * @param options.displayMode\n * @return The MathML string.\n * @throws Will throw an error if the LaTeX is invalid.\n *\n * @example\n * ```js\n * import latexToMathML from '@wordpress/latex-to-mathml';\n *\n * const mathML = latexToMathML( 'x^2', { displayMode: false } );\n * ```\n */\nexport default function latexToMathML(\n\tlatex: string,\n\t{ displayMode = true }: LatexToMathMLOptions = {}\n): string {\n\tconst mathML = temml.renderToString( latex, {\n\t\tdisplayMode,\n\t\tannotate: true,\n\t\tthrowOnError: true,\n\t} );\n\tconst doc = document.implementation.createHTMLDocument( '' );\n\tdoc.body.innerHTML = mathML;\n\treturn doc.body.querySelector( 'math' )?.innerHTML ?? '';\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkB;AA6BH,SAAR,cACN,OACA,EAAE,cAAc,KAAK,IAA0B,CAAC,GACvC;AACT,QAAM,SAAS,aAAAA,QAAM,eAAgB,OAAO;AAAA,IAC3C;AAAA,IACA,UAAU;AAAA,IACV,cAAc;AAAA,EACf,CAAE;AACF,QAAM,MAAM,SAAS,eAAe,mBAAoB,EAAG;AAC3D,MAAI,KAAK,YAAY;AACrB,SAAO,IAAI,KAAK,cAAe,MAAO,GAAG,aAAa;AACvD;",
4
+ "sourcesContent": ["import temml from 'temml';\n\n/**\n * Options for LaTeX to MathML conversion.\n */\nexport interface LatexToMathMLOptions {\n\t/**\n\t * Whether to render in display mode (block) or inline mode.\n\t * @default true\n\t */\n\tdisplayMode?: boolean;\n}\n\n/**\n * Converts LaTeX math syntax to MathML.\n *\n * @param latex - The LaTeX string to convert.\n * @param options - Conversion options.\n * @param options.displayMode\n * @return The MathML string.\n * @throws Will throw an error if the LaTeX is invalid.\n *\n * @example\n * ```js\n * import latexToMathML from '@wordpress/latex-to-mathml';\n *\n * const mathML = latexToMathML( 'x^2', { displayMode: false } );\n * ```\n */\nexport default function latexToMathML(\n\tlatex: string,\n\t{ displayMode = true }: LatexToMathMLOptions = {}\n): string {\n\tconst mathML = temml.renderToString( latex, {\n\t\tdisplayMode,\n\t\tannotate: true,\n\t\tthrowOnError: true,\n\t} );\n\tconst doc = document.implementation.createHTMLDocument( '' );\n\tdoc.body.innerHTML = mathML;\n\tconst math = doc.body.querySelector( 'math' );\n\tif ( ! math ) {\n\t\treturn '';\n\t}\n\n\t// temml marks the cells of `aligned`, `cases` and tagged equations with\n\t// classes for its own stylesheet. Write the MathML `columnalign`\n\t// attribute on the cell instead, so the alignment is part of the content:\n\t// Firefox and Safari honor it natively, and the polyfill in `style.scss`\n\t// covers Chromium, which implements MathML Core only.\n\tfor ( const cell of math.querySelectorAll(\n\t\t'mtd.tml-right, mtd.tml-left'\n\t) ) {\n\t\tcell.setAttribute(\n\t\t\t'columnalign',\n\t\t\tcell.classList.contains( 'tml-right' ) ? 'right' : 'left'\n\t\t);\n\t}\n\n\t// The remaining classes are hooks for temml's stylesheet, which is not\n\t// used. Drop them so the content is plain MathML and does not depend on\n\t// the class names of a temml version.\n\tfor ( const element of math.querySelectorAll( '[class]' ) ) {\n\t\telement.removeAttribute( 'class' );\n\t}\n\n\treturn math.innerHTML;\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkB;AA6BH,SAAR,cACN,OACA,EAAE,cAAc,KAAK,IAA0B,CAAC,GACvC;AACT,QAAM,SAAS,aAAAA,QAAM,eAAgB,OAAO;AAAA,IAC3C;AAAA,IACA,UAAU;AAAA,IACV,cAAc;AAAA,EACf,CAAE;AACF,QAAM,MAAM,SAAS,eAAe,mBAAoB,EAAG;AAC3D,MAAI,KAAK,YAAY;AACrB,QAAM,OAAO,IAAI,KAAK,cAAe,MAAO;AAC5C,MAAK,CAAE,MAAO;AACb,WAAO;AAAA,EACR;AAOA,aAAY,QAAQ,KAAK;AAAA,IACxB;AAAA,EACD,GAAI;AACH,SAAK;AAAA,MACJ;AAAA,MACA,KAAK,UAAU,SAAU,WAAY,IAAI,UAAU;AAAA,IACpD;AAAA,EACD;AAKA,aAAY,WAAW,KAAK,iBAAkB,SAAU,GAAI;AAC3D,YAAQ,gBAAiB,OAAQ;AAAA,EAClC;AAEA,SAAO,KAAK;AACb;",
6
6
  "names": ["temml"]
7
7
  }
@@ -8,7 +8,22 @@ function latexToMathML(latex, { displayMode = true } = {}) {
8
8
  });
9
9
  const doc = document.implementation.createHTMLDocument("");
10
10
  doc.body.innerHTML = mathML;
11
- return doc.body.querySelector("math")?.innerHTML ?? "";
11
+ const math = doc.body.querySelector("math");
12
+ if (!math) {
13
+ return "";
14
+ }
15
+ for (const cell of math.querySelectorAll(
16
+ "mtd.tml-right, mtd.tml-left"
17
+ )) {
18
+ cell.setAttribute(
19
+ "columnalign",
20
+ cell.classList.contains("tml-right") ? "right" : "left"
21
+ );
22
+ }
23
+ for (const element of math.querySelectorAll("[class]")) {
24
+ element.removeAttribute("class");
25
+ }
26
+ return math.innerHTML;
12
27
  }
13
28
  export {
14
29
  latexToMathML as default
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts"],
4
- "sourcesContent": ["import temml from 'temml';\n\n/**\n * Options for LaTeX to MathML conversion.\n */\nexport interface LatexToMathMLOptions {\n\t/**\n\t * Whether to render in display mode (block) or inline mode.\n\t * @default true\n\t */\n\tdisplayMode?: boolean;\n}\n\n/**\n * Converts LaTeX math syntax to MathML.\n *\n * @param latex - The LaTeX string to convert.\n * @param options - Conversion options.\n * @param options.displayMode\n * @return The MathML string.\n * @throws Will throw an error if the LaTeX is invalid.\n *\n * @example\n * ```js\n * import latexToMathML from '@wordpress/latex-to-mathml';\n *\n * const mathML = latexToMathML( 'x^2', { displayMode: false } );\n * ```\n */\nexport default function latexToMathML(\n\tlatex: string,\n\t{ displayMode = true }: LatexToMathMLOptions = {}\n): string {\n\tconst mathML = temml.renderToString( latex, {\n\t\tdisplayMode,\n\t\tannotate: true,\n\t\tthrowOnError: true,\n\t} );\n\tconst doc = document.implementation.createHTMLDocument( '' );\n\tdoc.body.innerHTML = mathML;\n\treturn doc.body.querySelector( 'math' )?.innerHTML ?? '';\n}\n"],
5
- "mappings": ";AAAA,OAAO,WAAW;AA6BH,SAAR,cACN,OACA,EAAE,cAAc,KAAK,IAA0B,CAAC,GACvC;AACT,QAAM,SAAS,MAAM,eAAgB,OAAO;AAAA,IAC3C;AAAA,IACA,UAAU;AAAA,IACV,cAAc;AAAA,EACf,CAAE;AACF,QAAM,MAAM,SAAS,eAAe,mBAAoB,EAAG;AAC3D,MAAI,KAAK,YAAY;AACrB,SAAO,IAAI,KAAK,cAAe,MAAO,GAAG,aAAa;AACvD;",
4
+ "sourcesContent": ["import temml from 'temml';\n\n/**\n * Options for LaTeX to MathML conversion.\n */\nexport interface LatexToMathMLOptions {\n\t/**\n\t * Whether to render in display mode (block) or inline mode.\n\t * @default true\n\t */\n\tdisplayMode?: boolean;\n}\n\n/**\n * Converts LaTeX math syntax to MathML.\n *\n * @param latex - The LaTeX string to convert.\n * @param options - Conversion options.\n * @param options.displayMode\n * @return The MathML string.\n * @throws Will throw an error if the LaTeX is invalid.\n *\n * @example\n * ```js\n * import latexToMathML from '@wordpress/latex-to-mathml';\n *\n * const mathML = latexToMathML( 'x^2', { displayMode: false } );\n * ```\n */\nexport default function latexToMathML(\n\tlatex: string,\n\t{ displayMode = true }: LatexToMathMLOptions = {}\n): string {\n\tconst mathML = temml.renderToString( latex, {\n\t\tdisplayMode,\n\t\tannotate: true,\n\t\tthrowOnError: true,\n\t} );\n\tconst doc = document.implementation.createHTMLDocument( '' );\n\tdoc.body.innerHTML = mathML;\n\tconst math = doc.body.querySelector( 'math' );\n\tif ( ! math ) {\n\t\treturn '';\n\t}\n\n\t// temml marks the cells of `aligned`, `cases` and tagged equations with\n\t// classes for its own stylesheet. Write the MathML `columnalign`\n\t// attribute on the cell instead, so the alignment is part of the content:\n\t// Firefox and Safari honor it natively, and the polyfill in `style.scss`\n\t// covers Chromium, which implements MathML Core only.\n\tfor ( const cell of math.querySelectorAll(\n\t\t'mtd.tml-right, mtd.tml-left'\n\t) ) {\n\t\tcell.setAttribute(\n\t\t\t'columnalign',\n\t\t\tcell.classList.contains( 'tml-right' ) ? 'right' : 'left'\n\t\t);\n\t}\n\n\t// The remaining classes are hooks for temml's stylesheet, which is not\n\t// used. Drop them so the content is plain MathML and does not depend on\n\t// the class names of a temml version.\n\tfor ( const element of math.querySelectorAll( '[class]' ) ) {\n\t\telement.removeAttribute( 'class' );\n\t}\n\n\treturn math.innerHTML;\n}\n"],
5
+ "mappings": ";AAAA,OAAO,WAAW;AA6BH,SAAR,cACN,OACA,EAAE,cAAc,KAAK,IAA0B,CAAC,GACvC;AACT,QAAM,SAAS,MAAM,eAAgB,OAAO;AAAA,IAC3C;AAAA,IACA,UAAU;AAAA,IACV,cAAc;AAAA,EACf,CAAE;AACF,QAAM,MAAM,SAAS,eAAe,mBAAoB,EAAG;AAC3D,MAAI,KAAK,YAAY;AACrB,QAAM,OAAO,IAAI,KAAK,cAAe,MAAO;AAC5C,MAAK,CAAE,MAAO;AACb,WAAO;AAAA,EACR;AAOA,aAAY,QAAQ,KAAK;AAAA,IACxB;AAAA,EACD,GAAI;AACH,SAAK;AAAA,MACJ;AAAA,MACA,KAAK,UAAU,SAAU,WAAY,IAAI,UAAU;AAAA,IACpD;AAAA,EACD;AAKA,aAAY,WAAW,KAAK,iBAAkB,SAAU,GAAI;AAC3D,YAAQ,gBAAiB,OAAQ;AAAA,EAClC;AAEA,SAAO,KAAK;AACb;",
6
6
  "names": []
7
7
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACpC;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACtB;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CACpC,KAAK,EAAE,MAAM,EACb,EAAE,WAAkB,EAAE,GAAE,oBAAyB,GAC/C,MAAM,CASR"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACpC;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACtB;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CACpC,KAAK,EAAE,MAAM,EACb,EAAE,WAAkB,EAAE,GAAE,oBAAyB,GAC/C,MAAM,CAmCR"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.jsdom.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.jsdom.test.d.ts","sourceRoot":"","sources":["../../src/test/index.jsdom.test.js"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/latex-to-mathml",
3
- "version": "1.23.1-next.v.202609171837.0",
3
+ "version": "1.25.0",
4
4
  "description": "Convert LaTeX math syntax to MathML.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -21,8 +21,7 @@
21
21
  "url": "https://github.com/WordPress/gutenberg/issues"
22
22
  },
23
23
  "engines": {
24
- "node": ">=18.12.0",
25
- "npm": ">=8.19.2"
24
+ "node": ">=18.12.0"
26
25
  },
27
26
  "files": [
28
27
  "src",
@@ -49,8 +48,11 @@
49
48
  "dependencies": {
50
49
  "temml": "^0.10.33"
51
50
  },
51
+ "devDependencies": {
52
+ "vitest": "^5.0.3"
53
+ },
52
54
  "publishConfig": {
53
55
  "access": "public"
54
56
  },
55
- "gitHead": "7fc9f6676debc888d2f071d17cb71837040d4b48"
57
+ "gitHead": "12441e2520c78b2254a792a8cba40c34110be060"
56
58
  }
package/src/index.ts CHANGED
@@ -38,5 +38,31 @@ export default function latexToMathML(
38
38
  } );
39
39
  const doc = document.implementation.createHTMLDocument( '' );
40
40
  doc.body.innerHTML = mathML;
41
- return doc.body.querySelector( 'math' )?.innerHTML ?? '';
41
+ const math = doc.body.querySelector( 'math' );
42
+ if ( ! math ) {
43
+ return '';
44
+ }
45
+
46
+ // temml marks the cells of `aligned`, `cases` and tagged equations with
47
+ // classes for its own stylesheet. Write the MathML `columnalign`
48
+ // attribute on the cell instead, so the alignment is part of the content:
49
+ // Firefox and Safari honor it natively, and the polyfill in `style.scss`
50
+ // covers Chromium, which implements MathML Core only.
51
+ for ( const cell of math.querySelectorAll(
52
+ 'mtd.tml-right, mtd.tml-left'
53
+ ) ) {
54
+ cell.setAttribute(
55
+ 'columnalign',
56
+ cell.classList.contains( 'tml-right' ) ? 'right' : 'left'
57
+ );
58
+ }
59
+
60
+ // The remaining classes are hooks for temml's stylesheet, which is not
61
+ // used. Drop them so the content is plain MathML and does not depend on
62
+ // the class names of a temml version.
63
+ for ( const element of math.querySelectorAll( '[class]' ) ) {
64
+ element.removeAttribute( 'class' );
65
+ }
66
+
67
+ return math.innerHTML;
42
68
  }
@@ -0,0 +1,49 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import latexToMathML from '../';
3
+
4
+ describe( 'latexToMathML', () => {
5
+ it( 'writes columnalign on the cells of an aligned environment', () => {
6
+ const doc = document.implementation.createHTMLDocument( '' );
7
+ doc.body.innerHTML = `<math>${ latexToMathML(
8
+ '\\begin{aligned} a &= b \\\\ c &= d \\end{aligned}'
9
+ ) }</math>`;
10
+
11
+ expect(
12
+ Array.from( doc.querySelectorAll( 'mtd' ), ( cell ) =>
13
+ cell.getAttribute( 'columnalign' )
14
+ )
15
+ ).toEqual( [ 'right', 'left', 'right', 'left' ] );
16
+ } );
17
+
18
+ it( 'writes columnalign on the cells of a cases environment', () => {
19
+ const doc = document.implementation.createHTMLDocument( '' );
20
+ doc.body.innerHTML = `<math>${ latexToMathML(
21
+ '\\begin{cases} x & x > 0 \\\\ -x & x \\le 0 \\end{cases}'
22
+ ) }</math>`;
23
+
24
+ expect(
25
+ Array.from( doc.querySelectorAll( 'mtd' ), ( cell ) =>
26
+ cell.getAttribute( 'columnalign' )
27
+ )
28
+ ).toEqual( [ 'left', 'left', 'left', 'left' ] );
29
+ } );
30
+
31
+ it( 'strips the classes temml adds for its stylesheet', () => {
32
+ expect(
33
+ latexToMathML( '\\overline{ab} \\begin{cases} a & b \\end{cases}' )
34
+ ).not.toContain( 'class=' );
35
+ } );
36
+
37
+ it( 'leaves centered cells alone', () => {
38
+ const doc = document.implementation.createHTMLDocument( '' );
39
+ doc.body.innerHTML = `<math>${ latexToMathML(
40
+ '\\begin{pmatrix} a & b \\end{pmatrix}'
41
+ ) }</math>`;
42
+
43
+ expect(
44
+ Array.from( doc.querySelectorAll( 'mtd' ), ( cell ) =>
45
+ cell.hasAttribute( 'columnalign' )
46
+ )
47
+ ).toEqual( [ false, false ] );
48
+ } );
49
+ } );