feat: content prop types for markdoc!

This commit is contained in:
bholmesdev 2023-02-13 14:13:02 -05:00
parent 576b481adb
commit 8e3f4ca430
2 changed files with 36 additions and 13 deletions

View file

@ -5,24 +5,27 @@ import { parseFrontmatter } from './utils.js';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import fs from 'node:fs'; import fs from 'node:fs';
const contentEntryType = {
extensions: ['.mdoc'],
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,
};
},
};
export default function markdoc(): AstroIntegration { export default function markdoc(): AstroIntegration {
return { return {
name: '@astrojs/markdoc', name: '@astrojs/markdoc',
hooks: { hooks: {
'astro:config:setup': async ({ updateConfig, config, addContentEntryType, command }: any) => { 'astro:config:setup': async ({ updateConfig, config, addContentEntryType, command }: any) => {
const contentEntryType = {
extensions: ['.mdoc'],
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('../template/content-module-types.d.ts', import.meta.url),
'utf-8'
),
};
addContentEntryType(contentEntryType); addContentEntryType(contentEntryType);
const markdocConfigUrl = new URL('./markdoc.config', config.srcDir); const markdocConfigUrl = new URL('./markdoc.config', config.srcDir);

View file

@ -0,0 +1,20 @@
declare module 'astro:content' {
type ComponentRenderer =
| import('astro').ComponentInstance['default']
| {
component: import('astro').ComponentInstance['default'];
props?(params: {
attributes: Record<string, any>;
getTreeNode(): import('@markdoc/markdoc').Tag;
}): Record<string, any>;
};
interface Render {
'.mdoc': Promise<{
Content(props: {
config?: import('@markdoc/markdoc').Config;
components?: Record<string, ComponentRenderer>;
}): import('astro').MarkdownInstance<{}>['Content'];
}>;
}
}