Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧮 Use katex for mathml rendering in jats #507

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/good-apples-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'myst-to-jats': patch
'myst-cli': patch
'mystmd': patch
---

Add mathml to the exported JATS.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/myst-to-jats/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"citation-js-utils": "^1.0.1",
"jats-tags": "^1.0.1",
"katex": "^0.15.2",
"myst-common": "^1.1.0",
"myst-frontmatter": "^1.1.0",
"myst-spec": "^0.0.4",
Expand All @@ -49,6 +50,7 @@
},
"devDependencies": {
"@types/js-yaml": "^4.0.5",
"@types/katex": "^0.14.0",
"@types/mdast": "^3.0.10",
"jats-xml": "^1.0.3",
"js-yaml": "^4.1.0",
Expand Down
29 changes: 28 additions & 1 deletion packages/myst-to-jats/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { Root, CrossReference, TableCell as SpecTableCell } from 'myst-spec
import type { Cite, Code, FootnoteDefinition, FootnoteReference } from 'myst-spec-ext';
import type { Plugin } from 'unified';
import type { VFile } from 'vfile';
import { js2xml } from 'xml-js';
import { js2xml, xml2js } from 'xml-js';
import katex from 'katex';
import type { CitationRenderer } from 'citation-js-utils';
import type { MessageInfo, GenericNode } from 'myst-common';
import { copyNode, fileError } from 'myst-common';
Expand Down Expand Up @@ -118,6 +119,26 @@ function alternativesFromMinifiedOutput(output: MinifiedOutput, state: IJatsSeri
state.closeNode();
}

function mathToMml(math?: string, inline?: boolean) {
const katexXml = katex.renderToString(math, { output: 'mathml', throwOnError: false });
const katexJs = xml2js(katexXml, { compact: false }) as Element;
const spanElement = katexJs.elements?.[0];
const mathElement = spanElement?.elements?.[0];
if (!mathElement) return;
if (inline) mathElement.attributes = { ...mathElement.attributes, display: 'inline' };
delete mathElement.attributes?.xmlns;
function addMmlAndRemoveAnnotation(el?: Element) {
if (el?.name) el.name = `mml:${el.name}`;
if (!el?.elements) return;
el.elements = el.elements.filter((child: Element) => child.name !== 'annotation');
el.elements.forEach((child: Element) => {
addMmlAndRemoveAnnotation(child);
});
}
addMmlAndRemoveAnnotation(mathElement);
return mathElement;
}

const handlers: Record<string, Handler> = {
text(node, state) {
state.text(node.value);
Expand Down Expand Up @@ -181,10 +202,13 @@ const handlers: Record<string, Handler> = {
},
inlineMath(node, state) {
state.openNode('inline-formula');
state.openNode('alternatives');
state.pushNode(mathToMml(node.value, true));
state.openNode('tex-math');
state.addLeaf('cdata', { cdata: node.value });
state.closeNode();
state.closeNode();
state.closeNode();
},
math(node, state) {
const dispFormulaAttrs: Attributes = {};
Expand All @@ -193,10 +217,13 @@ const handlers: Record<string, Handler> = {
}
state.openNode('disp-formula', dispFormulaAttrs);
renderLabel(node, state, (enumerator) => `(${enumerator})`);
state.openNode('alternatives');
state.pushNode(mathToMml(node.value));
state.openNode('tex-math');
state.addLeaf('cdata', { cdata: node.value });
state.closeNode();
state.closeNode();
state.closeNode();
},
mystRole(node, state) {
state.renderChildren(node);
Expand Down
1 change: 1 addition & 0 deletions packages/myst-to-jats/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface IJatsSerializer<D extends Record<string, any> = StateData> {
text: (value?: string) => void;
renderChildren: (node: any) => void;
renderInline: (node: GenericNode, name: string, attributes?: Attributes) => void;
pushNode: (el?: Element) => void;
addLeaf: (name: string, attributes?: Attributes) => void;
openNode: (name: string, attributes?: Attributes) => void;
closeNode: () => void;
Expand Down
1 change: 1 addition & 0 deletions packages/myst-to-jats/src/types/katex/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'katex';
8 changes: 4 additions & 4 deletions packages/myst-to-jats/tests/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ cases:
value: Ax=b
- type: text
value: .
jats: <p>This math is a role, <inline-formula><tex-math><![CDATA[e=mc^2]]></tex-math></inline-formula>, while this math is wrapped in dollar signs, <inline-formula><tex-math><![CDATA[Ax=b]]></tex-math></inline-formula>.</p>
jats: <p>This math is a role, <inline-formula><alternatives><mml:math display="inline"><mml:semantics><mml:mrow><mml:mi>e</mml:mi><mml:mo>=</mml:mo><mml:mi>m</mml:mi><mml:msup><mml:mi>c</mml:mi><mml:mn>2</mml:mn></mml:msup></mml:mrow></mml:semantics></mml:math><tex-math><![CDATA[e=mc^2]]></tex-math></alternatives></inline-formula>, while this math is wrapped in dollar signs, <inline-formula><alternatives><mml:math display="inline"><mml:semantics><mml:mrow><mml:mi>A</mml:mi><mml:mi>x</mml:mi><mml:mo>=</mml:mo><mml:mi>b</mml:mi></mml:mrow></mml:semantics></mml:math><tex-math><![CDATA[Ax=b]]></tex-math></alternatives></inline-formula>.</p>
- title: Display Math (label)
tree:
type: root
Expand All @@ -198,14 +198,14 @@ cases:
identifier: my-equation
enumerator: 1
value: w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
jats: <disp-formula id="my-equation"><label>(1)</label><tex-math><![CDATA[w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}]]></tex-math></disp-formula>
jats: <disp-formula id="my-equation"><label>(1)</label><alternatives><mml:math><mml:semantics><mml:mrow><mml:msub><mml:mi>w</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo>=</mml:mo><mml:mo stretchy="false">(</mml:mo><mml:mn>1</mml:mn><mml:mo>+</mml:mo><mml:msub><mml:mi>r</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mi>s</mml:mi><mml:mo stretchy="false">(</mml:mo><mml:msub><mml:mi>w</mml:mi><mml:mi>t</mml:mi></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mo>+</mml:mo><mml:msub><mml:mi>y</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub></mml:mrow></mml:semantics></mml:math><tex-math><![CDATA[w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}]]></tex-math></alternatives></disp-formula>
- title: Display Math (no label)
tree:
type: root
children:
- type: math
value: w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
jats: <disp-formula><tex-math><![CDATA[w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}]]></tex-math></disp-formula>
jats: <disp-formula><alternatives><mml:math><mml:semantics><mml:mrow><mml:msub><mml:mi>w</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo>=</mml:mo><mml:mo stretchy="false">(</mml:mo><mml:mn>1</mml:mn><mml:mo>+</mml:mo><mml:msub><mml:mi>r</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mi>s</mml:mi><mml:mo stretchy="false">(</mml:mo><mml:msub><mml:mi>w</mml:mi><mml:mi>t</mml:mi></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mo>+</mml:mo><mml:msub><mml:mi>y</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub></mml:mrow></mml:semantics></mml:math><tex-math><![CDATA[w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}]]></tex-math></alternatives></disp-formula>
- title: Display Math (not numbered)
tree:
type: root
Expand All @@ -215,7 +215,7 @@ cases:
enumerated: false
enumerator: 1
value: w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}
jats: <disp-formula id="my-equation"><tex-math><![CDATA[w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}]]></tex-math></disp-formula>
jats: <disp-formula id="my-equation"><alternatives><mml:math><mml:semantics><mml:mrow><mml:msub><mml:mi>w</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo>=</mml:mo><mml:mo stretchy="false">(</mml:mo><mml:mn>1</mml:mn><mml:mo>+</mml:mo><mml:msub><mml:mi>r</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mi>s</mml:mi><mml:mo stretchy="false">(</mml:mo><mml:msub><mml:mi>w</mml:mi><mml:mi>t</mml:mi></mml:msub><mml:mo stretchy="false">)</mml:mo><mml:mo>+</mml:mo><mml:msub><mml:mi>y</mml:mi><mml:mrow><mml:mi>t</mml:mi><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:mrow></mml:msub></mml:mrow></mml:semantics></mml:math><tex-math><![CDATA[w_{t+1} = (1 + r_{t+1}) s(w_t) + y_{t+1}]]></tex-math></alternatives></disp-formula>
- title: Lists
tree:
type: root
Expand Down
Loading