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

🧮 Math code blocks for GitHub Flavored Markdown #524

Merged
merged 1 commit 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/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
Loading