summaryrefslogtreecommitdiff
path: root/packages/integrations/markdoc/src/config.ts
blob: c62bfebab4a7c155b0a78774bdae27082548a849 (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
50
51
52
53
import { isRelativePath } from '@astrojs/internal-helpers/path';
import type {
	Config,
	ConfigType as MarkdocConfig,
	MaybePromise,
	NodeType,
	Schema,
} from '@markdoc/markdoc';
import _Markdoc from '@markdoc/markdoc';
import type { AstroInstance } from 'astro';
import { heading } from './heading-ids.js';
import { componentConfigSymbol } from './utils.js';

export type Render = ComponentConfig | AstroInstance['default'] | string;
export type ComponentConfig = {
	type: 'package' | 'local';
	path: string;
	namedExport?: string;
	[componentConfigSymbol]: true;
};

export type AstroMarkdocConfig<C extends Record<string, any> = Record<string, any>> = Omit<
	MarkdocConfig,
	'tags' | 'nodes'
> &
	Partial<{
		tags: Record<string, Schema<Config, Render>>;
		nodes: Partial<Record<NodeType, Schema<Config, Render>>>;
		ctx: C;
		extends: MaybePromise<ResolvedAstroMarkdocConfig>[];
	}>;

export type ResolvedAstroMarkdocConfig = Omit<AstroMarkdocConfig, 'extends'>;

export const Markdoc = _Markdoc;
export const nodes = { ...Markdoc.nodes, heading };

export function defineMarkdocConfig(config: AstroMarkdocConfig): AstroMarkdocConfig {
	return config;
}

export function component(pathnameOrPkgName: string, namedExport?: string): ComponentConfig {
	return {
		type: isNpmPackageName(pathnameOrPkgName) ? 'package' : 'local',
		path: pathnameOrPkgName,
		namedExport,
		[componentConfigSymbol]: true,
	};
}

function isNpmPackageName(pathname: string) {
	return !isRelativePath(pathname) && !pathname.startsWith('/');
}