Skip to content

Commit

Permalink
feat: more directives
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Nov 19, 2024
1 parent 0400744 commit c245b95
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 59 deletions.
14 changes: 8 additions & 6 deletions packages/myst-directives/src/bibliography.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import type { DirectiveSpec, DirectiveData, GenericNode } from 'myst-common';
import { addCommonDirectiveOptions, commonDirectiveOptions } from './utils.js';

export const bibliographyDirective: DirectiveSpec = {
name: 'bibliography',
options: {
...commonDirectiveOptions('bibliography'),
filter: {
type: String,
},
},
run(data: DirectiveData): GenericNode[] {
return [
{
type: 'bibliography',
filter: data.options?.filter,
},
];
const bibliography = {
type: 'bibliography',
filter: data.options?.filter,
};
addCommonDirectiveOptions(data, bibliography);
return [bibliography];
},
};
18 changes: 10 additions & 8 deletions packages/myst-directives/src/embed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { DirectiveSpec, DirectiveData } from 'myst-common';
import { normalizeLabel } from 'myst-common';
import type { Embed } from 'myst-spec-ext';
import { commonDirectiveOptions, addCommonDirectiveOptions } from './utils.js';

export const embedDirective: DirectiveSpec = {
name: 'embed',
Expand All @@ -11,6 +12,7 @@ export const embedDirective: DirectiveSpec = {
required: true,
},
options: {
...commonDirectiveOptions('embed'),
'remove-input': {
type: Boolean,
doc: 'If embedding a Jupyter Notebook cell, remove the input of the cell.',
Expand All @@ -26,13 +28,13 @@ export const embedDirective: DirectiveSpec = {
const arg = argString.startsWith('#') ? argString.substring(1) : argString;
const { label } = normalizeLabel(arg) || {};
if (!label) return [];
return [
{
type: 'embed',
source: { label },
'remove-input': data.options?.['remove-input'] as boolean | undefined,
'remove-output': data.options?.['remove-output'] as boolean | undefined,
},
];
const embed: Embed = {
type: 'embed',
source: { label },
'remove-input': data.options?.['remove-input'] as boolean | undefined,
'remove-output': data.options?.['remove-output'] as boolean | undefined,
};
addCommonDirectiveOptions(data, embed);
return [embed];
},
};
16 changes: 10 additions & 6 deletions packages/myst-directives/src/glossary.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import type { DirectiveSpec, DirectiveData, GenericNode } from 'myst-common';
import { addCommonDirectiveOptions, commonDirectiveOptions } from './utils.js';

export const glossaryDirective: DirectiveSpec = {
name: 'glossary',
body: {
type: 'myst',
required: true,
},
options: {
...commonDirectiveOptions('glossary'),
},
run(data: DirectiveData): GenericNode[] {
return [
{
type: 'glossary',
children: data.body as GenericNode[],
},
];
const glossary = {
type: 'glossary',
children: data.body as GenericNode[],
};
addCommonDirectiveOptions(data, glossary);
return [glossary];
},
};
45 changes: 19 additions & 26 deletions packages/myst-directives/src/include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CODE_DIRECTIVE_OPTIONS, getCodeBlockOptions } from './code.js';
import type { Include } from 'myst-spec-ext';
import type { VFile } from 'vfile';
import { select } from 'unist-util-select';
import { addCommonDirectiveOptions, commonDirectiveOptions } from './utils.js';

/**
* RST documentation:
Expand All @@ -22,10 +23,7 @@ export const includeDirective: DirectiveSpec = {
required: true,
},
options: {
label: {
type: String,
alias: ['name'],
},
...commonDirectiveOptions('include'),
literal: {
type: Boolean,
doc: 'Flag the include block as literal, and show the contents as a code block. This can also be set automatically by setting the `language` or using the `literalinclude` directive.',
Expand Down Expand Up @@ -70,21 +68,18 @@ export const includeDirective: DirectiveSpec = {
},
},
run(data, vfile): Include[] {
const { label, identifier } = normalizeLabel(data.options?.label as string | undefined) || {};
const literal =
data.name === 'literalinclude' || !!data.options?.literal || !!data.options?.lang;

const file = data.arg as string;
if (!literal) {
// TODO: warn on unused options
return [
{
type: 'include',
file,
label,
identifier,
},
];
const include: Include = {
type: 'include',
file,
};
addCommonDirectiveOptions(data, include);
return [include];
}
const lang = (data.options?.lang as string) ?? extToLanguage(file.split('.').pop());
const opts = getCodeBlockOptions(
Expand Down Expand Up @@ -122,19 +117,17 @@ export const includeDirective: DirectiveSpec = {
];
}
}
return [
{
type: 'include',
file,
literal,
lang,
label,
identifier,
caption: data.options?.caption as any[],
filter: Object.keys(filter).length > 0 ? filter : undefined,
...opts,
},
];
const include: Include = {
type: 'include',
file,
literal,
lang,
caption: data.options?.caption as any[],
filter: Object.keys(filter).length > 0 ? filter : undefined,
...opts,
};
addCommonDirectiveOptions(data, include);
return [include];
},
};

Expand Down
16 changes: 10 additions & 6 deletions packages/myst-directives/src/mdast.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import type { DirectiveSpec, DirectiveData, GenericNode } from 'myst-common';
import { addCommonDirectiveOptions, commonDirectiveOptions } from './utils.js';

export const mdastDirective: DirectiveSpec = {
name: 'mdast',
arg: {
type: String,
required: true,
},
options: {
...commonDirectiveOptions('mdast'),
},
run(data: DirectiveData): GenericNode[] {
return [
{
type: 'mdast',
id: data.arg as string,
},
];
const mdast = {
type: 'mdast',
id: data.arg as string,
};
addCommonDirectiveOptions(data, mdast);
return [mdast];
},
};
16 changes: 9 additions & 7 deletions packages/myst-directives/src/mystdemo.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import yaml from 'js-yaml';
import type { DirectiveSpec, DirectiveData, GenericNode } from 'myst-common';
import { addCommonDirectiveOptions, commonDirectiveOptions } from './utils.js';

export const mystdemoDirective: DirectiveSpec = {
name: 'myst',
options: {
...commonDirectiveOptions('myst'),
numbering: {
type: String,
},
Expand All @@ -21,12 +23,12 @@ export const mystdemoDirective: DirectiveSpec = {
//pass
}
}
return [
{
type: 'myst',
numbering,
value: data.body as string,
},
];
const myst = {
type: 'myst',
numbering,
value: data.body as string,
};
addCommonDirectiveOptions(data, myst);
return [myst];
},
};

0 comments on commit c245b95

Please sign in to comment.