From 4226b03579858b2b840573b0977aa9646295483c Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 6 Mar 2023 10:31:12 -0500 Subject: [PATCH] refactor: add type defs for private integration hooks --- packages/astro/src/@types/astro.ts | 27 +++++++++++++--------- packages/integrations/markdoc/src/index.ts | 22 +++++++++++++----- packages/integrations/mdx/src/index.ts | 25 ++++++++++++++------ 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 818384c99..bf37fda59 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -980,20 +980,25 @@ export interface AstroConfig extends z.output { export interface ContentEntryType { extensions: string[]; - getEntryInfo(params: { fileUrl: URL; contents: string }): Promise<{ - data: Record; - /** - * 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; contentModuleTypes?: string; } +type GetEntryInfoReturnType = { + data: Record; + /** + * 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; diff --git a/packages/integrations/markdoc/src/index.ts b/packages/integrations/markdoc/src/index.ts index ba715981d..bdb2f21b8 100644 --- a/packages/integrations/markdoc/src/index.ts +++ b/packages/integrations/markdoc/src/index.ts @@ -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 & { + 'astro:config:setup': (params: HookParameters<'astro:config:setup'> & { + // `contentEntryType` is not a public API + // Add type defs here + addContentEntryType: (contentEntryType: ContentEntryType) => void + }) => void | Promise; + }; +}; + +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: [ diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts index 00a765d89..a520920cf 100644 --- a/packages/integrations/mdx/src/index.ts +++ b/packages/integrations/mdx/src/index.ts @@ -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 = {}): AstroIntegration { + +type IntegrationWithPrivateHooks = { + name: string; + hooks: Omit & { + '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; + }; +}; + +export default function mdx(partialMdxOptions: Partial = {}): IntegrationWithPrivateHooks { return { name: '@astrojs/mdx', hooks: { @@ -34,7 +47,8 @@ export default function mdx(partialMdxOptions: Partial = {}): 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 = {}): AstroI new URL('../template/content-module-types.d.ts', import.meta.url), 'utf-8' ), - }; - - addPageExtension('.mdx'); - addContentEntryType(contentEntryType); + }); const extendMarkdownConfig = partialMdxOptions.extendMarkdownConfig ?? defaultOptions.extendMarkdownConfig;