Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom components to mdx integration guide #4530

Merged
merged 9 commits into from
Aug 30, 2022
48 changes: 48 additions & 0 deletions packages/integrations/mdx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,54 @@ const { title, fancyJsHelper } = Astro.props;
<!-- -->
```

### Custom components

Under the hood, MDX will convert Markdown into HTML components. For example, this blockquote:

```md
> A blockquote with *some* emphasis.
```

will be converted into this HTML:

```html
<blockquote>
<p>A blockquote with <em>some</em> emphasis.</p>
</blockquote>
```

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 has either a `<slot />` component or accepts a `children` prop. MDX provides a way to tap into this rendering and use your own custom components. In the above example, you could create a custom Blockquote component (in any language) that has either a `<slot />` component or accepts a `children` prop.
Copy link
Member

@sarah11918 sarah11918 Aug 30, 2022

Choose a reason for hiding this comment

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

Suggested change
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 has either a `<slot />` component or accepts a `children` prop. MDX provides a way to tap into this rendering and use your own custom components. In the above example, you could create a custom Blockquote component (in any language) that has either a `<slot />` component or accepts a `children` prop.
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.

Thanks for incorporating the comments @kylebutts! It looks like there's some copy here twice? So I'll clean this up and then happy for you and @bholmesdev to edit for nuance/reasonableness.

Note, making some editorial decisions based strictly on formatting here, so not meant to disapprove of any previous language. I just don't like how it looks to write plural tags and stuff.

EDIT: Now I care less about changing "blockquotes" because that's how we would refer to the Markdown syntax. So, I'm reverting that. (Change it to <blockquote> elements if you prefer, but it's fine as is.)


```astro title="src/components/Blockquote.astro"
<blockquote class="bg-blue-50 p-4">
<span class="text-4xl text-blue-600 mb-2">“</span>
<slot />
</blockquote>
```

Then in the MDX file you import the component and export it to the `components` export.

```mdx title="src/pages/posts/post-1.mdx" {2}
import Blockquote from '../components/Blockquote.astro';
export const components = { blockquote: Blockquote };
```

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.


#### Custom components with imported `mdx`

When rendering imported MDX content, custom components can also be passed via the `components` prop:

```astro title="src/pages/page.astro" "components={{ h1: Heading }}"
---
import Content from '../content.mdx';
import Heading from '../Heading.astro';
---

<Content components={{ h1: Heading }} />
```

### Syntax highlighting

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