From 8e3f4ca430ce86df7722c4da16191080579ee4ee Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 13 Feb 2023 14:13:02 -0500 Subject: [PATCH] feat: content prop types for markdoc! --- packages/integrations/markdoc/src/index.ts | 29 ++++++++++--------- .../template/content-module-types.d.ts | 20 +++++++++++++ 2 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 packages/integrations/markdoc/template/content-module-types.d.ts diff --git a/packages/integrations/markdoc/src/index.ts b/packages/integrations/markdoc/src/index.ts index 7542bc015..abace45f8 100644 --- a/packages/integrations/markdoc/src/index.ts +++ b/packages/integrations/markdoc/src/index.ts @@ -5,24 +5,27 @@ import { parseFrontmatter } from './utils.js'; import { fileURLToPath } from 'node:url'; 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 { return { name: '@astrojs/markdoc', hooks: { '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); const markdocConfigUrl = new URL('./markdoc.config', config.srcDir); diff --git a/packages/integrations/markdoc/template/content-module-types.d.ts b/packages/integrations/markdoc/template/content-module-types.d.ts new file mode 100644 index 000000000..d32d292d2 --- /dev/null +++ b/packages/integrations/markdoc/template/content-module-types.d.ts @@ -0,0 +1,20 @@ +declare module 'astro:content' { + type ComponentRenderer = + | import('astro').ComponentInstance['default'] + | { + component: import('astro').ComponentInstance['default']; + props?(params: { + attributes: Record; + getTreeNode(): import('@markdoc/markdoc').Tag; + }): Record; + }; + + interface Render { + '.mdoc': Promise<{ + Content(props: { + config?: import('@markdoc/markdoc').Config; + components?: Record; + }): import('astro').MarkdownInstance<{}>['Content']; + }>; + } +}