#Custom Components with Content Collections

9 messages · Page 1 of 1 (latest)

tawny falcon
#

Is there a way to use Custom Components when using a content collection? I'm trying to replace all img tags with a custom <Picture /> element.

Astro Documentation

Learn how to create content using Markdown or MDX with Astro

full sailBOT
#

Hi @tawny falcon. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, you can use custom components in your content collections in Astro. You can map Markdown syntax to custom components instead of their standard HTML elements. This allows you to write in standard Markdown syntax, but apply special component styling to selected elements.

Here's an example of how you can do this:

First, import your custom component into your .mdx file, then export a components object that maps the standard HTML element to your custom component:

import Picture from '../components/Picture.astro';
export const components = {img: Picture}

In this case, every img tag in your .mdx file will be replaced with your custom Picture component.

However, there's a known bug in Astro where the img MDX component override is not used in Astro collections. When providing custom components for MDX file in content collections, !**alt** (image.jpg) will always be replaced by astro:assets Image component, hence not allowing to style it or customise the behaviour. The regression seems to come in some of @astrojs/mdx updates, reverting to 0.18 helps to mitigate the issue. You can track the progress of this issue on GitHub (https://github.com/withastro/astro/issues/7223).

Also, if you're using the optimize configuration in your Astro setup, you might need to prevent the MDX optimizer from handling any custom components passed to imported MDX content via the components prop. You can do this by specifying an array of HTML element names that should be treated as custom components in the customComponentNames property:

import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';
#

export default defineConfig({
integrations: [
mdx({
optimize: {
// Prevent the optimizer from handling img elements
// These will be treated as custom components
customComponentNames: ['img'],
},
}),
],
});


Please note that if your MDX file configures custom components using `export const components = { ... }`, then you do not need to manually configure this option. The optimizer will automatically detect them.

Sources:
- **Custom Components with Imported MDX** (<https://docs.astro.build/en/guides/markdown-content/#custom-components-with-imported-mdx>)
- **GitHub Issue for Known Bug** (<https://github.com/withastro/astro/issues/7223>)
- **Configuration > `optimize` > `customComponentNames`** (<https://docs.astro.build/en/guides/integrations-guide/mdx/>)
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
#

@tawny falcon kapa.ai is still learning and improving, please let us know how it did by reacting below

tawny falcon
#

@full sail is there a way to override the components by default, rather than having to manually import the components in every mdx file?

full sailBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, there is a way to override components by default in Astro. Starting from Astro v0.21.0, you can import components inside of your Markdown frontmatter via an optional setup script. Once imported, your components can be used anywhere on the page. Here's an example:

---
title: "Astro 0.21 Early Preview"
setup: |
 import Picture from '../components/Picture.astro';
---

# Astro now supports components in Markdown!

<Picture />

- Back to markdown here.
- Supports static Astro components.

In this example, the Picture component is imported in the setup script and can be used anywhere on the page. This feature was designed for maximum flexibility and will continue to be improved with planned support for default components, default layouts, and markdown component overrides.

However, please note that this feature is available starting from Astro v0.21.0. If you're using an older version of Astro, you might need to upgrade to use this feature.

Source: Astro 0.21 Preview: Vite + WASM = ⚡️ (https://astro.build/blog/astro-021-preview/)

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@tawny falcon kapa.ai is still learning and improving, please let us know how it did by reacting below