refactor: add type defs for private integration hooks
This commit is contained in:
parent
e467e485cf
commit
4226b03579
3 changed files with 50 additions and 24 deletions
|
@ -980,20 +980,25 @@ export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
|
|||
|
||||
export interface ContentEntryType {
|
||||
extensions: string[];
|
||||
getEntryInfo(params: { fileUrl: URL; contents: string }): Promise<{
|
||||
data: Record<string, unknown>;
|
||||
/**
|
||||
* Used for error hints to point to correct line and location
|
||||
* Should be the untouched data as read from the file,
|
||||
* including newlines
|
||||
*/
|
||||
rawData: string;
|
||||
body: string;
|
||||
slug: string;
|
||||
}>;
|
||||
getEntryInfo(params: {
|
||||
fileUrl: URL;
|
||||
contents: string;
|
||||
}): GetEntryInfoReturnType | Promise<GetEntryInfoReturnType>;
|
||||
contentModuleTypes?: string;
|
||||
}
|
||||
|
||||
type GetEntryInfoReturnType = {
|
||||
data: Record<string, unknown>;
|
||||
/**
|
||||
* Used for error hints to point to correct line and location
|
||||
* Should be the untouched data as read from the file,
|
||||
* including newlines
|
||||
*/
|
||||
rawData: string;
|
||||
body: string;
|
||||
slug: string;
|
||||
}
|
||||
|
||||
export interface AstroSettings {
|
||||
config: AstroConfig;
|
||||
adapter: AstroAdapter | undefined;
|
||||
|
|
|
@ -1,12 +1,23 @@
|
|||
import type { AstroIntegration, AstroConfig } from 'astro';
|
||||
import type { AstroIntegration, AstroConfig, ContentEntryType, HookParameters } from 'astro';
|
||||
import { InlineConfig } from 'vite';
|
||||
import type { Config } from '@markdoc/markdoc';
|
||||
import Markdoc from '@markdoc/markdoc';
|
||||
import { prependForwardSlash, getAstroConfigPath, MarkdocError, parseFrontmatter } from './utils.js';
|
||||
import { getAstroConfigPath, MarkdocError, parseFrontmatter } from './utils.js';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
import fs from 'node:fs';
|
||||
|
||||
export default function markdoc(markdocConfig: Config = {}): AstroIntegration {
|
||||
type IntegrationWithPrivateHooks = {
|
||||
name: string;
|
||||
hooks: Omit<AstroIntegration['hooks'], 'astro:config:setup'> & {
|
||||
'astro:config:setup': (params: HookParameters<'astro:config:setup'> & {
|
||||
// `contentEntryType` is not a public API
|
||||
// Add type defs here
|
||||
addContentEntryType: (contentEntryType: ContentEntryType) => void
|
||||
}) => void | Promise<void>;
|
||||
};
|
||||
};
|
||||
|
||||
export default function markdoc(markdocConfig: Config = {}): IntegrationWithPrivateHooks {
|
||||
return {
|
||||
name: '@astrojs/markdoc',
|
||||
hooks: {
|
||||
|
@ -20,15 +31,14 @@ export default function markdoc(markdocConfig: Config = {}): AstroIntegration {
|
|||
rawData: parsed.matter,
|
||||
};
|
||||
}
|
||||
const contentEntryType = {
|
||||
addContentEntryType({
|
||||
extensions: ['.mdoc'],
|
||||
getEntryInfo,
|
||||
contentModuleTypes: await fs.promises.readFile(
|
||||
new URL('../template/content-module-types.d.ts', import.meta.url),
|
||||
'utf-8'
|
||||
),
|
||||
};
|
||||
addContentEntryType(contentEntryType);
|
||||
});
|
||||
|
||||
const viteConfig: InlineConfig = {
|
||||
plugins: [
|
||||
|
|
|
@ -3,7 +3,7 @@ import { toRemarkInitializeAstroData } from '@astrojs/markdown-remark/dist/inter
|
|||
import { compile as mdxCompile } from '@mdx-js/mdx';
|
||||
import { PluggableList } from '@mdx-js/mdx/lib/core.js';
|
||||
import mdxPlugin, { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
|
||||
import type { AstroIntegration } from 'astro';
|
||||
import type { AstroIntegration, ContentEntryType, HookParameters } from 'astro';
|
||||
import { parse as parseESM } from 'es-module-lexer';
|
||||
import fs from 'node:fs/promises';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
@ -23,7 +23,20 @@ export type MdxOptions = Omit<typeof markdownConfigDefaults, 'remarkPlugins' | '
|
|||
remarkRehype: RemarkRehypeOptions;
|
||||
};
|
||||
|
||||
export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroIntegration {
|
||||
|
||||
type IntegrationWithPrivateHooks = {
|
||||
name: string;
|
||||
hooks: Omit<AstroIntegration['hooks'], 'astro:config:setup'> & {
|
||||
'astro:config:setup': (params: HookParameters<'astro:config:setup'> & {
|
||||
// `addPageExtension` and `contentEntryType` are not a public APIs
|
||||
// Add type defs here
|
||||
addPageExtension: (extension: string) => void
|
||||
addContentEntryType: (contentEntryType: ContentEntryType) => void
|
||||
}) => void | Promise<void>;
|
||||
};
|
||||
};
|
||||
|
||||
export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): IntegrationWithPrivateHooks {
|
||||
return {
|
||||
name: '@astrojs/mdx',
|
||||
hooks: {
|
||||
|
@ -34,7 +47,8 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
|
|||
addContentEntryType,
|
||||
command,
|
||||
}) => {
|
||||
const contentEntryType = {
|
||||
addPageExtension('.mdx');
|
||||
addContentEntryType({
|
||||
extensions: ['.mdx'],
|
||||
async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
||||
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
||||
|
@ -49,10 +63,7 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
|
|||
new URL('../template/content-module-types.d.ts', import.meta.url),
|
||||
'utf-8'
|
||||
),
|
||||
};
|
||||
|
||||
addPageExtension('.mdx');
|
||||
addContentEntryType(contentEntryType);
|
||||
});
|
||||
|
||||
const extendMarkdownConfig =
|
||||
partialMdxOptions.extendMarkdownConfig ?? defaultOptions.extendMarkdownConfig;
|
||||
|
|
Loading…
Reference in a new issue