summaryrefslogtreecommitdiff
path: root/packages/integrations/markdoc/src/index.ts
blob: d328d4a8d8897a774ffcab46fd69770c9f7d4ab5 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import type { AstroConfig, AstroIntegration, ContentEntryType, HookParameters } from 'astro';
import { getContentEntryType } from './content-entry-type.js';
import {
	type MarkdocConfigResult,
	SUPPORTED_MARKDOC_CONFIG_FILES,
	loadMarkdocConfig,
} from './load-config.js';
import type { MarkdocIntegrationOptions } from './options.js';

type SetupHookParams = HookParameters<'astro:config:setup'> & {
	// `contentEntryType` is not a public API
	// Add type defs here
	addContentEntryType: (contentEntryType: ContentEntryType) => void;
};

export default function markdocIntegration(options?: MarkdocIntegrationOptions): AstroIntegration {
	let markdocConfigResult: MarkdocConfigResult | undefined;
	let astroConfig: AstroConfig;
	return {
		name: '@astrojs/markdoc',
		hooks: {
			'astro:config:setup': async (params) => {
				const { updateConfig, addContentEntryType } = params as SetupHookParams;
				astroConfig = params.config;

				markdocConfigResult = await loadMarkdocConfig(astroConfig);

				addContentEntryType(
					await getContentEntryType({ markdocConfigResult, astroConfig, options }),
				);

				updateConfig({
					vite: {
						ssr: {
							external: ['@astrojs/markdoc/prism', '@astrojs/markdoc/shiki'],
						},
					},
				});
			},
			'astro:server:setup': async ({ server }) => {
				server.watcher.on('all', (_event, entry) => {
					if (SUPPORTED_MARKDOC_CONFIG_FILES.some((f) => entry.endsWith(f))) {
						server.restart();
					}
				});
			},
		},
	};
}