Skip to content

Commit

Permalink
feat: generate .md types override
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Feb 16, 2023
1 parent e464ae8 commit 5b9a1f3
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 39 deletions.
1 change: 1 addition & 0 deletions packages/astro/src/content/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
1 change: 1 addition & 0 deletions packages/astro/src/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
29 changes: 29 additions & 0 deletions packages/astro/src/content/markdown.ts
Original file line number Diff line number Diff line change
@@ -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'
),
};
}
9 changes: 9 additions & 0 deletions packages/astro/src/content/template/markdown-types.d.ts
Original file line number Diff line number Diff line change
@@ -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>;
}>;
}
}
10 changes: 0 additions & 10 deletions packages/astro/src/content/template/types.d.ts
Original file line number Diff line number Diff line change
@@ -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> =
Expand Down
5 changes: 3 additions & 2 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -313,6 +312,7 @@ export function contentObservable(initialCtx: ContentCtx): ContentObservable {
}

export type ContentPaths = {
templateDir: URL;
contentDir: URL;
cacheDir: URL;
typesTemplate: URL;
Expand All @@ -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),
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/core/config/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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: [],
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/src/integrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -126,6 +128,12 @@ export async function runHookConfigSetup({
}

updatedSettings.config = updatedConfig;
if (!hasMdContentEntryTypeOverride(updatedSettings)) {
updatedSettings.contentEntryTypes.push(
await getMarkdownContentEntryType(updatedSettings.config, fs)
);
}

return updatedSettings;
}

Expand Down
19 changes: 0 additions & 19 deletions packages/astro/src/vite-plugin-markdown/content-entry-type.ts

This file was deleted.

15 changes: 9 additions & 6 deletions packages/integrations/markdoc/template/content-module-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ declare module 'astro:content' {
}): Record<string, any>;
};

type RenderResult = Promise<{
Content(props: {
config?: import('@astrojs/markdoc').MarkdocConfig;
components?: Record<string, ComponentRenderer>;
}): import('astro').MarkdownInstance<{}>['Content'];
}>;

interface Render {
'.mdoc': Promise<{
Content(props: {
config?: import('@astrojs/markdoc').MarkdocConfig;
components?: Record<string, ComponentRenderer>;
}): import('astro').MarkdownInstance<{}>['Content'];
}>;
'.md': RenderResult;
'.mdoc': RenderResult;
}
}

0 comments on commit 5b9a1f3

Please sign in to comment.