From bb14ede14f273bdb7fab7c9a68654b7c1dc75082 Mon Sep 17 00:00:00 2001 From: Franklin Koch Date: Sun, 8 Oct 2023 16:14:27 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=86=20Add=20inline=20property=20to=20i?= =?UTF-8?q?mages=20in=20paragraphs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/fifty-dolphins-sneeze.md | 7 +++++++ packages/myst-cli/src/process/mdast.ts | 2 ++ packages/myst-spec-ext/src/types.ts | 1 + packages/myst-transforms/src/images.spec.ts | 23 +++++++++++++++++++++ packages/myst-transforms/src/images.ts | 10 ++++++++- packages/myst-transforms/src/index.ts | 2 +- 6 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 .changeset/fifty-dolphins-sneeze.md create mode 100644 packages/myst-transforms/src/images.spec.ts diff --git a/.changeset/fifty-dolphins-sneeze.md b/.changeset/fifty-dolphins-sneeze.md new file mode 100644 index 000000000..f2afab50d --- /dev/null +++ b/.changeset/fifty-dolphins-sneeze.md @@ -0,0 +1,7 @@ +--- +'myst-transforms': patch +'myst-spec-ext': patch +'myst-cli': patch +--- + +Add inline property to images in paragraphs diff --git a/packages/myst-cli/src/process/mdast.ts b/packages/myst-cli/src/process/mdast.ts index d1c093462..56574e940 100644 --- a/packages/myst-cli/src/process/mdast.ts +++ b/packages/myst-cli/src/process/mdast.ts @@ -24,6 +24,7 @@ import { joinGatesPlugin, glossaryPlugin, abbreviationPlugin, + imageInlineTransform, } from 'myst-transforms'; import { unified } from 'unified'; import { VFile } from 'vfile'; @@ -239,6 +240,7 @@ export async function transformMdast( }); } } + imageInlineTransform(mdast); await transformDeleteBase64UrlSource(mdast); const sha256 = selectors.selectFileInfo(store.getState(), file).sha256 as string; const useSlug = pageSlug !== index; diff --git a/packages/myst-spec-ext/src/types.ts b/packages/myst-spec-ext/src/types.ts index 94c7d24e0..a27b9a919 100644 --- a/packages/myst-spec-ext/src/types.ts +++ b/packages/myst-spec-ext/src/types.ts @@ -67,6 +67,7 @@ export type Image = SpecImage & { urlSource?: string; height?: string; placeholder?: boolean; + inline?: boolean; }; export type Iframe = { diff --git a/packages/myst-transforms/src/images.spec.ts b/packages/myst-transforms/src/images.spec.ts new file mode 100644 index 000000000..adab318a3 --- /dev/null +++ b/packages/myst-transforms/src/images.spec.ts @@ -0,0 +1,23 @@ +import { describe, expect, test } from 'vitest'; +import { imageInlineTransform } from './images'; + +describe('Test imageInlineTransform', () => { + test('image outside paragraph returns self', async () => { + const mdast = { type: 'root', children: [{ type: 'image', url: 'my-image' }] }; + imageInlineTransform(mdast); + expect(mdast).toEqual({ type: 'root', children: [{ type: 'image', url: 'my-image' }] }); + }); + test('image inside paragraph adds inline', async () => { + const mdast = { + type: 'root', + children: [{ type: 'paragraph', children: [{ type: 'image', url: 'my-image' }] }], + }; + imageInlineTransform(mdast); + expect(mdast).toEqual({ + type: 'root', + children: [ + { type: 'paragraph', children: [{ type: 'image', url: 'my-image', inline: true }] }, + ], + }); + }); +}); diff --git a/packages/myst-transforms/src/images.ts b/packages/myst-transforms/src/images.ts index 465c1e4ef..5830dc36f 100644 --- a/packages/myst-transforms/src/images.ts +++ b/packages/myst-transforms/src/images.ts @@ -1,5 +1,6 @@ import type { Plugin } from 'unified'; -import type { Container, Paragraph, PhrasingContent, Image } from 'myst-spec'; +import type { Container, Paragraph, PhrasingContent } from 'myst-spec'; +import type { Image } from 'myst-spec-ext'; import { select, selectAll } from 'unist-util-select'; import type { GenericParent } from 'myst-common'; import { toText } from 'myst-common'; @@ -21,3 +22,10 @@ export function imageAltTextTransform(tree: GenericParent) { export const imageAltTextPlugin: Plugin<[], GenericParent, GenericParent> = () => (tree) => { imageAltTextTransform(tree); }; + +export function imageInlineTransform(tree: GenericParent) { + const imagesInParagraphs = selectAll('paragraph > image', tree) as Image[]; + imagesInParagraphs.forEach((image) => { + image.inline = true; + }); +} diff --git a/packages/myst-transforms/src/index.ts b/packages/myst-transforms/src/index.ts index c3b01336a..62573ee3b 100644 --- a/packages/myst-transforms/src/index.ts +++ b/packages/myst-transforms/src/index.ts @@ -25,7 +25,7 @@ export { } from './blocks.js'; export { codePlugin, codeTransform } from './code.js'; export { blockquotePlugin, blockquoteTransform } from './blockquote.js'; -export { imageAltTextPlugin, imageAltTextTransform } from './images.js'; +export { imageAltTextPlugin, imageAltTextTransform, imageInlineTransform } from './images.js'; export { liftMystDirectivesAndRolesPlugin, liftMystDirectivesAndRolesTransform,