Skip to content

Commit

Permalink
👯‍♀️ Add directive option alias (#608)
Browse files Browse the repository at this point in the history
You can now use `label` or `name` for all directives
  • Loading branch information
rowanc1 committed Sep 19, 2023
1 parent c873f2d commit ed7b430
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 18 deletions.
7 changes: 7 additions & 0 deletions .changeset/cold-ghosts-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'myst-directives': patch
'myst-common': patch
'myst-parser': patch
---

Allow alias field for directive options.
7 changes: 7 additions & 0 deletions .changeset/moody-months-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'myst-directives': patch
'myst-parser': patch
'myst-cli': patch
---

All instances of `name` options in directives can also use `label`. (e.g. in a figure or equation).
4 changes: 3 additions & 1 deletion packages/myst-common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export type ArgDefinition = {

type BodyDefinition = ArgDefinition;

type OptionDefinition = ArgDefinition;
type OptionDefinition = ArgDefinition & {
alias?: string[];
};

export type DirectiveData = {
name: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/myst-directives/src/admonition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export const admonitionDirective: DirectiveSpec = {
type: ParseTypesEnum.parsed,
},
options: {
// name: {
// label: {
// type: ParseTypesEnum.string,
// alias: ['name'],
// },
class: {
type: ParseTypesEnum.string,
Expand Down
10 changes: 6 additions & 4 deletions packages/myst-directives/src/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ export const codeDirective: DirectiveSpec = {
type: ParseTypesEnum.string,
},
options: {
name: {
label: {
type: ParseTypesEnum.string,
alias: ['name'],
},
class: {
type: ParseTypesEnum.string,
Expand All @@ -29,7 +30,7 @@ export const codeDirective: DirectiveSpec = {
type: ParseTypesEnum.string,
},
run(data): GenericNode[] {
const { label, identifier } = normalizeLabel(data.options?.name as string | undefined) || {};
const { label, identifier } = normalizeLabel(data.options?.label as string | undefined) || {};
const numberLines = data.options?.['number-lines'] as number | undefined;
const showLineNumbers = !!numberLines;
const startingLineNumber = numberLines && numberLines > 1 ? numberLines : undefined;
Expand All @@ -54,8 +55,9 @@ export const codeBlockDirective: DirectiveSpec = {
type: ParseTypesEnum.string,
},
options: {
name: {
label: {
type: ParseTypesEnum.string,
alias: ['name'],
},
class: {
type: ParseTypesEnum.string,
Expand Down Expand Up @@ -89,7 +91,7 @@ export const codeBlockDirective: DirectiveSpec = {
type: ParseTypesEnum.string,
},
run(data): GenericNode[] {
const { label, identifier } = normalizeLabel(data.options?.name as string | undefined) || {};
const { label, identifier } = normalizeLabel(data.options?.label as string | undefined) || {};
// Validating this should probably happen first
const emphasizeLinesString = data.options?.['emphasize-lines'] as string | undefined;
const emphasizeLines = emphasizeLinesString
Expand Down
5 changes: 3 additions & 2 deletions packages/myst-directives/src/figure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export const figureDirective: DirectiveSpec = {
required: true,
},
options: {
name: {
label: {
type: ParseTypesEnum.string,
alias: ['name'],
},
class: {
type: ParseTypesEnum.string,
Expand Down Expand Up @@ -99,7 +100,7 @@ export const figureDirective: DirectiveSpec = {
children.push({ type: 'legend', children: legend });
}
}
const { label, identifier } = normalizeLabel(data.options?.name as string | undefined) || {};
const { label, identifier } = normalizeLabel(data.options?.label as string | undefined) || {};
const container = {
type: 'container',
kind: 'figure',
Expand Down
6 changes: 2 additions & 4 deletions packages/myst-directives/src/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ export const iframeDirective: DirectiveSpec = {
required: true,
},
options: {
name: {
label: {
type: ParseTypesEnum.string,
alias: ['name'],
},
class: {
type: ParseTypesEnum.string,
// class_option: list of strings?
},
label: {
type: ParseTypesEnum.string,
},
width: {
type: ParseTypesEnum.string,
// length_or_percentage_or_unitless,
Expand Down
3 changes: 2 additions & 1 deletion packages/myst-directives/src/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export const imageDirective: DirectiveSpec = {
required: true,
},
options: {
// name: {
// label: {
// type: ParseTypesEnum.string,
// alias: ['name'],
// },
class: {
type: ParseTypesEnum.string,
Expand Down
1 change: 1 addition & 0 deletions packages/myst-directives/src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const mathDirective: DirectiveSpec = {
options: {
label: {
type: ParseTypesEnum.string,
alias: ['name'],
},
},
body: {
Expand Down
5 changes: 3 additions & 2 deletions packages/myst-directives/src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export const listTableDirective: DirectiveSpec = {
type: ParseTypesEnum.parsed,
},
options: {
name: {
label: {
type: ParseTypesEnum.string,
alias: ['name'],
},
'header-rows': {
type: ParseTypesEnum.number,
Expand Down Expand Up @@ -92,7 +93,7 @@ export const listTableDirective: DirectiveSpec = {
}),
};
children.push(table);
const { label, identifier } = normalizeLabel(data.options?.name as string | undefined) || {};
const { label, identifier } = normalizeLabel(data.options?.label as string | undefined) || {};
const container = {
type: 'container',
kind: 'table',
Expand Down
23 changes: 22 additions & 1 deletion packages/myst-parser/src/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,29 @@ export function applyDirectives(tree: GenericParent, specs: DirectiveSpec[], vfi
optionNodeLookup[optionNode.name] = optionNode;
}
});

// Deal with each option in the spec
Object.entries(optionsSpec || {}).forEach(([optionName, optionSpec]) => {
const optionNode = optionNodeLookup[optionName];
let optionNameUsed = optionName;
let optionNode = optionNodeLookup[optionName];
// Replace alias options or warn on duplicates
optionSpec.alias?.forEach((alias) => {
const aliasNode = optionNodeLookup[alias];
if (!aliasNode) return;
if (!optionNode && aliasNode) {
optionNode = aliasNode;
optionNameUsed = alias;
optionNodeLookup[optionName] = optionNode;
} else {
fileWarn(
vfile,
`option "${optionNameUsed}" used instead of "${alias}" for directive: ${name}`,
{ node },
);
}
delete optionNodeLookup[alias];
});

if (optionSpec.required && !optionNode) {
fileError(vfile, `required option "${optionName}" not provided for directive: ${name}`, {
node,
Expand Down
11 changes: 9 additions & 2 deletions packages/myst-parser/tests/directives/directives.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import path from 'node:path';
import yaml from 'js-yaml';
import { selectAll } from 'unist-util-select';
import { mystParse } from '../../src';
import { VFile } from 'vfile';

type TestCase = {
title: string;
markdown: string;
mdast: Record<string, any>;
warnings?: number;
};

type TestCases = {
Expand All @@ -28,13 +30,18 @@ casesList.forEach(({ title, cases }) => {
describe(title, () => {
test.each(cases.map((c): [string, TestCase] => [c.title, c]))(
'%s',
(_, { markdown, mdast }) => {
const output = mystParse(markdown);
(_, { markdown, mdast, warnings = 0 }) => {
const vfile = new VFile();
const output = mystParse(markdown, { vfile });
// Dont worry about position
selectAll('[position]', output).forEach((node) => {
delete node.position;
});
expect(output).toEqual(mdast);
if (vfile.messages.length !== warnings) {
console.log(vfile.messages);
}
expect(vfile.messages.length).toBe(warnings);
},
);
});
Expand Down
68 changes: 68 additions & 0 deletions packages/myst-parser/tests/directives/label-alias.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
title: label alias tests
cases:
- title: figure label works
markdown: |-
```{figure} my_image.png
:label: my-fig
```
mdast:
type: root
children:
- type: mystDirective
name: figure
args: my_image.png
options:
label: my-fig
children:
- type: container
kind: figure
identifier: my-fig
label: my-fig
children:
- type: image
url: my_image.png
- title: figure name works
markdown: |-
```{figure} my_image.png
:name: my-fig
```
mdast:
type: root
children:
- type: mystDirective
name: figure
args: my_image.png
options:
name: my-fig
children:
- type: container
kind: figure
identifier: my-fig
label: my-fig
children:
- type: image
url: my_image.png
- title: figure name/label warns and takes label
warnings: 1
markdown: |-
```{figure} my_image.png
:label: my-fig
:name: not-used
```
mdast:
type: root
children:
- type: mystDirective
name: figure
args: my_image.png
options:
label: my-fig
name: not-used
children:
- type: container
kind: figure
identifier: my-fig
label: my-fig
children:
- type: image
url: my_image.png

0 comments on commit ed7b430

Please sign in to comment.