summaryrefslogtreecommitdiff
path: root/packages/integrations/markdoc/src/experimental-assets-config.ts
blob: 2eb96ec99277ae64b8e3348d16fdb1d9dd3b938a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import type { Config as MarkdocConfig } from '@markdoc/markdoc';
import Markdoc from '@markdoc/markdoc';
//@ts-expect-error Cannot find module 'astro:assets' or its corresponding type declarations.
import { Image } from 'astro:assets';

// Separate module to only import `astro:assets` when
// `experimental.assets` flag is set in a project.
// TODO: merge with `./runtime.ts` when `experimental.assets` is baselined.
export const experimentalAssetsConfig: MarkdocConfig = {
	nodes: {
		image: {
			attributes: {
				...Markdoc.nodes.image.attributes,
				__optimizedSrc: { type: 'Object' },
			},
			transform(node, config) {
				const attributes = node.transformAttributes(config);
				const children = node.transformChildren(config);

				if (node.type === 'image' && '__optimizedSrc' in node.attributes) {
					const { __optimizedSrc, ...rest } = node.attributes;
					return new Markdoc.Tag(Image, { ...rest, src: __optimizedSrc }, children);
				} else {
					return new Markdoc.Tag('img', attributes, children);
				}
			},
		},
	},
};