Skip to content

Commit 2960da0

Browse files
authored
🧮 Math code blocks for GitHub Flavored Markdown (#524)
See executablebooks/MyST-Parser#798
1 parent a89ecba commit 2960da0

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

.changeset/strong-ducks-appear.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'myst-transforms': patch
3+
'myst-cli': patch
4+
'mystmd': patch
5+
---
6+
7+
Transform code blocks with `math` language to be math blocks. This is GitHub markdown.

packages/myst-transforms/src/basic.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import { admonitionBlockquoteTransform, admonitionHeadersTransform } from './adm
88
import { blockMetadataTransform, blockNestingTransform } from './blocks.js';
99
import { htmlIdsTransform } from './htmlIds.js';
1010
import { imageAltTextTransform } from './images.js';
11-
import { mathLabelTransform, mathNestingTransform } from './math.js';
11+
import { mathCodeBlockTransform, mathLabelTransform, mathNestingTransform } from './math.js';
1212
import { blockquoteTransform } from './blockquote.js';
1313

1414
export function basicTransformations(tree: Root, file: VFile) {
1515
// lifting roles and directives must happen before the mystTarget transformation
1616
liftMystDirectivesAndRolesTransform(tree);
1717
// Some specifics about the ordering are noted below
1818
captionParagraphTransform(tree);
19+
mathCodeBlockTransform(tree, file);
1920
mathNestingTransform(tree, file);
2021
// Math labelling should happen before the target-transformation
2122
mathLabelTransform(tree, file);

packages/myst-transforms/src/math.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, test } from 'vitest';
22
import { unified } from 'unified';
33
import { VFile } from 'vfile';
4-
import { mathTransform, mathPlugin, mathNestingTransform } from './math';
4+
import { mathTransform, mathPlugin, mathNestingTransform, mathCodeBlockTransform } from './math';
55

66
const ARRAY_ALIGN = `\\begin{align*}
77
L=
@@ -124,3 +124,14 @@ describe('Test math nesting transformation', () => {
124124
expect(mdast.children[0].children[2].class).toBe('importantClass');
125125
});
126126
});
127+
128+
describe('Test math code block transformation', () => {
129+
test('Block paragraph', () => {
130+
const file = new VFile();
131+
const mdast = { children: [{ type: 'code', lang: 'math', value: 'Ax = b' }] } as any;
132+
mathCodeBlockTransform(mdast, file);
133+
expect(mdast.children[0].type).toBe('math');
134+
expect(mdast.children[0].lang).toBeUndefined();
135+
expect(mdast.children[0].value).toBe('Ax = b');
136+
});
137+
});

packages/myst-transforms/src/math.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Plugin } from 'unified';
22
import type { VFile } from 'vfile';
33
import katex from 'katex';
44
import type { Root } from 'mdast';
5-
import type { Math, InlineMath, Node } from 'myst-spec';
5+
import type { Math, InlineMath, Node, Code } from 'myst-spec';
66
import { selectAll } from 'unist-util-select';
77
import type { GenericParent } from 'myst-common';
88
import { copyNode, fileError, fileWarn, normalizeLabel } from 'myst-common';
@@ -239,13 +239,26 @@ export function mathLabelTransform(tree: Root, file: VFile) {
239239
});
240240
}
241241

242+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
243+
export function mathCodeBlockTransform(tree: Root, file: VFile) {
244+
const nodes = selectAll('code[lang="math"]', tree) as Code[];
245+
nodes.forEach((node) => {
246+
(node as unknown as Math).type = 'math';
247+
delete node.lang;
248+
});
249+
}
250+
242251
export function mathTransform(tree: Root, file: VFile, opts?: Options) {
243252
const nodes = selectAll('math,inlineMath', tree) as (Math | InlineMath)[];
244253
nodes.forEach((node) => {
245254
renderEquation(file, node, opts);
246255
});
247256
}
248257

258+
export const mathCodeBlockPlugin: Plugin<[], Root, Root> = () => (tree, file) => {
259+
mathCodeBlockTransform(tree, file);
260+
};
261+
249262
export const mathNestingPlugin: Plugin<[], Root, Root> = () => (tree, file) => {
250263
mathNestingTransform(tree, file);
251264
};

0 commit comments

Comments
 (0)