Skip to content

Commit

Permalink
fix: more fixes for e.g. visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Nov 21, 2024
1 parent 3fbf213 commit 34a014e
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 67 deletions.
64 changes: 53 additions & 11 deletions packages/myst-cli/src/transforms/crossReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,80 @@ export type MystData = {
references?: References;
};

function upgradeMystData(data: MystData): MystData {
// Visit outputs
/**
* Convert between MyST AST versions either side of the `output` refactoring work
*
* "past" AST is upgraded to "contemporary" AST.
* "future" AST is downgraded to "contemporary AST".
*
* where "contemporary` AST is immediately after #1661 merges"
*
* These two changes allow us to continue to publish AST that will mostly work with old mystmd/myst-theme deployments, whilst being ready for a future breaking change that we can anticipate.
*
* After these upgrades/downgrades, we ensure that we have the following schema:
*
* type Outputs = {
* type: "outputs";
* children: Output[];
* visibility: ...;
* identifier: ...;
* html_id: ...;
* }
*
* type Output = {
* type: "output";
* children: GenericNode[];
* visibility: ...;
* data: IOutput[1];
* }
*
*/
function upgradeAndDowngradeMystData(data: MystData): MystData {
// TODO: output-refactoring -- rewrite this function
visit(
data.mdast as any,
'output',
(node: GenericNode, index: number | null, parent: GenericParent | null) => {
// Upgrade "original" (pre #1661) AST to be compatible with new assumptions
// Case 1: "past" AST
// 1. nest `Output` under `Outputs`
// 2. lift `identifier` and `html_id` labels to `Outputs`
// 3. lift `visibility` to `Outputs`
// 4. duplicate `Outputs.visibility` to new `Output` children
if (parent && parent.type !== 'outputs') {
// assert node.children.length === 1
const outputsChildren = node.data.map((outputData: IOutput) => {
return {
type: 'output',
// TODO: output-refactoring -- drop to single output in future
// Duplicate `Output.visibility` for all sublings (4)
visibility: node.visibility,
data: [outputData],
children: [], // FIXME: ignoring children
children: [], // FIXME: ignoring children here
};
});
// Nest `output` under `outputs` (1)
const outputs = {
type: 'outputs',
children: outputsChildren,
// Lift `Output.visibility` to `Outputs` (3)
visibility: node.visibility,
// Lift `Output.identifier` and `Output.html_id` to `Outputs` (2)
identifier: node.identifier,
html_id: node.html_id,
};
parent.children[index!] = outputs;
return SKIP;
}
// Future ASTs may use `jupyter_output` instead of `data`
// Here we **downgrade** future ASTs. We do this so that MyST post-#1661
// _outputs_ pre-#1661 compatible AST, but remains able to _consume_ post-#1661 AST
// i.e. _this_ version of MyST speaks forward and backwards
// Future AST can only come from xrefs, and .myst.json files (which we ignore, for now?)
// Case 2: "future" AST
// 1. delete new `jupyter_output` field of `Output`
// 2. restore `Output.data`
// 3. duplicate `Outputs.visibility` to `Output`
else if (parent && parent.type === 'outputs' && 'jupyter_output' in node) {
// Downgrade `jupyter_output` (1) and (2)
node.data = [node.jupyter_output];
node.jupyter_output = undefined;

// Duplicate `visibility` onto `Output` children (3)
node.visibility = parent.visibility;
}
},
);
Expand All @@ -88,7 +130,7 @@ async function fetchMystData(
try {
const resp = await session.fetch(dataUrl);
if (resp.ok) {
const data = upgradeMystData((await resp.json()) as MystData);
const data = upgradeAndDowngradeMystData((await resp.json()) as MystData);

writeToCache(session, filename, JSON.stringify(data));
return data;
Expand Down
138 changes: 82 additions & 56 deletions packages/mystmd/tests/notebook-fig-embed/outputs/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,28 @@
"kind": "figure",
"children": [
{
"type": "output",
"data": [
"type": "outputs",
"children": [
{
"output_type": "execute_result",
"execution_count": 2,
"metadata": {},
"data": {
"text/html": {
"content_type": "text/html",
"hash": "a16fcedcd26437c820ccfc05d1f48a57",
"path": "/a16fcedcd26437c820ccfc05d1f48a57.html"
},
"text/plain": { "content": "alt.Chart(...)", "content_type": "text/plain" }
}
"type": "output",
"data": [
{
"output_type": "execute_result",
"execution_count": 2,
"metadata": {},
"data": {
"text/html": {
"content_type": "text/html",
"hash": "a16fcedcd26437c820ccfc05d1f48a57",
"path": "/a16fcedcd26437c820ccfc05d1f48a57.html"
},
"text/plain": {
"content": "alt.Chart(...)",
"content_type": "text/plain"
}
}
}
]
}
]
},
Expand Down Expand Up @@ -143,20 +151,28 @@
"enumerator": "1"
},
{
"type": "output",
"data": [
"type": "outputs",
"children": [
{
"output_type": "execute_result",
"execution_count": 2,
"metadata": {},
"data": {
"text/html": {
"content_type": "text/html",
"hash": "a16fcedcd26437c820ccfc05d1f48a57",
"path": "/a16fcedcd26437c820ccfc05d1f48a57.html"
},
"text/plain": { "content": "alt.Chart(...)", "content_type": "text/plain" }
}
"type": "output",
"data": [
{
"output_type": "execute_result",
"execution_count": 2,
"metadata": {},
"data": {
"text/html": {
"content_type": "text/html",
"hash": "a16fcedcd26437c820ccfc05d1f48a57",
"path": "/a16fcedcd26437c820ccfc05d1f48a57.html"
},
"text/plain": {
"content": "alt.Chart(...)",
"content_type": "text/plain"
}
}
}
]
}
]
}
Expand Down Expand Up @@ -189,24 +205,29 @@
"kind": "figure",
"children": [
{
"type": "output",
"data": [
"type": "outputs",
"children": [
{
"output_type": "execute_result",
"execution_count": 2,
"metadata": {},
"data": {
"text/html": {
"content_type": "text/html"
},
"text/plain": {
"content": "alt.VConcatChart(...)",
"content_type": "text/plain"
"type": "output",
"data": [
{
"output_type": "execute_result",
"execution_count": 2,
"metadata": {},
"data": {
"text/html": {
"content_type": "text/html"
},
"text/plain": {
"content": "alt.VConcatChart(...)",
"content_type": "text/plain"
}
}
}
}
],
"visibility": "show"
}
],
"visibility": "show"
]
},
{
"type": "caption",
Expand Down Expand Up @@ -285,24 +306,29 @@
"visibility": "show"
},
{
"type": "output",
"data": [
"type": "outputs",
"children": [
{
"output_type": "execute_result",
"execution_count": 2,
"metadata": {},
"data": {
"text/html": {
"content_type": "text/html"
},
"text/plain": {
"content": "alt.VConcatChart(...)",
"content_type": "text/plain"
"type": "output",
"data": [
{
"output_type": "execute_result",
"execution_count": 2,
"metadata": {},
"data": {
"text/html": {
"content_type": "text/html"
},
"text/plain": {
"content": "alt.VConcatChart(...)",
"content_type": "text/plain"
}
}
}
}
],
"visibility": "show"
}
],
"visibility": "show"
]
}
]
}
Expand Down

0 comments on commit 34a014e

Please sign in to comment.