Skip to content

Commit ed9851c

Browse files
committed
feat:add outputs node
1 parent 15f6552 commit ed9851c

File tree

12 files changed

+321
-318
lines changed

12 files changed

+321
-318
lines changed

packages/myst-cli/src/process/mdast.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,5 +451,6 @@ export async function finalizeMdast(
451451
postData.widgets = cache.$getMdast(file)?.pre.widgets;
452452
updateFileInfoFromFrontmatter(session, file, frontmatter);
453453
}
454+
454455
logMessagesFromVFile(session, vfile);
455456
}

packages/myst-cli/src/process/notebook.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,21 @@ export async function processNotebookFull(
165165
value: ensureString(cell.source),
166166
};
167167

168-
// Embed outputs in an output block
169-
const output: { type: 'output'; id: string; data: IOutput[] } = {
170-
type: 'output',
168+
const outputsChildren = (cell.outputs as IOutput[]).map((output) => {
169+
// Embed outputs in an output block
170+
const result = {
171+
type: 'output',
172+
jupyter_data: output,
173+
children: [],
174+
};
175+
return result;
176+
});
177+
const outputs = {
178+
type: 'outputs',
171179
id: nanoid(),
172-
data: [],
180+
children: outputsChildren,
173181
};
174-
175-
if (cell.outputs && (cell.outputs as IOutput[]).length > 0) {
176-
output.data = cell.outputs as IOutput[];
177-
}
178-
return acc.concat(blockParent(cell, [code, output]));
182+
return acc.concat(blockParent(cell, [code, outputs]));
179183
}
180184
return acc;
181185
},

packages/myst-cli/src/transforms/code.spec.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ function build_mdast(tags: string[], has_output: boolean) {
162162
],
163163
};
164164
if (has_output) {
165-
mdast.children[0].children.push({ type: 'output' });
165+
mdast.children[0].children.push({
166+
type: 'outputs',
167+
children: [{ type: 'output', children: [] }],
168+
});
166169
}
167170
return mdast;
168171
}
@@ -261,7 +264,7 @@ describe('propagateBlockDataToCode', () => {
261264
const mdast = build_mdast([tag], has_output);
262265
propagateBlockDataToCode(new Session(), new VFile(), mdast);
263266
let result = '';
264-
const outputNode = mdast.children[0].children[1];
267+
const outputsNode = mdast.children[0].children[1];
265268
switch (target) {
266269
case 'cell':
267270
result = mdast.children[0].visibility;
@@ -270,12 +273,14 @@ describe('propagateBlockDataToCode', () => {
270273
result = mdast.children[0].children[0].visibility;
271274
break;
272275
case 'output':
273-
if (!has_output && target == 'output') {
274-
expect(outputNode).toEqual(undefined);
276+
if (!has_output) {
277+
expect(outputsNode).toEqual(undefined);
275278
continue;
276279
}
277-
result = outputNode.visibility;
280+
result = outputsNode.visibility;
278281
break;
282+
default:
283+
throw new Error();
279284
}
280285
expect(result).toEqual(action);
281286
}
@@ -290,13 +295,13 @@ describe('propagateBlockDataToCode', () => {
290295
propagateBlockDataToCode(new Session(), new VFile(), mdast);
291296
const blockNode = mdast.children[0];
292297
const codeNode = mdast.children[0].children[0];
293-
const outputNode = mdast.children[0].children[1];
298+
const outputsNode = mdast.children[0].children[1];
294299
expect(blockNode.visibility).toEqual(action);
295300
expect(codeNode.visibility).toEqual(action);
296301
if (has_output) {
297-
expect(outputNode.visibility).toEqual(action);
302+
expect(outputsNode.visibility).toEqual(action);
298303
} else {
299-
expect(outputNode).toEqual(undefined);
304+
expect(outputsNode).toEqual(undefined);
300305
}
301306
}
302307
}
@@ -313,7 +318,8 @@ describe('propagateBlockDataToCode', () => {
313318
executable: true,
314319
},
315320
{
316-
type: 'output',
321+
type: 'outputs',
322+
children: [],
317323
},
318324
],
319325
data: {
@@ -323,10 +329,10 @@ describe('propagateBlockDataToCode', () => {
323329
],
324330
};
325331
propagateBlockDataToCode(new Session(), new VFile(), mdast);
326-
const outputNode = mdast.children[0].children[1];
327-
expect(outputNode.children?.length).toEqual(1);
328-
expect(outputNode.children[0].type).toEqual('image');
329-
expect(outputNode.children[0].placeholder).toBeTruthy();
332+
const outputsNode = mdast.children[0].children[1];
333+
expect(outputsNode.children?.length).toEqual(1);
334+
expect(outputsNode.children[0].type).toEqual('image');
335+
expect(outputsNode.children[0].placeholder).toBeTruthy();
330336
});
331337
it('placeholder passes with no output', async () => {
332338
const mdast: any = {

packages/myst-cli/src/transforms/code.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { GenericNode, GenericParent } from 'myst-common';
22
import { NotebookCellTags, RuleId, fileError, fileWarn } from 'myst-common';
3-
import type { Image, Output } from 'myst-spec-ext';
3+
import type { Image, Outputs } from 'myst-spec-ext';
44
import { select, selectAll } from 'unist-util-select';
55
import yaml from 'js-yaml';
66
import type { VFile } from 'vfile';
@@ -156,10 +156,9 @@ export function propagateBlockDataToCode(session: ISession, vfile: VFile, mdast:
156156
const blocks = selectAll('block', mdast) as GenericNode[];
157157
blocks.forEach((block) => {
158158
if (!block.data) return;
159-
const outputNode = select('output', block) as Output | null;
160-
if (block.data.placeholder && outputNode) {
161-
if (!outputNode.children) outputNode.children = [];
162-
outputNode.children.push({
159+
const outputsNode = select('outputs', block) as Outputs | null;
160+
if (block.data.placeholder && outputsNode) {
161+
outputsNode.children.push({
163162
type: 'image',
164163
placeholder: true,
165164
url: block.data.placeholder as string,
@@ -195,18 +194,18 @@ export function propagateBlockDataToCode(session: ISession, vfile: VFile, mdast:
195194
if (codeNode) codeNode.visibility = 'remove';
196195
break;
197196
case NotebookCellTags.hideOutput:
198-
if (outputNode) outputNode.visibility = 'hide';
197+
if (outputsNode) outputsNode.visibility = 'hide';
199198
break;
200199
case NotebookCellTags.removeOutput:
201-
if (outputNode) outputNode.visibility = 'remove';
200+
if (outputsNode) outputsNode.visibility = 'remove';
202201
break;
203202
default:
204203
session.log.debug(`tag '${tag}' is not valid in code-cell tags'`);
205204
}
206205
});
207206
if (!block.visibility) block.visibility = 'show';
208207
if (codeNode && !codeNode.visibility) codeNode.visibility = 'show';
209-
if (outputNode && !outputNode.visibility) outputNode.visibility = 'show';
208+
if (outputsNode && !outputsNode.visibility) outputsNode.visibility = 'show';
210209
});
211210
}
212211

@@ -233,7 +232,7 @@ export function transformLiftCodeBlocksInJupytext(mdast: GenericParent) {
233232
child.type === 'block' &&
234233
child.children?.length === 2 &&
235234
child.children?.[0].type === 'code' &&
236-
child.children?.[1].type === 'output'
235+
child.children?.[1].type === 'outputs'
237236
) {
238237
newBlocks.push(child as GenericParent);
239238
newBlocks.push({ type: 'block', children: [] });

packages/myst-cli/src/transforms/outputs.spec.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ describe('reduceOutputs', () => {
2020
],
2121
},
2222
{
23-
type: 'output',
24-
id: 'abc123',
25-
data: [],
23+
type: 'outputs',
24+
children: [
25+
{
26+
type: 'output',
27+
id: 'abc123',
28+
jupyter_data: null,
29+
},
30+
],
2631
},
2732
],
2833
},
@@ -49,18 +54,21 @@ describe('reduceOutputs', () => {
4954
],
5055
},
5156
{
52-
type: 'output',
57+
type: 'outputs',
5358
id: 'abc123',
54-
data: [
59+
children: [
5560
{
56-
output_type: 'display_data',
57-
execution_count: 3,
58-
metadata: {},
59-
data: {
60-
'application/octet-stream': {
61-
content_type: 'application/octet-stream',
62-
hash: 'def456',
63-
path: '/my/path/def456.png',
61+
type: 'output',
62+
jupyter_data: {
63+
output_type: 'display_data',
64+
execution_count: 3,
65+
metadata: {},
66+
data: {
67+
'application/octet-stream': {
68+
content_type: 'application/octet-stream',
69+
hash: 'def456',
70+
path: '/my/path/def456.png',
71+
},
6472
},
6573
},
6674
},
@@ -91,14 +99,19 @@ describe('reduceOutputs', () => {
9199
],
92100
},
93101
{
94-
type: 'output',
102+
type: 'outputs',
95103
id: 'abc123',
96-
data: [],
97104
children: [
98105
{
99-
type: 'image',
100-
placeholder: true,
101-
url: 'placeholder.png',
106+
type: 'output',
107+
jupyter_data: null,
108+
children: [
109+
{
110+
type: 'image',
111+
placeholder: true,
112+
url: 'placeholder.png',
113+
},
114+
],
102115
},
103116
],
104117
},

0 commit comments

Comments
 (0)