Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏷 Propagate block metatags to code node and output node #407

Merged
merged 10 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/myst-cli/src/process/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
transformThumbnail,
StaticFileTransformer,
inlineExpressionsPlugin,
propagateBlockDataToCode,
} from '../transforms';
import type { ImageExtensions } from '../utils';
import { logMessagesFromVFile } from '../utils';
Expand Down Expand Up @@ -140,6 +141,9 @@ export async function transformMdast(
.use(joinGatesPlugin)
.run(mdast, vfile);

// This needs to come after basic transformations since meta tags are added there
propagateBlockDataToCode(session, file, mdast);

// Run the link transformations that can be done without knowledge of other files
const intersphinx = projectPath ? await loadIntersphinx(session, { projectPath }) : [];
const transformers = [
Expand Down
64 changes: 63 additions & 1 deletion packages/myst-cli/src/transforms/code.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Session } from '../session';
import { liftCodeMetadataToBlock, metadataFromCode } from './code';
import { liftCodeMetadataToBlock, metadataFromCode, propagateBlockDataToCode } from './code';

describe('metadataFromCode', () => {
it('empty code returns self', async () => {
Expand Down Expand Up @@ -114,3 +114,65 @@ describe('liftCodeMetadataToBlock', () => {
expect(mdast.children[0].children[1].value).toEqual('print("hello world2")');
});
});

function build_mdast_by_tags(tags: string[]) {
const mdast: any = {
type: 'root',
children: [
{
type: 'block',
children: [
{
type: 'code',
},
{
type: 'output',
},
],
data: {
tags: tags,
},
},
],
};
return mdast;
}

describe('propagateBlockDataToCode', () => {
it('single tag propagation', async () => {
for (const action of ['hide', 'remove']) {
for (const target of ['cell', 'input', 'output']) {
const tag = `${action}-${target}`;
const mdast = build_mdast_by_tags([tag]);
propagateBlockDataToCode(new Session(), '', mdast);
let result = '';
switch (target) {
case 'cell':
result = mdast.children[0].visibility;
break;
case 'input':
result = mdast.children[0].children[0].visibility;
break;
case 'output':
result = mdast.children[0].children[1].visibility;
break;
}
expect(result).toEqual(action);
}
}
});
it('multi tags propagation', async () => {
for (const action of [`hide`, `remove`]) {
const tags = [`${action}-cell`, `${action}-input`, `${action}-output`];
const mdast = build_mdast_by_tags(tags);
propagateBlockDataToCode(new Session(), '', mdast);
for (const node of [
mdast.children[0],
mdast.children[0].children[0],
mdast.children[0].children[1],
]) {
expect(node.visibility).toEqual(action);
}
}
});
});
47 changes: 46 additions & 1 deletion packages/myst-cli/src/transforms/code.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Root } from 'mdast';
import type { GenericNode } from 'myst-common';
import { selectAll } from 'unist-util-select';
import { select, selectAll } from 'unist-util-select';
import yaml from 'js-yaml';
import type { ISession } from '../session/types';

Expand Down Expand Up @@ -83,3 +83,48 @@ export function liftCodeMetadataToBlock(session: ISession, filename: string, mda
}
});
}

/**
* Traverse mdast, propagate block tags to code and output
*/
export function propagateBlockDataToCode(session: ISession, filename: string, mdast: Root) {
const blocks = selectAll('block', mdast) as GenericNode[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is possible that this might grab too many blocks (e.g. the markdown ones, which aren't going to have the code/output). I think we put a kind or something on the block, not sure where that is at the moment!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is changed, it's reasonable to change the liftCodeMetadataToBlock with the same way. This worth a separate pr to do since two tests will be added and their functionality do not have direct link to the meta tag implementation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good -- let me know when you are a bit further on the feature and I can go in and test against a few other projects!

blocks.forEach((block) => {
if (!block.data || !block.data.tags) return;
if (!Array.isArray(block.data.tags)) {
session.log.error(`tags in code-cell directive must be a list of strings`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am tweaking this a bit to pass in the node and file information for better console tracking:

image

}
// only single ('code', 'output') pair?
const codeNode = select('code', block) as GenericNode;
const outputNode = select('output', block) as GenericNode;
block.data.tags.forEach((tag: string) => {
switch (tag) {
// should we raise when hide and remove both exist?
case 'hide-cell':
block.visibility = 'hide';
break;
case 'remove-cell':
block.visibility = 'remove';
break;
case 'hide-input':
codeNode.visibility = 'hide';
break;
case 'remove-input':
codeNode.visibility = 'remove';
break;
case 'hide-output':
outputNode.visibility = 'hide';
break;
case 'remove-output':
outputNode.visibility = 'remove';
break;
default:
session.log.warn(`tag '${tag}' is not valid in code-cell tags'`);
}
});
if (!block.visibility) block.visibility = 'show';
if (!codeNode.visibility) codeNode.visibility = 'show';
if (!outputNode.visibility) outputNode.visibility = 'show';
delete block.data.tags;
});
}
1 change: 1 addition & 0 deletions packages/myst-spec-ext/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export type Admonition = SpecAdmonition & {

export type Code = SpecCode & {
executable?: boolean;
visibility?: 'show' | 'hide' | 'remove';
};

export type ListItem = SpecListItem & {
Expand Down