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

🪓 Split Output into Outputs → Output[] #1671

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5f58972
feat!: add new outputs node
agoose77 Nov 25, 2024
b257932
fix: restore hidden node handling
agoose77 Jan 14, 2025
bd0ee78
refactor: store future AST as actual object
agoose77 Jan 14, 2025
b659d79
fix: update external AST too
agoose77 Jan 14, 2025
0a91516
refactor: just filter directly
agoose77 Jan 14, 2025
82a2c0d
refactor: directly use output
agoose77 Jan 14, 2025
64d8d31
chore: drop note
agoose77 Jan 14, 2025
7ef0e1e
refactor: don't return if mutating in place
agoose77 Jan 15, 2025
200ab98
wip: initial myst-compat package
agoose77 Jan 15, 2025
210c97d
wip: ast up and downgrading
agoose77 Jan 15, 2025
51df4a0
test: add simple test
agoose77 Jan 15, 2025
bc8b7e1
chore: run linter
agoose77 Jan 15, 2025
3ead27f
fix: get tests working
agoose77 Jan 16, 2025
13237e1
fix: try to copy children where there is only one "output" object
agoose77 Jan 16, 2025
5544d9e
fix: allow missing children
agoose77 Jan 16, 2025
7d8af6e
fix: improve error handling
agoose77 Jan 16, 2025
af58582
chore: add changeset
agoose77 Jan 16, 2025
ba7e162
fix: improve types, support placeholders
agoose77 Jan 16, 2025
af18001
fix: only apply identifier to single outputs
agoose77 Jan 16, 2025
f902776
fix: always set children
agoose77 Jan 16, 2025
95a0a69
refactor: rename outputNode, assert always children
agoose77 Jan 16, 2025
3f4e3c3
docs: remove comments
agoose77 Jan 16, 2025
7955362
docs: more comments
agoose77 Jan 20, 2025
7cf22f4
refactor: remove unneeded guard
agoose77 Jan 20, 2025
130a9ae
fix: add label and identifier information to outputs
agoose77 Jan 20, 2025
90ac03d
fix: tag propagation
agoose77 Jan 20, 2025
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
10 changes: 10 additions & 0 deletions .changeset/flat-pumas-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"myst-spec-ext": minor
"mystmd": minor
"myst-directives": patch
"myst-execute": patch
"myst-compat": patch
"myst-cli": patch
---

Add support for new Outputs node
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/myst-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"mime-types": "^2.1.35",
"myst-cli-utils": "^2.0.11",
"myst-common": "^1.7.6",
"myst-compat": "^0.0.0",
"myst-config": "^1.7.6",
"myst-execute": "^0.1.2",
"myst-ext-card": "^1.0.9",
Expand Down
34 changes: 30 additions & 4 deletions packages/myst-cli/src/process/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@ import type { GenericParent } from 'myst-common';
import { RuleId, toText, fileError, fileWarn } from 'myst-common';
import type { PageFrontmatter } from 'myst-frontmatter';
import { validatePageFrontmatter, fillProjectFrontmatter } from 'myst-frontmatter';
import { SourceFileKind } from 'myst-spec-ext';
import { SourceFileKind, VERSION } from 'myst-spec-ext';
import { frontmatterValidationOpts, getPageFrontmatter } from '../frontmatter.js';
import type { ISession, ISessionWithCache } from '../session/types.js';
import { castSession } from '../session/cache.js';
import { config, projects, warnings, watch } from '../store/reducers.js';
import type { PreRendererData, RendererData } from '../transforms/types.js';
import { makeCompatible } from 'myst-compat';
import { logMessagesFromVFile } from '../utils/logging.js';
import { isValidFile, parseFilePath } from '../utils/resolveExtension.js';
import { addWarningForFile } from '../utils/addWarningForFile.js';
import { loadBibTeXCitationRenderers } from './citations.js';
import { parseMyst } from './myst.js';
import { processNotebookFull } from './notebook.js';
import { selectors } from '../store/index.js';
import { defined, incrementOptions, validateObjectKeys, validateEnum } from 'simple-validators';
import {
defined,
incrementOptions,
validateObjectKeys,
validateEnum,
validateString,
} from 'simple-validators';
import type { ValidationOptions } from 'simple-validators';

type LoadFileOptions = {
Expand Down Expand Up @@ -144,10 +151,12 @@ export function mystJSONValidationOpts(
function validateMySTJSON(
input: any,
opts: ValidationOptions,
): { mdast: GenericParent; kind: SourceFileKind; frontmatter: PageFrontmatter } | undefined {
):
| { mdast: GenericParent; kind: SourceFileKind; frontmatter: PageFrontmatter; version: string }
| undefined {
const value = validateObjectKeys(
input,
{ required: ['mdast'], optional: ['kind', 'frontmatter'] },
{ required: ['mdast'], optional: ['kind', 'frontmatter', 'version'] },
opts,
);
if (value === undefined) {
Expand All @@ -156,22 +165,39 @@ function validateMySTJSON(

const { mdast } = value;

// Check kind
let kind: undefined | SourceFileKind;
if (defined(value.kind)) {
kind = validateEnum<SourceFileKind>(value.kind, {
...incrementOptions('kind', opts),
enum: SourceFileKind,
});
}

// Check frontmatter
let frontmatter: undefined | PageFrontmatter;
if (defined(value.frontmatter)) {
frontmatter = validatePageFrontmatter(value.frontmatter, incrementOptions('frontmatter', opts));
}

let version: undefined | string;
if (defined(value.version)) {
version = validateString(value.version, incrementOptions('version', opts));
// Need a valid version if given
if (version === undefined) {
return undefined;
}
}
version = version ?? '1';

// Ingest "legacy" mdast and upgrade it
makeCompatible(version, '2', mdast);

return {
mdast,
kind: kind ?? SourceFileKind.Article,
frontmatter: frontmatter ?? {},
version: version,
};
}

Expand Down
8 changes: 7 additions & 1 deletion packages/myst-cli/src/process/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { tic } from 'myst-cli-utils';
import type { GenericParent, IExpressionResult, PluginUtils, References } from 'myst-common';
import { fileError, fileWarn, RuleId, slugToUrl } from 'myst-common';
import type { PageFrontmatter } from 'myst-frontmatter';
import { SourceFileKind } from 'myst-spec-ext';
import { SourceFileKind, VERSION } from 'myst-spec-ext';
import type { LinkTransformer } from 'myst-transforms';
import { downgrade as downgradeAST } from 'myst-compat';
import {
basicTransformationsPlugin,
htmlPlugin,
Expand Down Expand Up @@ -273,6 +274,7 @@ export async function transformMdast(
mdast,
references,
widgets,
version: VERSION,
} as any;
const cachedMdast = cache.$getMdast(file);
if (cachedMdast) cachedMdast.post = data;
Expand Down Expand Up @@ -429,5 +431,9 @@ export async function finalizeMdast(
postData.widgets = cache.$getMdast(file)?.pre.widgets;
updateFileInfoFromFrontmatter(session, file, frontmatter);
}
// TODO[ast-downgrade]: remove this once AST downgrade support is widespread
// i.e. once VERSION from myst-spec-ext is 2
downgradeAST('2', '1', mdast as any);

logMessagesFromVFile(session, vfile);
}
22 changes: 13 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,21 @@ 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,
children: [],
};
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
32 changes: 19 additions & 13 deletions packages/myst-cli/src/transforms/code.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ function build_mdast(tags: string[], has_output: boolean) {
],
};
if (has_output) {
mdast.children[0].children.push({ type: 'output' });
mdast.children[0].children.push({
type: 'outputs',
children: [{ type: 'output', children: [] }],
});
}
return mdast;
}
Expand Down Expand Up @@ -261,7 +264,7 @@ describe('propagateBlockDataToCode', () => {
const mdast = build_mdast([tag], has_output);
propagateBlockDataToCode(new Session(), new VFile(), mdast);
let result = '';
const outputNode = mdast.children[0].children[1];
const outputsNode = mdast.children[0].children[1];
switch (target) {
case 'cell':
result = mdast.children[0].visibility;
Expand All @@ -270,12 +273,14 @@ describe('propagateBlockDataToCode', () => {
result = mdast.children[0].children[0].visibility;
break;
case 'output':
if (!has_output && target == 'output') {
expect(outputNode).toEqual(undefined);
if (!has_output) {
expect(outputsNode).toEqual(undefined);
continue;
}
result = outputNode.visibility;
result = outputsNode.visibility;
break;
default:
throw new Error();
}
expect(result).toEqual(action);
}
Expand All @@ -290,13 +295,13 @@ describe('propagateBlockDataToCode', () => {
propagateBlockDataToCode(new Session(), new VFile(), mdast);
const blockNode = mdast.children[0];
const codeNode = mdast.children[0].children[0];
const outputNode = mdast.children[0].children[1];
const outputsNode = mdast.children[0].children[1];
expect(blockNode.visibility).toEqual(action);
expect(codeNode.visibility).toEqual(action);
if (has_output) {
expect(outputNode.visibility).toEqual(action);
expect(outputsNode.visibility).toEqual(action);
} else {
expect(outputNode).toEqual(undefined);
expect(outputsNode).toEqual(undefined);
}
}
}
Expand All @@ -313,7 +318,8 @@ describe('propagateBlockDataToCode', () => {
executable: true,
},
{
type: 'output',
type: 'outputs',
children: [],
},
],
data: {
Expand All @@ -323,10 +329,10 @@ describe('propagateBlockDataToCode', () => {
],
};
propagateBlockDataToCode(new Session(), new VFile(), mdast);
const outputNode = mdast.children[0].children[1];
expect(outputNode.children?.length).toEqual(1);
expect(outputNode.children[0].type).toEqual('image');
expect(outputNode.children[0].placeholder).toBeTruthy();
const outputsNode = mdast.children[0].children[1];
expect(outputsNode.children?.length).toEqual(1);
expect(outputsNode.children[0].type).toEqual('image');
expect(outputsNode.children[0].placeholder).toBeTruthy();
});
it('placeholder passes with no output', async () => {
const mdast: any = {
Expand Down
17 changes: 8 additions & 9 deletions packages/myst-cli/src/transforms/code.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { GenericNode, GenericParent } from 'myst-common';
import { NotebookCellTags, RuleId, fileError, fileWarn } from 'myst-common';
import type { Image, Output } from 'myst-spec-ext';
import type { Image, Outputs } from 'myst-spec-ext';
import { select, selectAll } from 'unist-util-select';
import yaml from 'js-yaml';
import type { VFile } from 'vfile';
Expand Down Expand Up @@ -156,10 +156,9 @@ export function propagateBlockDataToCode(session: ISession, vfile: VFile, mdast:
const blocks = selectAll('block', mdast) as GenericNode[];
blocks.forEach((block) => {
if (!block.data) return;
const outputNode = select('output', block) as Output | null;
if (block.data.placeholder && outputNode) {
if (!outputNode.children) outputNode.children = [];
outputNode.children.push({
const outputsNode = select('outputs', block) as Outputs | null;
if (block.data.placeholder && outputsNode) {
outputsNode.children.push({
type: 'image',
placeholder: true,
url: block.data.placeholder as string,
Expand Down Expand Up @@ -195,18 +194,18 @@ export function propagateBlockDataToCode(session: ISession, vfile: VFile, mdast:
if (codeNode) codeNode.visibility = 'remove';
break;
case NotebookCellTags.hideOutput:
if (outputNode) outputNode.visibility = 'hide';
if (outputsNode) outputsNode.visibility = 'hide';
break;
case NotebookCellTags.removeOutput:
if (outputNode) outputNode.visibility = 'remove';
if (outputsNode) outputsNode.visibility = 'remove';
break;
default:
session.log.debug(`tag '${tag}' is not valid in code-cell tags'`);
}
});
if (!block.visibility) block.visibility = 'show';
if (codeNode && !codeNode.visibility) codeNode.visibility = 'show';
if (outputNode && !outputNode.visibility) outputNode.visibility = 'show';
if (outputsNode && !outputsNode.visibility) outputsNode.visibility = 'show';
});
}

Expand All @@ -233,7 +232,7 @@ export function transformLiftCodeBlocksInJupytext(mdast: GenericParent) {
child.type === 'block' &&
child.children?.length === 2 &&
child.children?.[0].type === 'code' &&
child.children?.[1].type === 'output'
child.children?.[1].type === 'outputs'
) {
newBlocks.push(child as GenericParent);
newBlocks.push({ type: 'block', children: [] });
Expand Down
Loading
Loading