Skip to content

Commit d6ea7c3

Browse files
committed
🧜‍♀️ Support mermaid as a code block
See jupyter/enhancement-proposals#101
1 parent 2960da0 commit d6ea7c3

File tree

6 files changed

+45
-17
lines changed

6 files changed

+45
-17
lines changed

.changeset/khaki-poems-hunt.md

+7
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+
Translate mermaid and math code blocks by default.

docs/diagrams.md

+4
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ flowchart LR
2020
D <--> J[JATS]
2121
```
2222
````
23+
24+
:::{note}
25+
Both GitHub and JupyterLab ([#101](https://github.com/jupyter/enhancement-proposals/pull/101)) support the translation of a code-block ` ```mermaid ` to a mermaid diagram directly, this can also be used by default in MyST.
26+
:::

packages/myst-transforms/src/basic.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ 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 { mathCodeBlockTransform, mathLabelTransform, mathNestingTransform } from './math.js';
11+
import { mathLabelTransform, mathNestingTransform } from './math.js';
1212
import { blockquoteTransform } from './blockquote.js';
13+
import { codeBlockToDirectiveTransform } from './code.js';
1314

1415
export function basicTransformations(tree: Root, file: VFile) {
1516
// lifting roles and directives must happen before the mystTarget transformation
1617
liftMystDirectivesAndRolesTransform(tree);
1718
// Some specifics about the ordering are noted below
1819
captionParagraphTransform(tree);
19-
mathCodeBlockTransform(tree, file);
20+
codeBlockToDirectiveTransform(tree, file, { translate: ['math', 'mermaid'] });
2021
mathNestingTransform(tree, file);
2122
// Math labelling should happen before the target-transformation
2223
mathLabelTransform(tree, file);

packages/myst-transforms/src/code.ts

+28
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Plugin } from 'unified';
22
import type { Root } from 'mdast';
33
import type { Code } from 'myst-spec';
44
import { selectAll } from 'unist-util-select';
5+
import type { GenericNode } from 'myst-common';
56
import { fileWarn } from 'myst-common';
67
import type { VFile } from 'vfile';
78

@@ -30,3 +31,30 @@ export function codeTransform(mdast: Root, file: VFile, opts?: Options) {
3031
export const codePlugin: Plugin<[Options?], Root, Root> = (opts) => (tree, file) => {
3132
codeTransform(tree, file, opts);
3233
};
34+
35+
type CodeBlockTransformOptions = {
36+
translate: (string | { lang: string; directive?: string })[];
37+
};
38+
39+
export function codeBlockToDirectiveTransform(
40+
tree: Root,
41+
file: VFile,
42+
opts?: CodeBlockTransformOptions,
43+
) {
44+
if (!opts || !opts.translate || opts.translate.length === 0) return;
45+
const nodes = selectAll('code', tree) as Code[];
46+
nodes.forEach((node) => {
47+
if (!node.lang) return;
48+
const res = opts.translate.find(
49+
(t) => t === node.lang || (typeof t !== 'string' && t.lang === node.lang),
50+
);
51+
if (!res) return;
52+
(node as GenericNode).type = typeof res === 'string' ? res : res.directive || res.lang;
53+
delete node.lang;
54+
});
55+
}
56+
57+
export const codeBlockToDirectivePlugin: Plugin<[CodeBlockTransformOptions?], Root, Root> =
58+
(opts) => (tree, file) => {
59+
codeBlockToDirectiveTransform(tree, file, opts);
60+
};

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { describe, expect, test } from 'vitest';
22
import { unified } from 'unified';
33
import { VFile } from 'vfile';
4-
import { mathTransform, mathPlugin, mathNestingTransform, mathCodeBlockTransform } from './math';
4+
import { mathTransform, mathPlugin, mathNestingTransform } from './math';
5+
import { codeBlockToDirectiveTransform } from './code';
56

67
const ARRAY_ALIGN = `\\begin{align*}
78
L=
@@ -129,7 +130,7 @@ describe('Test math code block transformation', () => {
129130
test('Block paragraph', () => {
130131
const file = new VFile();
131132
const mdast = { children: [{ type: 'code', lang: 'math', value: 'Ax = b' }] } as any;
132-
mathCodeBlockTransform(mdast, file);
133+
codeBlockToDirectiveTransform(mdast, file, { translate: ['math'] });
133134
expect(mdast.children[0].type).toBe('math');
134135
expect(mdast.children[0].lang).toBeUndefined();
135136
expect(mdast.children[0].value).toBe('Ax = b');

packages/myst-transforms/src/math.ts

-13
Original file line numberDiff line numberDiff line change
@@ -239,26 +239,13 @@ 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-
251242
export function mathTransform(tree: Root, file: VFile, opts?: Options) {
252243
const nodes = selectAll('math,inlineMath', tree) as (Math | InlineMath)[];
253244
nodes.forEach((node) => {
254245
renderEquation(file, node, opts);
255246
});
256247
}
257248

258-
export const mathCodeBlockPlugin: Plugin<[], Root, Root> = () => (tree, file) => {
259-
mathCodeBlockTransform(tree, file);
260-
};
261-
262249
export const mathNestingPlugin: Plugin<[], Root, Root> = () => (tree, file) => {
263250
mathNestingTransform(tree, file);
264251
};

0 commit comments

Comments
 (0)