Skip to content

Commit

Permalink
📒 Allow executable code/output to be inside a figure
Browse files Browse the repository at this point in the history
  • Loading branch information
fwkoch committed Nov 17, 2023
1 parent 239c5e2 commit 2e0f878
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/nasty-carrots-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@myst-theme/jupyter': patch
'@myst-theme/site': patch
---

Allow executable code/output to be inside a figure
1 change: 1 addition & 0 deletions packages/jupyter/src/execute/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './provider.js';
export * from './selectors.js';
export * from './types.js';
export * from './busy.js';
export * from './utils.js';
19 changes: 17 additions & 2 deletions packages/jupyter/src/execute/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ import type { GenericParent } from 'myst-common';
import type { Config, IRenderMimeRegistry, ThebeCore } from 'thebe-core';
import type { IdKeyMap, IdKeyMapTarget } from './types.js';

/**
* Return executable code/output from block or single figure inside block
*/
export function executableNodesFromBlock(block: GenericParent) {
if (!block || block.type !== 'block') return;
let target = block;
if (block.children && block.children.length === 1 && block.children[0].type === 'container') {
target = block.children[0] as GenericParent;
}
if (target.children && target.children.length >= 2 && target.children[0].type === 'code') {
return { codeCell: target.children[0], output: target.children[1] };
}
}

/**
* Use the mdast to create a ThebeNotebook from the mdast tree of a notebook.
* This is intended to be used to create an independent ThebeNotebook instance
Expand Down Expand Up @@ -40,8 +54,9 @@ export function notebookFromMdast(

notebook.cells = (mdast.children as GenericParent[]).map((block: GenericParent) => {
if (block.type !== 'block') console.warn(`Unexpected block type ${block.type}`);
if (block.children.length == 2 && block.children[0].type === 'code') {
const [codeCell, output] = block.children;
const executableNodes = executableNodesFromBlock(block);
if (executableNodes) {
const { codeCell, output } = executableNodes;

// use the block.key to identify the cell but maintain a mapping
// to allow code or output keys to look up cells and refs and idenifity
Expand Down
10 changes: 2 additions & 8 deletions packages/site/src/components/ContentBlocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@ import { SourceFileKind } from 'myst-spec-ext';
import type { GenericParent } from 'myst-common';
import classNames from 'classnames';
import {
executableNodesFromBlock,
NotebookClearCell,
NotebookRunCell,
NotebookRunCellSpinnerOnly,
} from '@myst-theme/jupyter';
import { useGridSystemProvider } from '@myst-theme/providers';

function isACodeCell(node: GenericParent) {
return (
node &&
node.type === 'block' &&
node.children &&
node.children?.length === 2 &&
node.children[0].type === 'code' &&
node.children[1].type === 'output'
);
return !!executableNodesFromBlock(node);
}

function Block({
Expand Down

0 comments on commit 2e0f878

Please sign in to comment.