Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 18 additions & 1 deletion src/content/docs/en/guides/content-collections.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ const posts = await getCollection('blog');
))}
</ul>
```
#### Rendering body content
#### Rendering body content

Once queried, you can render Markdown and MDX entries to HTML using the [`render()`](/en/reference/modules/astro-content/#render) function property. Calling this function gives you access to rendered HTML content, including both a `<Content />` component and a list of all rendered headings.

Expand All @@ -420,6 +420,23 @@ const { Content, headings } = await render(entry);
<Content />
```

##### Passing components to MDX Content

When rendering MDX entries, pass custom components to the `<Content />` component via the `components` prop:

```astro title="src/pages/blog/post-1.astro" ins="components={{ h1: CustomHeading }}"
---
import { getEntry, render } from 'astro:content';
import CustomHeading from '../components/CustomHeading.astro';

const entry = await getEntry('blog', 'post-1');
const { Content } = await render(entry);
---
<Content components={{ h1: CustomHeading }} />
```

The `components` object maps HTML element names (`h1`, `h2`, `blockquote`, etc.) to custom components. You can also merge in components exported from the MDX file itself using the spread operator.
Comment thread
sarah11918 marked this conversation as resolved.
Outdated

#### Passing content as props

A component can also pass an entire collection entry as a prop.
Expand Down
21 changes: 19 additions & 2 deletions src/content/docs/en/guides/integrations-guide/mdx.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,13 @@
<ReactCounter client:load />
```

#### Custom components with imported MDX
#### Rendering MDX with `<Content />`
Comment thread
sarah11918 marked this conversation as resolved.
Outdated

When rendering imported MDX content, [custom components](#assigning-custom-components-to-html-elements) can be passed via the `components` prop.
When rendering imported MDX content with the `<Content />` component, including rendering MDX blog posts using content collections, [custom components](#assigning-custom-components-to-html-elements) can be passed via the `components` prop. These components must first be imported, and then are available to the `<Content />` component.
Comment thread
sarah11918 marked this conversation as resolved.
Outdated

The `components` object maps HTML element names (`h1`, `h2`, `blockquote`, etc.) to your custom components. You can also include all components exported from the MDX file itself using the spread operator (`...`) which must also be imported from your MDX file as `components`.
Comment thread
sarah11918 marked this conversation as resolved.
Outdated

If you are importing MDX directly from a single file for use in an Astro component, import both the `Content` component and any exported components.

```astro title="src/pages/page.astro" "components={{...components, h1: Heading }}"
Comment thread
sarah11918 marked this conversation as resolved.
Outdated
---
Expand All @@ -198,6 +202,19 @@
<Content components={{...components, h1: Heading }} />
```

If your MDX file is a content collections entry, then use the content collections `render()` function to access the `<Content />` component.
Comment thread
sarah11918 marked this conversation as resolved.
Outdated

The following example passes a custom heading to the `<Content />` component via the `components` prop to be used in place of all `<h1>` HTML elements:
```astro title="src/pages/blog/post-1.astro" ins="components={{ h1: CustomHeading }}"
Comment thread
sarah11918 marked this conversation as resolved.
---
import { getEntry, render } from 'astro:content';
import CustomHeading from '../../components/CustomHeading.astro';
const entry = await getEntry('blog', 'post-1');
const { Content } = await render(entry);
---
<Content components={{ h1: CustomHeading }} />
```

:::note
Custom components defined and exported in an MDX file must be imported and then passed back to the `<Content />` component via the `components` property.
:::
Expand Down Expand Up @@ -378,7 +395,7 @@

Previously known as `customComponentNames`.

An optional property of `optimize` to prevent the MDX optimizer from handling certain element names, like [custom components passed to imported MDX content via the components prop](/en/guides/integrations-guide/mdx/#custom-components-with-imported-mdx).

Check failure on line 398 in src/content/docs/en/guides/integrations-guide/mdx.mdx

View workflow job for this annotation

GitHub Actions / Check Links

Broken fragment link in src/content/docs/en/guides/integrations-guide/mdx.mdx, line 398: The linked page does not contain a fragment with the name "#custom-components-with-imported-mdx". Available fragments: #theme-icons, #gradient, #starlight__sidebar, #__tab-start, #__tab-guides-and-recipes, #__tab-reference, #__tab-integrations, #__tab-third-party-services, #fbs-gradient-a-23792, #fbs-gradient-b-23792, #fbs-gradient-a-66691, #fbs-gradient-b-66691, #starlight__mobile-toc, #starlight__on-this-page--mobile, #starlight__on-this-page, #learn-astro-course-2, #_top, #why-mdx, #installation, #tab-3343, #tab-3344, #tab-3345, #tab-panel-3343, #tab-panel-3344, #tab-panel-3345, #manual-install, #tab-3346, #tab-3347, #tab-3348, #tab-panel-3346, #tab-panel-3347, #tab-panel-3348, #editor-integration, #usage, #mdx-in-astro, #using-mdx-with-content-collections, #using-exported-variables-in-mdx, #exported-properties, #using-frontmatter-variables-in-mdx, #using-components-in-mdx, #rendering-mdx-with-content-, #assigning-custom-components-to-html-elements, #configuration, #options-inherited-from-markdown-config, #extendmarkdownconfig, #recmaplugins, #optimize, #ignoreelementnames, #examples, #docsearch-lvl0, #learn-astro-course-1

You will need to exclude these components from optimization as the optimizer eagerly converts content into a static string, which will break custom components that needs to be dynamically rendered.

Expand Down
Loading