Skip to content

Commit

Permalink
🧮 Math code blocks for GitHub Flavored Markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 committed Aug 2, 2023
1 parent a89ecba commit 06bb7d8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/strong-ducks-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'myst-transforms': patch
'myst-cli': patch
'mystmd': patch
---

Transform code blocks with `math` language to be math blocks. This is GitHub markdown.
3 changes: 2 additions & 1 deletion packages/myst-transforms/src/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import { admonitionBlockquoteTransform, admonitionHeadersTransform } from './adm
import { blockMetadataTransform, blockNestingTransform } from './blocks.js';
import { htmlIdsTransform } from './htmlIds.js';
import { imageAltTextTransform } from './images.js';
import { mathLabelTransform, mathNestingTransform } from './math.js';
import { mathCodeBlockTransform, mathLabelTransform, mathNestingTransform } from './math.js';
import { blockquoteTransform } from './blockquote.js';

export function basicTransformations(tree: Root, file: VFile) {
// lifting roles and directives must happen before the mystTarget transformation
liftMystDirectivesAndRolesTransform(tree);
// Some specifics about the ordering are noted below
captionParagraphTransform(tree);
mathCodeBlockTransform(tree, file);
mathNestingTransform(tree, file);
// Math labelling should happen before the target-transformation
mathLabelTransform(tree, file);
Expand Down
13 changes: 12 additions & 1 deletion packages/myst-transforms/src/math.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, test } from 'vitest';
import { unified } from 'unified';
import { VFile } from 'vfile';
import { mathTransform, mathPlugin, mathNestingTransform } from './math';
import { mathTransform, mathPlugin, mathNestingTransform, mathCodeBlockTransform } from './math';

const ARRAY_ALIGN = `\\begin{align*}
L=
Expand Down Expand Up @@ -124,3 +124,14 @@ describe('Test math nesting transformation', () => {
expect(mdast.children[0].children[2].class).toBe('importantClass');
});
});

describe('Test math code block transformation', () => {
test('Block paragraph', () => {
const file = new VFile();
const mdast = { children: [{ type: 'code', lang: 'math', value: 'Ax = b' }] } as any;
mathCodeBlockTransform(mdast, file);
expect(mdast.children[0].type).toBe('math');
expect(mdast.children[0].lang).toBeUndefined();
expect(mdast.children[0].value).toBe('Ax = b');
});
});
15 changes: 14 additions & 1 deletion packages/myst-transforms/src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Plugin } from 'unified';
import type { VFile } from 'vfile';
import katex from 'katex';
import type { Root } from 'mdast';
import type { Math, InlineMath, Node } from 'myst-spec';
import type { Math, InlineMath, Node, Code } from 'myst-spec';
import { selectAll } from 'unist-util-select';
import type { GenericParent } from 'myst-common';
import { copyNode, fileError, fileWarn, normalizeLabel } from 'myst-common';
Expand Down Expand Up @@ -239,13 +239,26 @@ export function mathLabelTransform(tree: Root, file: VFile) {
});
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function mathCodeBlockTransform(tree: Root, file: VFile) {
const nodes = selectAll('code[lang="math"]', tree) as Code[];
nodes.forEach((node) => {
(node as unknown as Math).type = 'math';
delete node.lang;
});
}

export function mathTransform(tree: Root, file: VFile, opts?: Options) {
const nodes = selectAll('math,inlineMath', tree) as (Math | InlineMath)[];
nodes.forEach((node) => {
renderEquation(file, node, opts);
});
}

export const mathCodeBlockPlugin: Plugin<[], Root, Root> = () => (tree, file) => {
mathCodeBlockTransform(tree, file);
};

export const mathNestingPlugin: Plugin<[], Root, Root> = () => (tree, file) => {
mathNestingTransform(tree, file);
};
Expand Down

0 comments on commit 06bb7d8

Please sign in to comment.