Skip to content

Commit c447219

Browse files
stevejpurvesfwkoch
andauthored
🚛 Migrate xrefs (#2551)
Co-authored-by: Franklin Koch <[email protected]>
1 parent 28d53f8 commit c447219

File tree

4 files changed

+139
-60
lines changed

4 files changed

+139
-60
lines changed

.changeset/whole-taxis-follow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'myst-cli': patch
3+
---
4+
5+
Applies the `myst-migrate` functions to embedded xref content

packages/myst-cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"myst-cli-utils": "^2.0.12",
7272
"myst-common": "^1.9.2",
7373
"myst-config": "^1.9.2",
74+
"myst-migrate": "^1.7.0",
7475
"myst-execute": "^0.3.2",
7576
"myst-ext-button": "^0.0.1",
7677
"myst-ext-card": "^1.0.9",

packages/myst-cli/src/transforms/crossReferences.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import type { CrossReference, Dependency, Link, SourceFileKind } from 'myst-spec
99
import type { ISession } from '../session/types.js';
1010
import { loadFromCache, writeToCache } from '../session/cache.js';
1111
import type { SiteAction, SiteExport } from 'myst-config';
12+
import { migrate } from 'myst-migrate';
13+
import { SPEC_VERSION } from '../spec-version.js';
1214

1315
export const XREF_MAX_AGE = 1; // in days
1416

@@ -58,6 +60,7 @@ async function fetchMystData(
5860
if (cacheData) {
5961
return JSON.parse(cacheData) as MystData;
6062
}
63+
6164
let data: MystData;
6265
try {
6366
const resp = await session.fetch(dataUrl);
@@ -67,7 +70,36 @@ async function fetchMystData(
6770
data = (await resp.json()) as MystData;
6871
} catch {
6972
return onError('Could not load fetched data');
70-
// data is unset
73+
}
74+
75+
// Migrate external content if version differs from current
76+
if (data.mdast && data.version !== undefined && data.version !== SPEC_VERSION) {
77+
session.log.info(
78+
`Migrating external content from v${data.version} to v${SPEC_VERSION} for ${urlSource}`,
79+
);
80+
try {
81+
const migrateFile = {
82+
version: data.version,
83+
mdast: data.mdast,
84+
};
85+
86+
const migrated = await migrate(migrateFile, {
87+
to: SPEC_VERSION,
88+
log: session.log,
89+
});
90+
91+
data.mdast = migrated.mdast;
92+
data.version = migrated.version;
93+
94+
session.log.debug(
95+
`Migrated external content from v${migrateFile.version} to v${SPEC_VERSION} for ${urlSource}`,
96+
);
97+
} catch (error) {
98+
session.log.warn(
99+
`Failed to migrate external content from v${data.version} to v${SPEC_VERSION}: ${error}`,
100+
);
101+
// Continue with original content if migration fails
102+
}
71103
}
72104

73105
writeToCache(session, filename, JSON.stringify(data));

packages/mystmd/tests/notebook-fig-embed/outputs/index.json

Lines changed: 100 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"version": 3,
23
"kind": "Article",
34
"sha256": "f55f0890d64d1ed20bdaa8e6af9c0a50fb3bb729e4a11c0e9c89dc4f5fca2ccb",
45
"slug": "index",
@@ -20,7 +21,8 @@
2021
"exports": [
2122
{
2223
"format": "md",
23-
"filename": "index.md"
24+
"filename": "index.md",
25+
"url": "/index-6f94b8993969cb04027606b5a32dba77.md"
2426
}
2527
]
2628
},
@@ -41,13 +43,15 @@
4143
"position": {
4244
"start": { "line": 3, "column": 1 },
4345
"end": { "line": 3, "column": 1 }
44-
}
46+
},
47+
"key": "KH2zUG8u3f"
4548
}
4649
],
4750
"identifier": "figure-directive-local",
4851
"label": "Figure directive - local",
4952
"html_id": "figure-directive-local",
50-
"implicit": true
53+
"implicit": true,
54+
"key": "axK2NQZ4zO"
5155
},
5256
{
5357
"type": "container",
@@ -68,14 +72,14 @@
6872
"hash": "a16fcedcd26437c820ccfc05d1f48a57",
6973
"path": "/a16fcedcd26437c820ccfc05d1f48a57.html"
7074
},
71-
"text/plain": {
72-
"content": "alt.Chart(...)",
73-
"content_type": "text/plain"
74-
}
75+
"text/plain": { "content": "alt.Chart(...)", "content_type": "text/plain" }
7576
}
76-
}
77+
},
78+
"children": [],
79+
"key": "QJazaUG4dm"
7780
}
78-
]
81+
],
82+
"key": "MbsKRKNyKE"
7983
},
8084
{
8185
"type": "caption",
@@ -93,11 +97,14 @@
9397
"position": {
9498
"start": { "line": 7, "column": 1 },
9599
"end": { "line": 7, "column": 1 }
96-
}
100+
},
101+
"key": "b0svqGHu3G"
97102
}
98-
]
103+
],
104+
"key": "HOrpmKDLct"
99105
}
100-
]
106+
],
107+
"key": "Le1CKCH5xu"
101108
}
102109
],
103110
"enumerator": "1",
@@ -107,7 +114,8 @@
107114
"kind": "Notebook",
108115
"slug": "plot",
109116
"location": "/plot.ipynb"
110-
}
117+
},
118+
"key": "HiFvMsiM5E"
111119
},
112120
{
113121
"type": "heading",
@@ -123,13 +131,15 @@
123131
"position": {
124132
"start": { "line": 10, "column": 1 },
125133
"end": { "line": 10, "column": 1 }
126-
}
134+
},
135+
"key": "x2svglGSqy"
127136
}
128137
],
129138
"identifier": "embed-directive-local",
130139
"label": "Embed directive - local",
131140
"html_id": "embed-directive-local",
132-
"implicit": true
141+
"implicit": true,
142+
"key": "XGu9UfU69D"
133143
},
134144
{
135145
"type": "embed",
@@ -146,7 +156,8 @@
146156
"lang": "python",
147157
"executable": true,
148158
"value": "points",
149-
"enumerator": "1"
159+
"enumerator": "1",
160+
"key": "I8KaZvlXUY"
150161
},
151162
{
152163
"type": "outputs",
@@ -165,11 +176,15 @@
165176
},
166177
"text/plain": { "content": "alt.Chart(...)", "content_type": "text/plain" }
167178
}
168-
}
179+
},
180+
"children": [],
181+
"key": "F0FppwUlIb"
169182
}
170-
]
183+
],
184+
"key": "gvmleAToDt"
171185
}
172-
]
186+
],
187+
"key": "okdCcNhScz"
173188
},
174189
{
175190
"type": "heading",
@@ -185,37 +200,45 @@
185200
"position": {
186201
"start": { "line": 15, "column": 1 },
187202
"end": { "line": 15, "column": 1 }
188-
}
203+
},
204+
"key": "DuP6QTzFkd"
189205
}
190206
],
191207
"identifier": "figure-directive-external",
192208
"label": "Figure directive - external",
193209
"html_id": "figure-directive-external",
194-
"implicit": true
210+
"implicit": true,
211+
"key": "BqMjKFS8va"
195212
},
196213
{
197214
"type": "container",
198215
"kind": "figure",
199216
"children": [
200217
{
201-
"type": "output",
202-
"data": [
218+
"type": "outputs",
219+
"children": [
203220
{
204-
"output_type": "execute_result",
205-
"execution_count": 2,
206-
"metadata": {},
207-
"data": {
208-
"text/html": {
209-
"content_type": "text/html"
210-
},
211-
"text/plain": {
212-
"content": "alt.VConcatChart(...)",
213-
"content_type": "text/plain"
221+
"type": "output",
222+
"jupyter_data": {
223+
"output_type": "execute_result",
224+
"execution_count": 2,
225+
"metadata": {},
226+
"data": {
227+
"text/html": {
228+
"content_type": "text/html",
229+
"hash": "280b61c8fba6a5767e2191500ef403d3",
230+
"path": "https://cdn.curvenote.com/019afec0-1af0-7b31-8ccf-4f0dfa8cbd8c/public/280b61c8fba6a5767e2191500ef403d3.html"
231+
},
232+
"text/plain": {
233+
"content": "alt.VConcatChart(...)",
234+
"content_type": "text/plain"
235+
}
214236
}
215237
}
216238
}
217239
],
218-
"visibility": "show"
240+
"visibility": "show",
241+
"key": "FTN3p05PMS"
219242
},
220243
{
221244
"type": "caption",
@@ -233,11 +256,14 @@
233256
"position": {
234257
"start": { "line": 19, "column": 1 },
235258
"end": { "line": 19, "column": 1 }
236-
}
259+
},
260+
"key": "m0gu4rvLJH"
237261
}
238-
]
262+
],
263+
"key": "yvjMKFtKeO"
239264
}
240-
]
265+
],
266+
"key": "QZy8YhAHOs"
241267
}
242268
],
243269
"enumerator": "2",
@@ -248,8 +274,10 @@
248274
"kind": "Notebook",
249275
"slug": "interactive-notebooks",
250276
"location": "/interactive-notebooks.ipynb",
251-
"title": "Generate and Display Rich Outputs"
252-
}
277+
"title": "Generate and Display Rich Outputs",
278+
"short_title": "Generate Rich Outputs"
279+
},
280+
"key": "nb06UVJ4eS"
253281
},
254282
{
255283
"type": "heading",
@@ -265,13 +293,15 @@
265293
"position": {
266294
"start": { "line": 22, "column": 1 },
267295
"end": { "line": 22, "column": 1 }
268-
}
296+
},
297+
"key": "Z52HvWXDGn"
269298
}
270299
],
271300
"identifier": "embed-directive-external",
272301
"label": "Embed directive - external",
273302
"html_id": "embed-directive-external",
274-
"implicit": true
303+
"implicit": true,
304+
"key": "rwCTTgylzc"
275305
},
276306
{
277307
"type": "embed",
@@ -282,42 +312,53 @@
282312
"kind": "Notebook",
283313
"slug": "interactive-notebooks",
284314
"location": "/interactive-notebooks.ipynb",
285-
"title": "Generate and Display Rich Outputs"
315+
"title": "Generate and Display Rich Outputs",
316+
"short_title": "Generate Rich Outputs"
286317
},
287318
"children": [
288319
{
289320
"type": "code",
290321
"lang": "python",
291322
"executable": true,
292323
"value": "points & bars",
324+
"visibility": "show",
293325
"enumerator": "1",
294-
"visibility": "show"
326+
"key": "d4dGQ2FQ7j"
295327
},
296328
{
297-
"type": "output",
298-
"data": [
329+
"type": "outputs",
330+
"children": [
299331
{
300-
"output_type": "execute_result",
301-
"execution_count": 2,
302-
"metadata": {},
303-
"data": {
304-
"text/html": {
305-
"content_type": "text/html"
306-
},
307-
"text/plain": {
308-
"content": "alt.VConcatChart(...)",
309-
"content_type": "text/plain"
332+
"type": "output",
333+
"jupyter_data": {
334+
"output_type": "execute_result",
335+
"execution_count": 2,
336+
"metadata": {},
337+
"data": {
338+
"text/html": {
339+
"content_type": "text/html",
340+
"hash": "280b61c8fba6a5767e2191500ef403d3",
341+
"path": "https://cdn.curvenote.com/019afec0-1af0-7b31-8ccf-4f0dfa8cbd8c/public/280b61c8fba6a5767e2191500ef403d3.html"
342+
},
343+
"text/plain": {
344+
"content": "alt.VConcatChart(...)",
345+
"content_type": "text/plain"
346+
}
310347
}
311348
}
312349
}
313350
],
314-
"visibility": "show"
351+
"visibility": "show",
352+
"key": "FTN3p05PMS"
315353
}
316-
]
354+
],
355+
"key": "hr2VDiLTDh"
317356
}
318-
]
357+
],
358+
"key": "YCERc1a73b"
319359
}
320-
]
360+
],
361+
"key": "kr0hf7l3Pk"
321362
},
322363
"references": { "cite": { "order": [], "data": {} } }
323364
}

0 commit comments

Comments
 (0)