From 2960da05176b12b9ac69340801ef5e56d9ffc153 Mon Sep 17 00:00:00 2001 From: Rowan Cockett Date: Wed, 2 Aug 2023 14:31:09 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AE=20Math=20code=20blocks=20for=20Git?= =?UTF-8?q?Hub=20Flavored=20Markdown=20(#524)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See executablebooks/MyST-Parser#798 --- .changeset/strong-ducks-appear.md | 7 +++++++ packages/myst-transforms/src/basic.ts | 3 ++- packages/myst-transforms/src/math.spec.ts | 13 ++++++++++++- packages/myst-transforms/src/math.ts | 15 ++++++++++++++- 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 .changeset/strong-ducks-appear.md diff --git a/.changeset/strong-ducks-appear.md b/.changeset/strong-ducks-appear.md new file mode 100644 index 000000000..0116b8e8e --- /dev/null +++ b/.changeset/strong-ducks-appear.md @@ -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. diff --git a/packages/myst-transforms/src/basic.ts b/packages/myst-transforms/src/basic.ts index 23500c10c..641a6e801 100644 --- a/packages/myst-transforms/src/basic.ts +++ b/packages/myst-transforms/src/basic.ts @@ -8,7 +8,7 @@ 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) { @@ -16,6 +16,7 @@ export function basicTransformations(tree: Root, file: VFile) { 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); diff --git a/packages/myst-transforms/src/math.spec.ts b/packages/myst-transforms/src/math.spec.ts index 85f4d0e9b..d7aac24b5 100644 --- a/packages/myst-transforms/src/math.spec.ts +++ b/packages/myst-transforms/src/math.spec.ts @@ -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= @@ -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'); + }); +}); diff --git a/packages/myst-transforms/src/math.ts b/packages/myst-transforms/src/math.ts index 07819b91b..e2a9d733e 100644 --- a/packages/myst-transforms/src/math.ts +++ b/packages/myst-transforms/src/math.ts @@ -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'; @@ -239,6 +239,15 @@ 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) => { @@ -246,6 +255,10 @@ export function mathTransform(tree: Root, file: VFile, opts?: Options) { }); } +export const mathCodeBlockPlugin: Plugin<[], Root, Root> = () => (tree, file) => { + mathCodeBlockTransform(tree, file); +}; + export const mathNestingPlugin: Plugin<[], Root, Root> = () => (tree, file) => { mathNestingTransform(tree, file); };