Skip to content

Commit

Permalink
馃珦 Flatten inlineCode (that may have children) (#920)
Browse files Browse the repository at this point in the history
When tex is being parsed, it is possible for inline code `\texttt{}` to have children. If all of those children are text, then the children are transformed into be a single node with `.value`.
  • Loading branch information
rowanc1 committed Feb 20, 2024
1 parent 9bd2906 commit 9cdd204
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-mayflies-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"myst-common": patch
---

Add inline code rule for errors
5 changes: 5 additions & 0 deletions .changeset/great-goats-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"myst-transforms": patch
---

Flatten inline code by default
1 change: 1 addition & 0 deletions packages/myst-common/src/ruleids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export enum RuleId {
codeMetatagsValid = 'code-metatags-valid',
codeLangDefined = 'code-lang-defined',
codeMetadataLoads = 'code-metadata-loads',
inlineCodeMalformed = 'inline-code-malformed',
inlineExpressionRenders = 'inline-expression-renders',
// Static file rules
staticFileCopied = 'static-file-copied',
Expand Down
3 changes: 2 additions & 1 deletion packages/myst-transforms/src/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { htmlIdsTransform } from './htmlIds.js';
import { imageAltTextTransform } from './images.js';
import { mathLabelTransform, mathNestingTransform, subequationTransform } from './math.js';
import { blockquoteTransform } from './blockquote.js';
import { codeBlockToDirectiveTransform } from './code.js';
import { codeBlockToDirectiveTransform, inlineCodeFlattenTransform } from './code.js';
import { removeUnicodeTransform } from './removeUnicode.js';
import { containerChildrenTransform } from './containers.js';
import { headingDepthTransform } from './headings.js';
Expand Down Expand Up @@ -41,6 +41,7 @@ export function basicTransformations(tree: GenericParent, file: VFile, opts: Rec
blockquoteTransform(tree);
removeUnicodeTransform(tree);
headingDepthTransform(tree, file, opts);
inlineCodeFlattenTransform(tree, file);
}

export const basicTransformationsPlugin: Plugin<
Expand Down
45 changes: 44 additions & 1 deletion packages/myst-transforms/src/code.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'vitest';
import { VFile } from 'vfile';
import { codeTransform } from './code';
import { codeTransform, inlineCodeFlattenTransform } from './code';

describe('Test codeTransform', () => {
test('simple code block returns self', async () => {
Expand Down Expand Up @@ -67,4 +67,47 @@ describe('Test codeTransform', () => {
});
expect(file.messages.length).toBe(1);
});

test('inline code with text children will flatten', async () => {
const mdast = {
type: 'root',
children: [
{
type: 'inlineCode',
children: [
{ type: 'text', value: 'one ' },
{ type: 'text', value: 'two' },
],
},
],
};
const file = new VFile();
inlineCodeFlattenTransform(mdast, file);
expect(mdast).toEqual({
type: 'root',
children: [{ type: 'inlineCode', value: 'one two' }],
});
});

test('inline code with children and value warns', async () => {
const mdast = {
type: 'root',
children: [
{
type: 'inlineCode',
value: 'blah',
children: [{ type: 'text', value: 'x' }],
},
],
};
const file = new VFile();
inlineCodeFlattenTransform(mdast, file);
expect(mdast).toEqual({
type: 'root',
// No changes
children: [{ type: 'inlineCode', value: 'blah', children: [{ type: 'text', value: 'x' }] }],
});
// A warning was created
expect(file.messages.length).toBe(1);
});
});
29 changes: 28 additions & 1 deletion packages/myst-transforms/src/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,35 @@ import { selectAll } from 'unist-util-select';
import type { GenericNode, GenericParent } from 'myst-common';
import { RuleId, fileWarn } from 'myst-common';
import type { VFile } from 'vfile';
import type { Code } from 'myst-spec';
import type { Code, InlineCode } from 'myst-spec';

type Options = {
lang?: string;
transformPython?: boolean;
};

/**
* Flatten any inline code that only has text in it to a single value.
*/
export function inlineCodeFlattenTransform(mdast: GenericParent, file: VFile) {
const code = selectAll('inlineCode', mdast) as (InlineCode & { children?: GenericNode[] })[];
code.forEach((node) => {
if (!node?.children) return;
if (node.value) {
fileWarn(file, 'Both children and value defined for inline code.', {
node,
ruleId: RuleId.inlineCodeMalformed,
});
// This is a warning, but can be ignored.
return;
}
if (!node.children.reduce((b, c) => b && c.type === 'text', true)) return;
// All the children are now text nodes
node.value = node.children.reduce((t, c) => t + c.value, '');
delete node.children;
});
}

export function codeTransform(mdast: GenericParent, file: VFile, opts?: Options) {
const code = selectAll('code', mdast) as Code[];
code.forEach((node) => {
Expand Down Expand Up @@ -65,3 +87,8 @@ export const codeBlockToDirectivePlugin: Plugin<
> = (opts) => (tree, file) => {
codeBlockToDirectiveTransform(tree, file, opts);
};

export const inlineCodeFlattenPlugin: Plugin<[], GenericParent, GenericParent> =
() => (tree, file) => {
inlineCodeFlattenTransform(tree, file);
};
7 changes: 6 additions & 1 deletion packages/myst-transforms/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ export {
blockMetadataPlugin,
blockMetadataTransform,
} from './blocks.js';
export { codePlugin, codeTransform } from './code.js';
export {
codePlugin,
codeTransform,
inlineCodeFlattenPlugin,
inlineCodeFlattenTransform,
} from './code.js';
export { blockquotePlugin, blockquoteTransform } from './blockquote.js';
export { imageAltTextPlugin, imageAltTextTransform } from './images.js';
export {
Expand Down

0 comments on commit 9cdd204

Please sign in to comment.