From 3db67a21873af17369af6ecc3315c58dc6616f38 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 14 Feb 2023 13:48:56 -0500 Subject: [PATCH] feat: generate `.md` types override --- packages/astro/src/content/consts.ts | 1 + packages/astro/src/content/index.ts | 1 + packages/astro/src/content/markdown.ts | 29 +++++++++++++++++++ .../src/content/template/markdown-types.d.ts | 9 ++++++ .../astro/src/content/template/types.d.ts | 10 ------- packages/astro/src/content/utils.ts | 5 ++-- packages/astro/src/core/config/settings.ts | 3 +- packages/astro/src/integrations/index.ts | 8 +++++ .../content-entry-type.ts | 19 ------------ .../template/content-module-types.d.ts | 15 ++++++---- 10 files changed, 61 insertions(+), 39 deletions(-) create mode 100644 packages/astro/src/content/markdown.ts create mode 100644 packages/astro/src/content/template/markdown-types.d.ts delete mode 100644 packages/astro/src/vite-plugin-markdown/content-entry-type.ts diff --git a/packages/astro/src/content/consts.ts b/packages/astro/src/content/consts.ts index 1f0470d5a..3c671102d 100644 --- a/packages/astro/src/content/consts.ts +++ b/packages/astro/src/content/consts.ts @@ -6,3 +6,4 @@ export const STYLES_PLACEHOLDER = '@@ASTRO-STYLES@@'; export const SCRIPTS_PLACEHOLDER = '@@ASTRO-SCRIPTS@@'; export const CONTENT_TYPES_FILE = 'types.d.ts'; +export const MARKDOWN_CONTENT_ENTRY_TYPE_NAME = 'astro:markdown'; diff --git a/packages/astro/src/content/index.ts b/packages/astro/src/content/index.ts index 67e55e48d..d77a26b64 100644 --- a/packages/astro/src/content/index.ts +++ b/packages/astro/src/content/index.ts @@ -6,6 +6,7 @@ export { getDotAstroTypeReference, hasMdContentEntryTypeOverride, } from './utils.js'; +export { getMarkdownContentEntryType } from './markdown.js'; export { astroContentAssetPropagationPlugin } from './vite-plugin-content-assets.js'; export { astroContentImportPlugin } from './vite-plugin-content-imports.js'; export { astroContentVirtualModPlugin } from './vite-plugin-content-virtual-mod.js'; diff --git a/packages/astro/src/content/markdown.ts b/packages/astro/src/content/markdown.ts new file mode 100644 index 000000000..8d342d5b3 --- /dev/null +++ b/packages/astro/src/content/markdown.ts @@ -0,0 +1,29 @@ +import type fsMod from 'node:fs'; +import { fileURLToPath } from 'node:url'; +import { AstroConfig, ContentEntryType } from '../@types/astro.js'; +import { getContentPaths, parseFrontmatter } from './utils.js'; +import { MARKDOWN_CONTENT_ENTRY_TYPE_NAME } from './consts.js'; + +export async function getMarkdownContentEntryType( + config: Pick, + fs: typeof fsMod +): Promise { + const contentPaths = getContentPaths(config, fs); + return { + name: MARKDOWN_CONTENT_ENTRY_TYPE_NAME, + extensions: ['.md'], + async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) { + const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl)); + return { + data: parsed.data, + body: parsed.content, + slug: parsed.data.slug, + rawData: parsed.matter, + }; + }, + contentModuleTypes: await fs.promises.readFile( + new URL('./markdown-types.d.ts', contentPaths.templateDir), + 'utf-8' + ), + }; +} diff --git a/packages/astro/src/content/template/markdown-types.d.ts b/packages/astro/src/content/template/markdown-types.d.ts new file mode 100644 index 000000000..74eea096e --- /dev/null +++ b/packages/astro/src/content/template/markdown-types.d.ts @@ -0,0 +1,9 @@ +declare module 'astro:content' { + interface Render { + '.md': Promise<{ + Content: import('astro').MarkdownInstance<{}>['Content']; + headings: import('astro').MarkdownHeading[]; + remarkPluginFrontmatter: Record; + }>; + } +} diff --git a/packages/astro/src/content/template/types.d.ts b/packages/astro/src/content/template/types.d.ts index abe187a33..b85ba8423 100644 --- a/packages/astro/src/content/template/types.d.ts +++ b/packages/astro/src/content/template/types.d.ts @@ -1,13 +1,3 @@ -declare module 'astro:content' { - interface Render { - '.md': Promise<{ - Content: import('astro').MarkdownInstance<{}>['Content']; - headings: import('astro').MarkdownHeading[]; - remarkPluginFrontmatter: Record; - }>; - } -} - declare module 'astro:content' { export { z } from 'astro/zod'; export type CollectionEntry = diff --git a/packages/astro/src/content/utils.ts b/packages/astro/src/content/utils.ts index ced7794d3..30841a9fd 100644 --- a/packages/astro/src/content/utils.ts +++ b/packages/astro/src/content/utils.ts @@ -7,8 +7,7 @@ import { ErrorPayload as ViteErrorPayload, normalizePath, ViteDevServer } from ' import { z } from 'zod'; import { AstroConfig, AstroSettings } from '../@types/astro.js'; import { AstroError, AstroErrorData } from '../core/errors/index.js'; -import { MARKDOWN_CONTENT_ENTRY_TYPE_NAME } from '../vite-plugin-markdown/content-entry-type.js'; -import { CONTENT_TYPES_FILE } from './consts.js'; +import { CONTENT_TYPES_FILE, MARKDOWN_CONTENT_ENTRY_TYPE_NAME } from './consts.js'; export const collectionConfigParser = z.object({ schema: z.any().optional(), @@ -316,6 +315,7 @@ export function contentObservable(initialCtx: ContentCtx): ContentObservable { } export type ContentPaths = { + templateDir: URL; contentDir: URL; cacheDir: URL; typesTemplate: URL; @@ -333,6 +333,7 @@ export function getContentPaths( const configStats = search(fs, srcDir); const templateDir = new URL('../../src/content/template/', import.meta.url); return { + templateDir, cacheDir: new URL('.astro/', root), contentDir: new URL('./content/', srcDir), typesTemplate: new URL('types.d.ts', templateDir), diff --git a/packages/astro/src/core/config/settings.ts b/packages/astro/src/core/config/settings.ts index c92222b04..c95a9fd19 100644 --- a/packages/astro/src/core/config/settings.ts +++ b/packages/astro/src/core/config/settings.ts @@ -6,7 +6,6 @@ import jsxRenderer from '../../jsx/renderer.js'; import { createDefaultDevConfig } from './config.js'; import { AstroTimer } from './timer.js'; import { loadTSConfig } from './tsconfig.js'; -import { markdownContentEntryType } from '../../vite-plugin-markdown/content-entry-type.js'; export function createBaseSettings(config: AstroConfig): AstroSettings { return { @@ -17,7 +16,7 @@ export function createBaseSettings(config: AstroConfig): AstroSettings { adapter: undefined, injectedRoutes: [], pageExtensions: ['.astro', '.html', ...SUPPORTED_MARKDOWN_FILE_EXTENSIONS], - contentEntryTypes: [markdownContentEntryType], + contentEntryTypes: [], renderers: [jsxRenderer], scripts: [], watchFiles: [], diff --git a/packages/astro/src/integrations/index.ts b/packages/astro/src/integrations/index.ts index 0c69355c9..f817a66ee 100644 --- a/packages/astro/src/integrations/index.ts +++ b/packages/astro/src/integrations/index.ts @@ -12,10 +12,12 @@ import { HookParameters, RouteData, } from '../@types/astro.js'; +import { hasMdContentEntryTypeOverride } from '../content/utils.js'; import type { SerializedSSRManifest } from '../core/app/types'; import type { PageBuildData } from '../core/build/types'; import { mergeConfig } from '../core/config/config.js'; import { info, LogOptions } from '../core/logger/core.js'; +import { getMarkdownContentEntryType } from '../content/index.js'; async function withTakingALongTimeMsg({ name, @@ -126,6 +128,12 @@ export async function runHookConfigSetup({ } updatedSettings.config = updatedConfig; + if (!hasMdContentEntryTypeOverride(updatedSettings)) { + updatedSettings.contentEntryTypes.push( + await getMarkdownContentEntryType(updatedSettings.config, fs) + ); + } + return updatedSettings; } diff --git a/packages/astro/src/vite-plugin-markdown/content-entry-type.ts b/packages/astro/src/vite-plugin-markdown/content-entry-type.ts deleted file mode 100644 index b8ed28f36..000000000 --- a/packages/astro/src/vite-plugin-markdown/content-entry-type.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { fileURLToPath } from 'node:url'; -import { ContentEntryType } from '../@types/astro.js'; -import { parseFrontmatter } from '../content/utils.js'; - -export const MARKDOWN_CONTENT_ENTRY_TYPE_NAME = 'astro:markdown'; - -export const markdownContentEntryType: ContentEntryType = { - name: MARKDOWN_CONTENT_ENTRY_TYPE_NAME, - extensions: ['.md'], - async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) { - const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl)); - return { - data: parsed.data, - body: parsed.content, - slug: parsed.data.slug, - rawData: parsed.matter, - }; - }, -}; diff --git a/packages/integrations/markdoc/template/content-module-types.d.ts b/packages/integrations/markdoc/template/content-module-types.d.ts index 528c7db7e..02be3fa27 100644 --- a/packages/integrations/markdoc/template/content-module-types.d.ts +++ b/packages/integrations/markdoc/template/content-module-types.d.ts @@ -9,12 +9,15 @@ declare module 'astro:content' { }): Record; }; + type RenderResult = Promise<{ + Content(props: { + config?: import('@astrojs/markdoc').MarkdocConfig; + components?: Record; + }): import('astro').MarkdownInstance<{}>['Content']; + }>; + interface Render { - '.mdoc': Promise<{ - Content(props: { - config?: import('@astrojs/markdoc').MarkdocConfig; - components?: Record; - }): import('astro').MarkdownInstance<{}>['Content']; - }>; + '.md': RenderResult; + '.mdoc': RenderResult; } }