Skip to content

Commit

Permalink
fix: proper lifting
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Nov 20, 2024
1 parent 1dbf97a commit 230b901
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions packages/myst-cli/src/transforms/outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
}
}
});
}
Expand Down

0 comments on commit 230b901

Please sign in to comment.