2023-06-27 19:08:29 +00:00
|
|
|
import { isRelativePath } from '@astrojs/internal-helpers/path';
|
2023-06-06 18:48:54 +00:00
|
|
|
import type {
|
|
|
|
Config,
|
2023-06-06 18:51:14 +00:00
|
|
|
ConfigType as MarkdocConfig,
|
|
|
|
MaybePromise,
|
2023-06-06 18:48:54 +00:00
|
|
|
NodeType,
|
|
|
|
Schema,
|
|
|
|
} from '@markdoc/markdoc';
|
2023-06-27 19:05:17 +00:00
|
|
|
import _Markdoc from '@markdoc/markdoc';
|
2023-06-27 19:08:29 +00:00
|
|
|
import type { AstroInstance } from 'astro';
|
2023-05-24 20:52:22 +00:00
|
|
|
import { heading } from './heading-ids.js';
|
2023-06-27 19:05:17 +00:00
|
|
|
import { componentConfigSymbol } from './utils.js';
|
2023-05-24 20:52:22 +00:00
|
|
|
|
2023-06-27 19:05:17 +00:00
|
|
|
export type Render = ComponentConfig | AstroInstance['default'] | string;
|
|
|
|
export type ComponentConfig = {
|
|
|
|
type: 'package' | 'local';
|
|
|
|
path: string;
|
|
|
|
namedExport?: string;
|
|
|
|
[componentConfigSymbol]: true;
|
|
|
|
};
|
2023-06-06 18:48:54 +00:00
|
|
|
|
|
|
|
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>[];
|
|
|
|
}>;
|
2023-05-24 20:52:22 +00:00
|
|
|
|
|
|
|
export type ResolvedAstroMarkdocConfig = Omit<AstroMarkdocConfig, 'extends'>;
|
2023-05-17 13:13:10 +00:00
|
|
|
|
|
|
|
export const Markdoc = _Markdoc;
|
2023-05-24 20:52:22 +00:00
|
|
|
export const nodes = { ...Markdoc.nodes, heading };
|
2023-03-27 22:04:37 +00:00
|
|
|
|
2023-05-24 20:52:22 +00:00
|
|
|
export function defineMarkdocConfig(config: AstroMarkdocConfig): AstroMarkdocConfig {
|
2023-03-27 22:04:37 +00:00
|
|
|
return config;
|
|
|
|
}
|
2023-06-27 19:05:17 +00:00
|
|
|
|
|
|
|
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('/');
|
|
|
|
}
|