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,7 +980,14 @@ export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
|
||||||
|
|
||||||
export interface ContentEntryType {
|
export interface ContentEntryType {
|
||||||
extensions: string[];
|
extensions: string[];
|
||||||
getEntryInfo(params: { fileUrl: URL; contents: string }): Promise<{
|
getEntryInfo(params: {
|
||||||
|
fileUrl: URL;
|
||||||
|
contents: string;
|
||||||
|
}): GetEntryInfoReturnType | Promise<GetEntryInfoReturnType>;
|
||||||
|
contentModuleTypes?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetEntryInfoReturnType = {
|
||||||
data: Record<string, unknown>;
|
data: Record<string, unknown>;
|
||||||
/**
|
/**
|
||||||
* Used for error hints to point to correct line and location
|
* Used for error hints to point to correct line and location
|
||||||
|
@ -990,8 +997,6 @@ export interface ContentEntryType {
|
||||||
rawData: string;
|
rawData: string;
|
||||||
body: string;
|
body: string;
|
||||||
slug: string;
|
slug: string;
|
||||||
}>;
|
|
||||||
contentModuleTypes?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AstroSettings {
|
export interface AstroSettings {
|
||||||
|
|
|
@ -1,12 +1,23 @@
|
||||||
import type { AstroIntegration, AstroConfig } from 'astro';
|
import type { AstroIntegration, AstroConfig, ContentEntryType, HookParameters } from 'astro';
|
||||||
import { InlineConfig } from 'vite';
|
import { InlineConfig } from 'vite';
|
||||||
import type { Config } from '@markdoc/markdoc';
|
import type { Config } from '@markdoc/markdoc';
|
||||||
import Markdoc 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 { fileURLToPath, pathToFileURL } from 'node:url';
|
||||||
import fs from 'node:fs';
|
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 {
|
return {
|
||||||
name: '@astrojs/markdoc',
|
name: '@astrojs/markdoc',
|
||||||
hooks: {
|
hooks: {
|
||||||
|
@ -20,15 +31,14 @@ export default function markdoc(markdocConfig: Config = {}): AstroIntegration {
|
||||||
rawData: parsed.matter,
|
rawData: parsed.matter,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const contentEntryType = {
|
addContentEntryType({
|
||||||
extensions: ['.mdoc'],
|
extensions: ['.mdoc'],
|
||||||
getEntryInfo,
|
getEntryInfo,
|
||||||
contentModuleTypes: await fs.promises.readFile(
|
contentModuleTypes: await fs.promises.readFile(
|
||||||
new URL('../template/content-module-types.d.ts', import.meta.url),
|
new URL('../template/content-module-types.d.ts', import.meta.url),
|
||||||
'utf-8'
|
'utf-8'
|
||||||
),
|
),
|
||||||
};
|
});
|
||||||
addContentEntryType(contentEntryType);
|
|
||||||
|
|
||||||
const viteConfig: InlineConfig = {
|
const viteConfig: InlineConfig = {
|
||||||
plugins: [
|
plugins: [
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { toRemarkInitializeAstroData } from '@astrojs/markdown-remark/dist/inter
|
||||||
import { compile as mdxCompile } from '@mdx-js/mdx';
|
import { compile as mdxCompile } from '@mdx-js/mdx';
|
||||||
import { PluggableList } from '@mdx-js/mdx/lib/core.js';
|
import { PluggableList } from '@mdx-js/mdx/lib/core.js';
|
||||||
import mdxPlugin, { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
|
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 { parse as parseESM } from 'es-module-lexer';
|
||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
|
@ -23,7 +23,20 @@ export type MdxOptions = Omit<typeof markdownConfigDefaults, 'remarkPlugins' | '
|
||||||
remarkRehype: RemarkRehypeOptions;
|
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 {
|
return {
|
||||||
name: '@astrojs/mdx',
|
name: '@astrojs/mdx',
|
||||||
hooks: {
|
hooks: {
|
||||||
|
@ -34,7 +47,8 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
|
||||||
addContentEntryType,
|
addContentEntryType,
|
||||||
command,
|
command,
|
||||||
}) => {
|
}) => {
|
||||||
const contentEntryType = {
|
addPageExtension('.mdx');
|
||||||
|
addContentEntryType({
|
||||||
extensions: ['.mdx'],
|
extensions: ['.mdx'],
|
||||||
async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
|
||||||
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
|
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),
|
new URL('../template/content-module-types.d.ts', import.meta.url),
|
||||||
'utf-8'
|
'utf-8'
|
||||||
),
|
),
|
||||||
};
|
});
|
||||||
|
|
||||||
addPageExtension('.mdx');
|
|
||||||
addContentEntryType(contentEntryType);
|
|
||||||
|
|
||||||
const extendMarkdownConfig =
|
const extendMarkdownConfig =
|
||||||
partialMdxOptions.extendMarkdownConfig ?? defaultOptions.extendMarkdownConfig;
|
partialMdxOptions.extendMarkdownConfig ?? defaultOptions.extendMarkdownConfig;
|
||||||
|
|
Loading…
Reference in a new issue