Skip to content

Commit 5b9a1f3

Browse files
committed
feat: generate .md types override
1 parent e464ae8 commit 5b9a1f3

File tree

10 files changed

+61
-39
lines changed

10 files changed

+61
-39
lines changed

packages/astro/src/content/consts.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export const STYLES_PLACEHOLDER = '@@ASTRO-STYLES@@';
66
export const SCRIPTS_PLACEHOLDER = '@@ASTRO-SCRIPTS@@';
77

88
export const CONTENT_TYPES_FILE = 'types.d.ts';
9+
export const MARKDOWN_CONTENT_ENTRY_TYPE_NAME = 'astro:markdown';

packages/astro/src/content/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export {
66
getDotAstroTypeReference,
77
hasMdContentEntryTypeOverride,
88
} from './utils.js';
9+
export { getMarkdownContentEntryType } from './markdown.js';
910
export { astroContentAssetPropagationPlugin } from './vite-plugin-content-assets.js';
1011
export { astroContentImportPlugin } from './vite-plugin-content-imports.js';
1112
export { astroContentVirtualModPlugin } from './vite-plugin-content-virtual-mod.js';
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type fsMod from 'node:fs';
2+
import { fileURLToPath } from 'node:url';
3+
import { AstroConfig, ContentEntryType } from '../@types/astro.js';
4+
import { getContentPaths, parseFrontmatter } from './utils.js';
5+
import { MARKDOWN_CONTENT_ENTRY_TYPE_NAME } from './consts.js';
6+
7+
export async function getMarkdownContentEntryType(
8+
config: Pick<AstroConfig, 'root' | 'srcDir'>,
9+
fs: typeof fsMod
10+
): Promise<ContentEntryType> {
11+
const contentPaths = getContentPaths(config, fs);
12+
return {
13+
name: MARKDOWN_CONTENT_ENTRY_TYPE_NAME,
14+
extensions: ['.md'],
15+
async getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) {
16+
const parsed = parseFrontmatter(contents, fileURLToPath(fileUrl));
17+
return {
18+
data: parsed.data,
19+
body: parsed.content,
20+
slug: parsed.data.slug,
21+
rawData: parsed.matter,
22+
};
23+
},
24+
contentModuleTypes: await fs.promises.readFile(
25+
new URL('./markdown-types.d.ts', contentPaths.templateDir),
26+
'utf-8'
27+
),
28+
};
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare module 'astro:content' {
2+
interface Render {
3+
'.md': Promise<{
4+
Content: import('astro').MarkdownInstance<{}>['Content'];
5+
headings: import('astro').MarkdownHeading[];
6+
remarkPluginFrontmatter: Record<string, any>;
7+
}>;
8+
}
9+
}

packages/astro/src/content/template/types.d.ts

-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
declare module 'astro:content' {
2-
interface Render {
3-
'.md': Promise<{
4-
Content: import('astro').MarkdownInstance<{}>['Content'];
5-
headings: import('astro').MarkdownHeading[];
6-
remarkPluginFrontmatter: Record<string, any>;
7-
}>;
8-
}
9-
}
10-
111
declare module 'astro:content' {
122
export { z } from 'astro/zod';
133
export type CollectionEntry<C extends keyof typeof entryMap> =

packages/astro/src/content/utils.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import { ErrorPayload as ViteErrorPayload, normalizePath, ViteDevServer } from '
77
import { z } from 'zod';
88
import { AstroConfig, AstroSettings } from '../@types/astro.js';
99
import { AstroError, AstroErrorData } from '../core/errors/index.js';
10-
import { MARKDOWN_CONTENT_ENTRY_TYPE_NAME } from '../vite-plugin-markdown/content-entry-type.js';
11-
import { CONTENT_TYPES_FILE } from './consts.js';
10+
import { CONTENT_TYPES_FILE, MARKDOWN_CONTENT_ENTRY_TYPE_NAME } from './consts.js';
1211

1312
export const collectionConfigParser = z.object({
1413
schema: z.any().optional(),
@@ -313,6 +312,7 @@ export function contentObservable(initialCtx: ContentCtx): ContentObservable {
313312
}
314313

315314
export type ContentPaths = {
315+
templateDir: URL;
316316
contentDir: URL;
317317
cacheDir: URL;
318318
typesTemplate: URL;
@@ -330,6 +330,7 @@ export function getContentPaths(
330330
const configStats = search(fs, srcDir);
331331
const templateDir = new URL('../../src/content/template/', import.meta.url);
332332
return {
333+
templateDir,
333334
cacheDir: new URL('.astro/', root),
334335
contentDir: new URL('./content/', srcDir),
335336
typesTemplate: new URL('types.d.ts', templateDir),

packages/astro/src/core/config/settings.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { fileURLToPath, pathToFileURL } from 'url';
55
import jsxRenderer from '../../jsx/renderer.js';
66
import { createDefaultDevConfig } from './config.js';
77
import { loadTSConfig } from './tsconfig.js';
8-
import { markdownContentEntryType } from '../../vite-plugin-markdown/content-entry-type.js';
98

109
export function createBaseSettings(config: AstroConfig): AstroSettings {
1110
return {
@@ -16,7 +15,7 @@ export function createBaseSettings(config: AstroConfig): AstroSettings {
1615
adapter: undefined,
1716
injectedRoutes: [],
1817
pageExtensions: ['.astro', '.html', ...SUPPORTED_MARKDOWN_FILE_EXTENSIONS],
19-
contentEntryTypes: [markdownContentEntryType],
18+
contentEntryTypes: [],
2019
renderers: [jsxRenderer],
2120
scripts: [],
2221
watchFiles: [],

packages/astro/src/integrations/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import {
1212
HookParameters,
1313
RouteData,
1414
} from '../@types/astro.js';
15+
import { hasMdContentEntryTypeOverride } from '../content/utils.js';
1516
import type { SerializedSSRManifest } from '../core/app/types';
1617
import type { PageBuildData } from '../core/build/types';
1718
import { mergeConfig } from '../core/config/config.js';
1819
import { info, LogOptions } from '../core/logger/core.js';
20+
import { getMarkdownContentEntryType } from '../content/index.js';
1921

2022
async function withTakingALongTimeMsg<T>({
2123
name,
@@ -126,6 +128,12 @@ export async function runHookConfigSetup({
126128
}
127129

128130
updatedSettings.config = updatedConfig;
131+
if (!hasMdContentEntryTypeOverride(updatedSettings)) {
132+
updatedSettings.contentEntryTypes.push(
133+
await getMarkdownContentEntryType(updatedSettings.config, fs)
134+
);
135+
}
136+
129137
return updatedSettings;
130138
}
131139

packages/astro/src/vite-plugin-markdown/content-entry-type.ts

-19
This file was deleted.

packages/integrations/markdoc/template/content-module-types.d.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ declare module 'astro:content' {
99
}): Record<string, any>;
1010
};
1111

12+
type RenderResult = Promise<{
13+
Content(props: {
14+
config?: import('@astrojs/markdoc').MarkdocConfig;
15+
components?: Record<string, ComponentRenderer>;
16+
}): import('astro').MarkdownInstance<{}>['Content'];
17+
}>;
18+
1219
interface Render {
13-
'.mdoc': Promise<{
14-
Content(props: {
15-
config?: import('@astrojs/markdoc').MarkdocConfig;
16-
components?: Record<string, ComponentRenderer>;
17-
}): import('astro').MarkdownInstance<{}>['Content'];
18-
}>;
20+
'.md': RenderResult;
21+
'.mdoc': RenderResult;
1922
}
2023
}

0 commit comments

Comments
 (0)