Skip to content

Commit

Permalink
fix: add label and identifier information to outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Jan 20, 2025
1 parent 7cf22f4 commit 130a9ae
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
1 change: 0 additions & 1 deletion packages/myst-compat/src/downgrade/version_2_1.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe, expect, it } from 'vitest';
import { downgrade } from './version_2_1.js';
import type { Parent } from 'mdast';
import { warn } from 'console';

const SIMPLE_AST: Parent = {
type: 'root',
Expand Down
1 change: 1 addition & 0 deletions packages/myst-compat/src/upgrade/version_1_2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { visit, SKIP } from 'unist-util-visit';
import { squeeze } from '../utils.js';

export function upgrade(ast: Parent) {
// Walk over `output` nodes, assuming there are no more than one per block
visit(ast as any, 'output', (node: Output1, index: number | null, parent: Parent | null) => {
// We can only correlate output children with the IOutput objects if there's only one IOutput
// Additionally, there may be placeholders that need to be removed
Expand Down
11 changes: 6 additions & 5 deletions packages/myst-spec-ext/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,12 @@ export type Container = Omit<SpecContainer, 'kind'> & {
parentEnumerator?: string;
};

export type Output = Node & {
type: 'output';
children: (FlowContent | ListContent | PhrasingContent)[];
jupyter_data: any; // TODO: set this to IOutput
};
export type Output = Node &
Target & {
type: 'output';
children: (FlowContent | ListContent | PhrasingContent)[];
jupyter_data: any; // TODO: set this to IOutput
};

export type Outputs = Node &
Target & {
Expand Down
12 changes: 8 additions & 4 deletions packages/myst-transforms/src/blocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ describe('Test blockMetadataTransform', () => {
test('label is propagated to outputs', async () => {
const mdast = u('root', [
u('block', { meta: '{"label": "My_Label", "key": "value"}' }, [
u('output', 'We know what we are'),
u('output', 'but know not what we may be.'),
u('outputs', [
u('output', 'We know what we are'),
u('output', 'but know not what we may be.'),
]),
]),
]) as any;
blockMetadataTransform(mdast, new VFile());
Expand All @@ -136,8 +138,10 @@ describe('Test blockMetadataTransform', () => {
data: { key: 'value' },
},
[
u('output', { identifier: 'my_label-output-0' }, 'We know what we are'),
u('output', { identifier: 'my_label-output-1' }, 'but know not what we may be.'),
u('outputs', { identifier: 'my_label-output' }, [
u('output', { identifier: 'my_label-output-0' }, 'We know what we are'),
u('output', { identifier: 'my_label-output-1' }, 'but know not what we may be.'),
]),
],
),
]),
Expand Down
8 changes: 8 additions & 0 deletions packages/myst-transforms/src/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ export function blockMetadataTransform(mdast: GenericParent, file: VFile) {
});
const outputsNode = select('outputs', block) as GenericNode | undefined;
if (outputsNode && !outputsNode.identifier) {
// Label outputs node
outputsNode.identifier = `${block.identifier}-output`;
// Enumerate outputs
const outputs = selectAll('output', outputsNode) as GenericNode[];
outputs.forEach((outputNode, index) => {
if (outputNode && !outputNode.identifier) {
outputNode.identifier = `${block.identifier}-output-${index}`;
}
});
}
}
});
Expand Down

0 comments on commit 130a9ae

Please sign in to comment.