Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/mdx-content-component-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/mdx': patch
---

Adds TypeScript support for the `components` prop on MDX `Content` component when using `await render()`. Developers now get proper IntelliSense and type checking when passing custom components to override default MDX element rendering.
29 changes: 29 additions & 0 deletions packages/astro/src/types/public/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,35 @@ export interface MDXInstance<T extends Record<string, any>>
components: Record<string, AstroComponentFactory> | undefined;
}

/**
* Props accepted by the MDX `Content` component when using `await render()`.
* Allows passing custom components to override default MDX element rendering.
*
* @example
* ```astro
* ---
* import { getEntry, render } from 'astro:content';
* import MyCustomH1 from '../components/CustomHeading.astro';
* import MyImage from '../images/MyImage.jpg';
* const entry = await getEntry('blog', 'post');
* const { Content } = await render(entry);
* ---
* <Content components={{ h1: MyCustomH1, img: MyImage }} />
* ```
*/
export interface MDXContentProps {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling @ArmandPhilippot -- I don't think we document Markdown or MDX types, really. So, nothing to update, but is this something we should have?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's useful to document these types like we do the ones where we expect users to import them into their project. They seem more useful internally than in a user project. And I don't recall seeing users complain those types are not documented/would have been useful.

We document MarkdownInstance in plain text in Importing Markdown (without the type name). Well, I guess we could move/repeat this in the Import statements reference. And, we will document components for MDX.

So, I think it's fine as it is!

/** Custom components to use for MDX elements (e.g., h1, h2, img, a, etc.) */
components?: Record<string, AstroComponentFactory>;
/** Any additional props to pass to the MDX content */
[key: string]: any;
}

/**
* The MDX `Content` component returned from `await render()`.
* Extends `AstroComponentFactory` with typed props for better developer experience.
*/
export type MDXContent = AstroComponentFactory & ((props?: MDXContentProps) => any);

export interface MarkdownLayoutProps<T extends Record<string, any>> {
frontmatter: {
file: MarkdownInstance<T>['file'];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare module 'astro:content' {
interface Render {
'.mdx': Promise<{
Content: import('astro').MarkdownInstance<{}>['Content'];
Content: import('astro').MDXContent;
headings: import('astro').MarkdownHeading[];
remarkPluginFrontmatter: Record<string, any>;
components: import('astro').MDXInstance<{}>['components'];
Expand Down
Loading