Skip to content

Commit

Permalink
💦 building widget state (#1382)
Browse files Browse the repository at this point in the history
Co-authored-by: Rowan Cockett <rowanc1@gmail.com>
Co-authored-by: Franklin Koch <franklinwkoch@gmail.com>
  • Loading branch information
3 people authored Sep 24, 2024
1 parent d053269 commit 9f50eb8
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-peas-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"myst-cli": patch
---

💦 building widget state
9 changes: 7 additions & 2 deletions packages/myst-cli/src/process/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type LoadFileResult = {
mdast: GenericParent;
frontmatter?: PageFrontmatter;
identifiers?: string[];
widgets?: Record<string, any>;
};

function checkCache(cache: ISessionWithCache, content: string, file: string) {
Expand Down Expand Up @@ -69,7 +70,11 @@ export async function loadNotebookFile(
): Promise<LoadFileResult> {
const vfile = new VFile();
vfile.path = file;
const { mdast, frontmatter: nbFrontmatter } = await processNotebookFull(session, file, content);
const {
mdast,
frontmatter: nbFrontmatter,
widgets,
} = await processNotebookFull(session, file, content);
const { frontmatter: cellFrontmatter, identifiers } = getPageFrontmatter(
session,
mdast,
Expand All @@ -82,7 +87,7 @@ export async function loadNotebookFile(
nbFrontmatter,
frontmatterValidationOpts(vfile),
);
return { kind: SourceFileKind.Notebook, mdast, frontmatter, identifiers };
return { kind: SourceFileKind.Notebook, mdast, frontmatter, identifiers, widgets };
}

export function loadTexFile(
Expand Down
6 changes: 5 additions & 1 deletion packages/myst-cli/src/process/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export async function transformMdast(
frontmatter: preFrontmatter,
location,
identifiers,
widgets,
} = cache.$getMdast(file)?.pre ?? {};
if (!mdastPre || !kind || !location) throw new Error(`Expected mdast to be parsed for ${file}`);
log.debug(`Processing "${file}"`);
Expand Down Expand Up @@ -256,7 +257,8 @@ export async function transformMdast(
frontmatter,
mdast,
references,
};
widgets,
} as any;
const cachedMdast = cache.$getMdast(file);
if (cachedMdast) cachedMdast.post = data;
if (extraTransforms) {
Expand Down Expand Up @@ -408,6 +410,8 @@ export async function finalizeMdast(
if (postData) {
postData.frontmatter = frontmatter;
postData.mdast = mdast;
// TODO out-of-band widgets?
postData.widgets = cache.$getMdast(file)?.pre.widgets;
updateFileInfoFromFrontmatter(session, file, frontmatter);
}
logMessagesFromVFile(session, vfile);
Expand Down
9 changes: 7 additions & 2 deletions packages/myst-cli/src/process/notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
ICell,
IMimeBundle,
INotebookContent,
INotebookMetadata,

Check warning on line 10 in packages/myst-cli/src/process/notebook.ts

View workflow job for this annotation

GitHub Actions / lint

'INotebookMetadata' is defined but never used
IOutput,
MultilineString,
} from '@jupyterlab/nbformat';
Expand Down Expand Up @@ -87,7 +88,7 @@ export async function processNotebookFull(
session: ISession,
file: string,
content: string,
): Promise<{ mdast: GenericParent; frontmatter: PageFrontmatter }> {
): Promise<{ mdast: GenericParent; frontmatter: PageFrontmatter; widgets: Record<string, any> }> {
const { log } = session;
const { metadata, cells } = JSON.parse(content) as INotebookContent;
// notebook will be empty, use generateNotebookChildren, generateNotebookOrder here if we want to populate those
Expand All @@ -111,6 +112,10 @@ export async function processNotebookFull(
frontmatterValidationOpts(vfile),
);

// Load widgets from notebook metadata
// TODO validation / sanitation
const widgets = (metadata?.widgets ?? {}) as Record<string, any>;

let end = cells.length;
if (cells && cells.length > 1 && cells?.[cells.length - 1].source.length === 0) {
end = -1;
Expand Down Expand Up @@ -180,5 +185,5 @@ export async function processNotebookFull(
logMessagesFromVFile(session, vfile);

const mdast = { type: 'root', children: items };
return { mdast, frontmatter };
return { mdast, frontmatter, widgets };
}
3 changes: 2 additions & 1 deletion packages/myst-cli/src/process/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export async function writeFile(
const toc = tic();
const selectedFile = selectFile(session, file);
if (!selectedFile) return;
const { frontmatter, mdast, kind, sha256, slug, references, dependencies, location } =
const { frontmatter, mdast, kind, sha256, slug, references, dependencies, location, widgets } =
selectedFile;
const exports = await Promise.all([
resolvePageSource(session, file),
Expand All @@ -389,6 +389,7 @@ export async function writeFile(
location,
dependencies,
frontmatter: frontmatterWithExports,
widgets,
mdast,
references,
}),
Expand Down
1 change: 1 addition & 0 deletions packages/myst-cli/src/transforms/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type PreRendererData = {
kind: SourceFileKind;
frontmatter?: PageFrontmatter;
identifiers?: string[];
widgets?: Record<string, any>;
};

export type RendererData = PreRendererData & {
Expand Down

0 comments on commit 9f50eb8

Please sign in to comment.