Skip to content

Commit

Permalink
feat!: add new outputs node
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Nov 25, 2024
1 parent b589574 commit e278262
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 266 deletions.
2 changes: 2 additions & 0 deletions packages/myst-cli/src/process/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import {
transformFilterOutputStreams,
transformLiftCodeBlocksInJupytext,
transformMystXRefs,
internalASTToExternal,
} from '../transforms/index.js';
import type { ImageExtensions } from '../utils/resolveExtension.js';
import { logMessagesFromVFile } from '../utils/logging.js';
Expand Down Expand Up @@ -431,5 +432,6 @@ export async function finalizeMdast(
postData.widgets = cache.$getMdast(file)?.pre.widgets;
updateFileInfoFromFrontmatter(session, file, frontmatter);
}
internalASTToExternal(mdast);
logMessagesFromVFile(session, vfile);
}
21 changes: 12 additions & 9 deletions packages/myst-cli/src/process/notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,20 @@ export async function processNotebookFull(
value: ensureString(cell.source),
};

// Embed outputs in an output block
const output: { type: 'output'; id: string; data: IOutput[] } = {
type: 'output',
const outputsChildren = (cell.outputs as IOutput[]).map((output) => {
// Embed outputs in an output block
const result = {
type: 'output',
jupyter_data: output,
};
return result;
});
const outputs = {
type: 'outputs',
id: nanoid(),
data: [],
children: outputsChildren,
};

if (cell.outputs && (cell.outputs as IOutput[]).length > 0) {
output.data = cell.outputs as IOutput[];
}
return acc.concat(blockParent(cell, [code, output]));
return acc.concat(blockParent(cell, [code, outputs]));
}
return acc;
},
Expand Down
13 changes: 12 additions & 1 deletion packages/myst-cli/src/transforms/crossReferences.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { VFile } from 'vfile';
import { selectAll } from 'unist-util-select';
import { visit, SKIP } from 'unist-util-visit';
import type { FrontmatterParts, GenericNode, GenericParent, References } from 'myst-common';
import { RuleId, fileWarn, plural, selectMdastNodes } from 'myst-common';
import { computeHash, tic } from 'myst-cli-utils';
Expand All @@ -9,6 +10,8 @@ import type { CrossReference, Dependency, Link, SourceFileKind } from 'myst-spec
import type { ISession } from '../session/types.js';
import { loadFromCache, writeToCache } from '../session/cache.js';
import type { SiteAction, SiteExport } from 'myst-config';
import type { IOutput } from '@jupyterlab/nbformat';
import { externalASTToInternal } from './schema.js';

export const XREF_MAX_AGE = 1; // in days

Expand All @@ -32,6 +35,13 @@ export type MystData = {
references?: References;
};

function upgradeAndDowngradeMystData(data: MystData): MystData {
if (data.mdast) {
externalASTToInternal(data.mdast);
}
return data;
}

async function fetchMystData(
session: ISession,
dataUrl: string | undefined,
Expand All @@ -48,7 +58,8 @@ async function fetchMystData(
try {
const resp = await session.fetch(dataUrl);
if (resp.ok) {
const data = (await resp.json()) as MystData;
const data = upgradeAndDowngradeMystData((await resp.json()) as MystData);

writeToCache(session, filename, JSON.stringify(data));
return data;
}
Expand Down
1 change: 1 addition & 0 deletions packages/myst-cli/src/transforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export * from './include.js';
export * from './links.js';
export * from './mdast.js';
export * from './outputs.js';
export * from './schema.js';
export * from './inlineExpressions.js';
export * from './types.js';
49 changes: 31 additions & 18 deletions packages/myst-cli/src/transforms/outputs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ describe('reduceOutputs', () => {
],
},
{
type: 'output',
id: 'abc123',
data: [],
type: 'outputs',
children: [
{
type: 'output',
id: 'abc123',
jupyter_data: null,
},
],
},
],
},
Expand All @@ -49,18 +54,21 @@ describe('reduceOutputs', () => {
],
},
{
type: 'output',
type: 'outputs',
id: 'abc123',
data: [
children: [
{
output_type: 'display_data',
execution_count: 3,
metadata: {},
data: {
'application/octet-stream': {
content_type: 'application/octet-stream',
hash: 'def456',
path: '/my/path/def456.png',
type: 'output',
jupyter_data: {
output_type: 'display_data',
execution_count: 3,
metadata: {},
data: {
'application/octet-stream': {
content_type: 'application/octet-stream',
hash: 'def456',
path: '/my/path/def456.png',
},
},
},
},
Expand Down Expand Up @@ -91,14 +99,19 @@ describe('reduceOutputs', () => {
],
},
{
type: 'output',
type: 'outputs',
id: 'abc123',
data: [],
children: [
{
type: 'image',
placeholder: true,
url: 'placeholder.png',
type: 'output',
jupyter_data: null,
children: [
{
type: 'image',
placeholder: true,
url: 'placeholder.png',
},
],
},
],
},
Expand Down
Loading

0 comments on commit e278262

Please sign in to comment.