Skip to content

Commit

Permalink
🎇 Placeholder for iFrame (#1609)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Oct 29, 2024
1 parent fd78758 commit eb411d0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/cool-suits-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"myst-directives": patch
"myst-spec-ext": patch
"myst-to-typst": patch
---

Allow for iframe to have a placeholder
19 changes: 18 additions & 1 deletion packages/myst-directives/src/iframe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Iframe } from 'myst-spec-ext';
import type { Iframe, Image } from 'myst-spec-ext';
import type { DirectiveSpec, DirectiveData, GenericNode } from 'myst-common';
import { addCommonDirectiveOptions, commonDirectiveOptions } from './utils.js';

Expand All @@ -23,6 +23,10 @@ export const iframeDirective: DirectiveSpec = {
type: String,
doc: 'The alignment of the iframe in the page. Choose one of `left`, `center` or `right`',
},
placeholder: {
type: String,
doc: 'A placeholder image for the iframe in static exports.',
},
},
body: { type: 'myst', doc: 'If provided, this will be the iframe caption.' },
run(data: DirectiveData): GenericNode[] {
Expand All @@ -32,6 +36,19 @@ export const iframeDirective: DirectiveSpec = {
width: data.options?.width as string,
align: data.options?.align as Iframe['align'],
};
if (data.options?.placeholder) {
iframe.children = [
{
type: 'image',
placeholder: true,
url: data.options.placeholder as string,
alt: data.options?.alt as string,
width: data.options?.width as string,
height: data.options?.height as string,
align: data.options?.align as Image['align'],
} as Image,
];
}
if (!data.body) {
iframe.class = data.options?.class as string;
addCommonDirectiveOptions(data, iframe);
Expand Down
1 change: 1 addition & 0 deletions packages/myst-spec-ext/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export type Iframe = Target & {
width?: string;
align?: Image['align'];
class?: Image['class'];
children?: Image[];
};

export type Admonition = SpecAdmonition & {
Expand Down
13 changes: 13 additions & 0 deletions packages/myst-to-typst/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,19 @@ const handlers: Record<string, Handler> = {
}
state.write(')\n\n');
},
iframe(node, state) {
const image = node.children?.[0];
if (!image || image.placeholder !== true) return;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { width: nodeWidth, url: nodeSrc, align } = image;
const src = nodeSrc;
const width = getLatexImageWidth(nodeWidth);
state.write(`#image("${src}"`);
if (!state.data.isInTable) {
state.write(`, width: ${width}`);
}
state.write(')\n\n');
},
container: containerHandler,
caption: captionHandler,
legend: captionHandler,
Expand Down

0 comments on commit eb411d0

Please sign in to comment.