Skip to content

Commit 8504cd7

Browse files
kylebuttsbholmesdevsarah11918
authored
Add custom components to mdx integration guide (#4530)
* Add custom components to mdx integration guide * Update packages/integrations/mdx/README.md Co-authored-by: Ben Holmes <[email protected]> * Update packages/integrations/mdx/README.md Co-authored-by: Ben Holmes <[email protected]> * Update packages/integrations/mdx/README.md Co-authored-by: Ben Holmes <[email protected]> * Update packages/integrations/mdx/README.md Co-authored-by: Ben Holmes <[email protected]> * Incorporate Sarah and Ben's Feedback * Fix what would be an ugly background lol * Sarah taking liberty of removing double text * Add changeset Co-authored-by: Ben Holmes <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]>
1 parent 65cc3f6 commit 8504cd7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

.changeset/kind-lobsters-leave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/mdx': patch
3+
---
4+
5+
Add custom components to README

packages/integrations/mdx/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,54 @@ const { title, fancyJsHelper } = Astro.props;
249249
<!-- -->
250250
```
251251

252+
### Custom components
253+
254+
Under the hood, MDX will convert Markdown into HTML components. For example, this blockquote:
255+
256+
```md
257+
> A blockquote with *some* emphasis.
258+
```
259+
260+
will be converted into this HTML:
261+
262+
```html
263+
<blockquote>
264+
<p>A blockquote with <em>some</em> emphasis.</p>
265+
</blockquote>
266+
```
267+
268+
But what if you want to specify your own markup for these blockquotes? In the above example, you could create a custom `<Blockquote />` component (in any language) that either has a `<slot />` component or accepts a `children` prop.
269+
270+
```astro title="src/components/Blockquote.astro"
271+
<blockquote class="bg-blue-50 p-4">
272+
<span class="text-4xl text-blue-600 mb-2">“</span>
273+
<slot />
274+
</blockquote>
275+
```
276+
277+
Then in the MDX file you import the component and export it to the `components` export.
278+
279+
```mdx title="src/pages/posts/post-1.mdx" {2}
280+
import Blockquote from '../components/Blockquote.astro';
281+
export const components = { blockquote: Blockquote };
282+
```
283+
284+
Now, writing the standard Markdown blockquote syntax (`>`) will use your custom `<Blockquote />` component instead. No need to use a component in Markdown, or write a remark/rehype plugin! Visit the [MDX website](https://mdxjs.com/table-of-components/) for a full list of HTML elements that can be overwritten as custom components.
285+
286+
287+
#### Custom components with imported `mdx`
288+
289+
When rendering imported MDX content, custom components can also be passed via the `components` prop:
290+
291+
```astro title="src/pages/page.astro" "components={{ h1: Heading }}"
292+
---
293+
import Content from '../content.mdx';
294+
import Heading from '../Heading.astro';
295+
---
296+
297+
<Content components={{ h1: Heading }} />
298+
```
299+
252300
### Syntax highlighting
253301

254302
The MDX integration respects [your project's `markdown.syntaxHighlight` configuration](https://docs.astro.build/en/guides/markdown-content/#syntax-highlighting).

0 commit comments

Comments
 (0)