Skip to content

Commit

Permalink
feat!: add 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 11b86a0
Show file tree
Hide file tree
Showing 12 changed files with 324 additions and 267 deletions.
7 changes: 7 additions & 0 deletions packages/myst-cli/src/process/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ import {
transformImagesToDisk,
transformFilterOutputStreams,
transformLiftCodeBlocksInJupytext,
transformMarkdownOutputs,
transformMystXRefs,
internalASTToExternal,
} from '../transforms/index.js';
import type { ImageExtensions } from '../utils/resolveExtension.js';
import { logMessagesFromVFile } from '../utils/logging.js';
Expand Down Expand Up @@ -239,6 +241,9 @@ export async function transformMdast(
log: session.log,
});
}
await transformMarkdownOutputs(mdast, {
parser: (content: string) => parseMyst(session, content, file),
});
transformRenderInlineExpressions(mdast, vfile);
await transformOutputsToCache(session, mdast, kind, { minifyMaxCharacters });
transformFilterOutputStreams(mdast, vfile, frontmatter.settings);
Expand Down Expand Up @@ -380,6 +385,7 @@ export async function finalizeMdast(
) {
const vfile = new VFile(); // Collect errors on this file
vfile.path = file;

if (simplifyFigures) {
// Transform output nodes to images / text
reduceOutputs(session, mdast, file, imageWriteFolder, {
Expand Down Expand Up @@ -431,5 +437,6 @@ export async function finalizeMdast(
postData.widgets = cache.$getMdast(file)?.pre.widgets;
updateFileInfoFromFrontmatter(session, file, frontmatter);
}
internalASTToExternal(mdast);
logMessagesFromVFile(session, vfile);
}
23 changes: 13 additions & 10 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',
id: nanoid(),
data: [],
const outputsChildren = (cell.outputs as IOutput[]).map((output) => {
// Embed outputs in an output block
const result: { type: 'output'; id: string; jupyter_data: IOutput } = {
type: 'output',
id: nanoid(),
jupyter_data: output,
};
return result;
});
const outputs = {
type: 'outputs',
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 11b86a0

Please sign in to comment.