Skip to content

Commit

Permalink
feat: content prop types for markdoc!
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Feb 16, 2023
1 parent 8bcd23c commit 815e2f2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
29 changes: 16 additions & 13 deletions packages/integrations/markdoc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions packages/integrations/markdoc/template/content-module-types.d.ts
Original file line number Diff line number Diff line change
@@ -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'];
}>;
}
}

0 comments on commit 815e2f2

Please sign in to comment.