astro/packages/integrations/markdoc/src/config.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-06-27 19:08:29 +00:00
import { isRelativePath } from '@astrojs/internal-helpers/path';
import type {
Config,
2023-06-06 18:51:14 +00:00
ConfigType as MarkdocConfig,
MaybePromise,
NodeType,
Schema,
} from '@markdoc/markdoc';
import _Markdoc from '@markdoc/markdoc';
2023-06-27 19:08:29 +00:00
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('/');
}