Skip to content

Commit

Permalink
馃悰 Fix embed mdast structure for executable content (#1221)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwkoch committed May 21, 2024
1 parent 8c4cb20 commit 72a127c
Show file tree
Hide file tree
Showing 8 changed files with 552 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/unlucky-kangaroos-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'myst-cli': patch
'mystmd': patch
---

Fix embed mdast structure for executable content
10 changes: 8 additions & 2 deletions packages/myst-cli/src/transforms/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ function mutateEmbedNode(
}
}

function targetsToTarget(targets: GenericNode[]): GenericNode {
if (targets.length === 1) return copyNode(targets[0]);
return { type: 'block', children: copyNode(targets) };
}

/**
* This is the {embed} directive, that embeds nodes from elsewhere in a page.
*/
Expand Down Expand Up @@ -120,7 +125,8 @@ export async function embedTransform(
if (!data) return;
targetNodes = data.mdast.children;
}
const targetNode = { type: 'block', children: targetNodes };
if (!targetNodes?.length) return;
const targetNode = targetsToTarget(targetNodes);
(selectAll('crossReference', targetNode) as CrossReference[]).forEach((targetXRef) => {
// If target xref already has remoteBaseUrl, it can be unchanged in the embedded context
if (targetXRef.remoteBaseUrl) return;
Expand Down Expand Up @@ -183,7 +189,7 @@ export async function embedTransform(
fileError(vfile, `Embed target for "${label}" not found`, { node });
return;
}
const target = { type: 'block', children: copyNode(targetNodes) };
const target = targetsToTarget(targetNodes);
if (!(state as MultiPageReferenceResolver).states) {
// Single page case - we do not need to update urls/dependencies
mutateEmbedNode(node, target);
Expand Down
6 changes: 6 additions & 0 deletions packages/mystmd/tests/exports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,9 @@ cases:
content: outputs/site-xrefs-config.json
- path: site-xrefs/_build/site/myst.xref.json
content: outputs/site-xrefs-myst.xref.json
- title: Notebook embed site build
cwd: notebook-fig-embed
command: myst build
outputs:
- path: notebook-fig-embed/_build/site/content/index.json
content: notebook-fig-embed/outputs/index.json
21 changes: 21 additions & 0 deletions packages/mystmd/tests/notebook-fig-embed/_toc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Table of Contents
#
# Myst will respect:
# 1. New pages
# - file: relative/path/to/page
# 2. New sections without an associated page
# - title: Folder Title
# sections: ...
# 3. New sections with an associated page
# - file: relative/path/to/page
# sections: ...
#
# Note: Titles defined on pages here are not recognized.
#
# This spec is based on the JupyterBook table of contents.
# Learn more at https://jupyterbook.org/customize/toc.html

format: jb-book
root: index
chapters:
- file: plot
25 changes: 25 additions & 0 deletions packages/mystmd/tests/notebook-fig-embed/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Plots In Markdown

## Figure directive - local

```{figure} #my-plot
My Caption
```

## Embed directive - local

```{embed} #my-plot
```

## Figure directive - external

```{figure} xref:guide#img:altair-horsepower
My Caption
```

## Embed directive - external

```{embed} xref:guide#img:altair-horsepower
```
24 changes: 24 additions & 0 deletions packages/mystmd/tests/notebook-fig-embed/myst.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# See docs at: https://mystmd.org/guide/frontmatter
version: 1
project:
id: 715b4b54-6366-422c-af6a-a96359815eaf
# title:
# description:
keywords: []
authors: []
github: https://github.com/executablebooks/mystjs
# bibliography: []
thebe: true
references:
guide: https://mystmd.org/guide
site:
template: book-theme
# title:
# options:
# favicon: favicon.ico
# logo: site_logo.png
nav: []
actions:
- title: Learn More
url: https://mystmd.org/guide
domains: []
Loading

0 comments on commit 72a127c

Please sign in to comment.