Skip to content

Commit ed89359

Browse files
authored
🌉 Update images to work locally (#145)
1 parent daab803 commit ed89359

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

‎.github/workflows/build.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ jobs:
153153
steps:
154154
- uses: actions/checkout@v2
155155
- uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
156+
- name: Install missing dependencies
157+
# On May 30, 2023 this wasn't included in the base try removing in future!
158+
run: pip install typing_extensions
156159
- uses: jupyterlab/maintainer-tools/.github/actions/check-links@v1
157160
with:
158161
ignore_links: 'https:\/\/mybinder\.org.*'

‎src/images.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AttachmentsResolver } from '@jupyterlab/attachments';
66

77
type Options = {
88
resolver: IRenderMime.IResolver | null;
9-
cell: MarkdownCell;
9+
cell?: MarkdownCell;
1010
};
1111

1212
export async function imageUrlSourceTransform(
@@ -17,10 +17,18 @@ export async function imageUrlSourceTransform(
1717
await Promise.all(
1818
images.map(async image => {
1919
if (!image || !image.url) return;
20-
const resolver = new AttachmentsResolver({
21-
parent: opts.resolver ?? undefined,
22-
model: opts.cell.model.attachments
23-
});
20+
if (!opts.resolver) {
21+
console.warn(
22+
'No resolver supplied to `imageUrlSourceTransform`, local images may not work.'
23+
);
24+
return;
25+
}
26+
const resolver = opts.cell
27+
? new AttachmentsResolver({
28+
parent: opts.resolver ?? undefined,
29+
model: opts.cell?.model.attachments
30+
})
31+
: opts.resolver;
2432
const path = await resolver.resolveUrl(image.url);
2533
if (!path) return;
2634
const url = await resolver.getDownloadUrl(path);

‎src/mime.tsx‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { ReactWidget, UseSignal } from '@jupyterlab/apputils';
3333
import { unified } from 'unified';
3434
import { Signal } from '@lumino/signaling';
3535
import React from 'react';
36+
import { imageUrlSourceTransform } from './images';
3637

3738
/**
3839
* The MIME type for Markdown.
@@ -120,8 +121,9 @@ export class RenderedMySTMarkdown
120121
*
121122
* @returns A promise which resolves when rendering is complete.
122123
*/
123-
renderModel(model: IRenderMime.IMimeModel): Promise<void> {
124-
const mdast = markdownParse(model.data[MIME_TYPE] as string);
124+
async renderModel(model: IRenderMime.IMimeModel): Promise<void> {
125+
const markdownText = model.data[MIME_TYPE] as string;
126+
const mdast = markdownParse(markdownText, false);
125127
const linkTransforms = [
126128
new WikiTransformer(),
127129
new GithubTransformer(),
@@ -131,7 +133,6 @@ export class RenderedMySTMarkdown
131133
const file = new VFile();
132134
const references = {
133135
cite: { order: [], data: {} },
134-
footnotes: {},
135136
article: mdast as any
136137
};
137138
const { frontmatter: frontmatterRaw } = getFrontmatter(mdast, {
@@ -159,6 +160,11 @@ export class RenderedMySTMarkdown
159160
.use(keysPlugin)
160161
.runSync(mdast as any, file);
161162

163+
// Go through all links and replace the source if they are local
164+
await imageUrlSourceTransform(mdast as any, {
165+
resolver: this.resolver
166+
});
167+
162168
this.state = {
163169
mdast,
164170
references,

‎src/myst.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { internalLinksPlugin } from './links';
3232
import { addCiteChildrenPlugin } from './citations';
3333
import { evalRole } from './roles';
3434

35-
export function markdownParse(text: string): Root {
35+
export function markdownParse(text: string, inNotebook = true): Root {
3636
const mdast = mystParse(text, {
3737
directives: [
3838
cardDirective,
@@ -56,9 +56,11 @@ export function markdownParse(text: string): Root {
5656
}
5757
})
5858
.runSync(mdast as any);
59-
// Lift children out of blocks for the next step
60-
// We are working here as one cell at a time
61-
liftChildren(mdast, 'block');
59+
if (inNotebook) {
60+
// If in the notebook, lift children out of blocks for the next step
61+
// We are working here as one cell at a time
62+
liftChildren(mdast, 'block');
63+
}
6264
return mdast as Root;
6365
}
6466

‎style/preflight.css‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
background-image: none; /* 2 */
2828
}
2929

30+
.myst img {
31+
max-width: 100%;
32+
}
33+
3034
.myst figure img {
3135
display: block;
36+
max-width: 100%;
3237
}

0 commit comments

Comments
 (0)