From 230b90147d7f74a7fdf4d611c15ee71147ccbb50 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Wed, 20 Nov 2024 15:09:36 +0000 Subject: [PATCH] fix: proper lifting --- packages/myst-cli/src/transforms/outputs.ts | 34 ++++++++++++--------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/packages/myst-cli/src/transforms/outputs.ts b/packages/myst-cli/src/transforms/outputs.ts index adfef58f8..5e0ca772a 100644 --- a/packages/myst-cli/src/transforms/outputs.ts +++ b/packages/myst-cli/src/transforms/outputs.ts @@ -28,10 +28,9 @@ function getWriteDestination(hash: string, contentType: string, writeFolder: str const MARKDOWN_MIME_TYPE = 'text/markdown'; function parseVariant(mimeType: string): string | undefined { - const [variant] = Array.from(mimeType.matchAll(/;([^;]+)=([^;]+)/)) + const [variant] = Array.from(mimeType.matchAll(/;([^;]+)=([^;]+)/g)) .filter(([name]) => name === 'variant') .map((pair) => pair[1]); - console.log('parse', mimeType, variant); return variant; } @@ -42,20 +41,25 @@ export async function transformMarkdownOutputs( }, ) { const outputs = selectAll('output', mdast) as GenericNode[]; - outputs.map(async (output) => { - // Find the most MyST-like Markdown (if any) - const [bestEntry] = Object.entries(output.data) - .filter(([mimeType]) => mimeType.startsWith(MARKDOWN_MIME_TYPE)) - .map(([mimeType, data]) => [parseVariant(mimeType), data]) - .filter(([variant]) => variant !== undefined && variant !== 'myst') - .sort((left) => (left[0] === undefined ? +1 : -1)); + outputs.forEach((output) => { + const rawOutput = output.data as IOutput; + switch (rawOutput.output_type) { + case 'display_data': + case 'execute_result': { + // Find the most MyST-like Markdown (if any) + const [bestEntry] = Object.entries(rawOutput.data as object) + .filter(([mimeType]) => mimeType.startsWith(MARKDOWN_MIME_TYPE)) + .map(([mimeType, data]) => [parseVariant(mimeType), data]) + .filter(([variant]) => variant === undefined || variant === 'myst') + .sort((left) => (left[0] === undefined ? +1 : -1)); - console.log(bestEntry); - // Process Markdown - if (bestEntry !== undefined) { - const data = bestEntry[1]; - const outputMdast = opts.parser(data as string); - output.children = outputMdast.children; + // Process Markdown + if (bestEntry !== undefined) { + const data = bestEntry[1]; + const outputMdast = opts.parser(data as string); + output.children = outputMdast.children; + } + } } }); }