feat: generate .md types override

This commit is contained in:
bholmesdev 2023-02-14 13:48:56 -05:00
parent 3408e9cb8b
commit a94e354832
10 changed files with 61 additions and 39 deletions

View file

@ -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';

View file

@ -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';

View file

@ -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<AstroConfig, 'root' | 'srcDir'>,
fs: typeof fsMod
): Promise<ContentEntryType> {
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'
),
};
}

View file

@ -0,0 +1,9 @@
declare module 'astro:content' {
interface Render {
'.md': Promise<{
Content: import('astro').MarkdownInstance<{}>['Content'];
headings: import('astro').MarkdownHeading[];
remarkPluginFrontmatter: Record<string, any>;
}>;
}
}

View file

@ -1,13 +1,3 @@
declare module 'astro:content' {
interface Render {
'.md': Promise<{
Content: import('astro').MarkdownInstance<{}>['Content'];
headings: import('astro').MarkdownHeading[];
remarkPluginFrontmatter: Record<string, any>;
}>;
}
}
declare module 'astro:content' {
export { z } from 'astro/zod';
export type CollectionEntry<C extends keyof typeof entryMap> =

View file

@ -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(),
@ -313,6 +312,7 @@ export function contentObservable(initialCtx: ContentCtx): ContentObservable {
}
export type ContentPaths = {
templateDir: URL;
contentDir: URL;
cacheDir: URL;
typesTemplate: URL;
@ -330,6 +330,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),

View file

@ -5,7 +5,6 @@ import { fileURLToPath, pathToFileURL } from 'url';
import jsxRenderer from '../../jsx/renderer.js';
import { createDefaultDevConfig } from './config.js';
import { loadTSConfig } from './tsconfig.js';
import { markdownContentEntryType } from '../../vite-plugin-markdown/content-entry-type.js';
export function createBaseSettings(config: AstroConfig): AstroSettings {
return {
@ -16,7 +15,7 @@ export function createBaseSettings(config: AstroConfig): AstroSettings {
adapter: undefined,
injectedRoutes: [],
pageExtensions: ['.astro', '.html', ...SUPPORTED_MARKDOWN_FILE_EXTENSIONS],
contentEntryTypes: [markdownContentEntryType],
contentEntryTypes: [],
renderers: [jsxRenderer],
scripts: [],
watchFiles: [],

View file

@ -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<T>({
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;
}

View file

@ -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,
};
},
};

View file

@ -9,12 +9,15 @@ declare module 'astro:content' {
}): Record<string, any>;
};
interface Render {
'.mdoc': Promise<{
type RenderResult = Promise<{
Content(props: {
config?: import('@astrojs/markdoc').MarkdocConfig;
components?: Record<string, ComponentRenderer>;
}): import('astro').MarkdownInstance<{}>['Content'];
}>;
interface Render {
'.md': RenderResult;
'.mdoc': RenderResult;
}
}