From 9f50eb8f8a43e5f2ba9157cc582f6ddbaa46506b Mon Sep 17 00:00:00 2001 From: Steve Purves Date: Tue, 24 Sep 2024 16:44:57 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A6=20building=20widget=20state=20(#13?= =?UTF-8?q?82)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rowan Cockett Co-authored-by: Franklin Koch --- .changeset/short-peas-love.md | 5 +++++ packages/myst-cli/src/process/file.ts | 9 +++++++-- packages/myst-cli/src/process/mdast.ts | 6 +++++- packages/myst-cli/src/process/notebook.ts | 9 +++++++-- packages/myst-cli/src/process/site.ts | 3 ++- packages/myst-cli/src/transforms/types.ts | 1 + 6 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 .changeset/short-peas-love.md diff --git a/.changeset/short-peas-love.md b/.changeset/short-peas-love.md new file mode 100644 index 000000000..9e23a68ef --- /dev/null +++ b/.changeset/short-peas-love.md @@ -0,0 +1,5 @@ +--- +"myst-cli": patch +--- + +💦 building widget state diff --git a/packages/myst-cli/src/process/file.ts b/packages/myst-cli/src/process/file.ts index 9a69ae6a0..735ce8195 100644 --- a/packages/myst-cli/src/process/file.ts +++ b/packages/myst-cli/src/process/file.ts @@ -32,6 +32,7 @@ export type LoadFileResult = { mdast: GenericParent; frontmatter?: PageFrontmatter; identifiers?: string[]; + widgets?: Record; }; function checkCache(cache: ISessionWithCache, content: string, file: string) { @@ -69,7 +70,11 @@ export async function loadNotebookFile( ): Promise { 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, @@ -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( diff --git a/packages/myst-cli/src/process/mdast.ts b/packages/myst-cli/src/process/mdast.ts index da6e7779a..9c4bce996 100644 --- a/packages/myst-cli/src/process/mdast.ts +++ b/packages/myst-cli/src/process/mdast.ts @@ -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}"`); @@ -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) { @@ -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); diff --git a/packages/myst-cli/src/process/notebook.ts b/packages/myst-cli/src/process/notebook.ts index 78ca95b71..ce8323f39 100644 --- a/packages/myst-cli/src/process/notebook.ts +++ b/packages/myst-cli/src/process/notebook.ts @@ -7,6 +7,7 @@ import type { ICell, IMimeBundle, INotebookContent, + INotebookMetadata, IOutput, MultilineString, } from '@jupyterlab/nbformat'; @@ -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 }> { 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 @@ -111,6 +112,10 @@ export async function processNotebookFull( frontmatterValidationOpts(vfile), ); + // Load widgets from notebook metadata + // TODO validation / sanitation + const widgets = (metadata?.widgets ?? {}) as Record; + let end = cells.length; if (cells && cells.length > 1 && cells?.[cells.length - 1].source.length === 0) { end = -1; @@ -180,5 +185,5 @@ export async function processNotebookFull( logMessagesFromVFile(session, vfile); const mdast = { type: 'root', children: items }; - return { mdast, frontmatter }; + return { mdast, frontmatter, widgets }; } diff --git a/packages/myst-cli/src/process/site.ts b/packages/myst-cli/src/process/site.ts index 97205f59b..3331942ce 100644 --- a/packages/myst-cli/src/process/site.ts +++ b/packages/myst-cli/src/process/site.ts @@ -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), @@ -389,6 +389,7 @@ export async function writeFile( location, dependencies, frontmatter: frontmatterWithExports, + widgets, mdast, references, }), diff --git a/packages/myst-cli/src/transforms/types.ts b/packages/myst-cli/src/transforms/types.ts index 54f0645ec..738a3f9ab 100644 --- a/packages/myst-cli/src/transforms/types.ts +++ b/packages/myst-cli/src/transforms/types.ts @@ -10,6 +10,7 @@ export type PreRendererData = { kind: SourceFileKind; frontmatter?: PageFrontmatter; identifiers?: string[]; + widgets?: Record; }; export type RendererData = PreRendererData & {