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

img MDX component override is not used in Astro collections #7223

Closed
1 task
LexSwed opened this issue May 26, 2023 · 2 comments · Fixed by #8468
Closed
1 task

img MDX component override is not used in Astro collections #7223

LexSwed opened this issue May 26, 2023 · 2 comments · Fixed by #8468
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) feat: assets Related to the Assets feature (scope)

Comments

@LexSwed
Copy link

LexSwed commented May 26, 2023

What version of astro are you using?

the issue comes from @astrojs/mdx@0.19+

Are you using an SSR adapter? If so, which one?

Vercel

What package manager are you using?

npm

What operating system are you using?

Mac

What browser are you using?

Chrome

Describe the Bug

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 (for example, rendering the caption). The regression seems to come in some of @astrojs/mdx updates, reverting to 0.18 helps to mitigate the issue.

export const components = {
  p: (props) => <p  {...props} class="custom-p" />
  img: (props) => <img {...props} class="custom-style" />
}

In .mdx:

---
title: New post
---

export { components } from '../components';

![alt for image](assets/image.jpg)

p will be correctly picked up from components, but img will be ignored and astro image used instead.
Ideally, ![alt](image) would be parsed as a local image, so props include src, alt, width, height, etc. So that they can be passed to astro:assets/Image

Link to Minimal Reproducible Example

https://codesandbox.io/p/sandbox/eager-pascal-b30gwz?file=%2Fsrc%2Fpages%2Findex.astro%3A14%2C1

Participation

  • I am willing to submit a pull request for this issue.
@LexSwed
Copy link
Author

LexSwed commented Jun 1, 2023

Found a way to style it (jsx + Tailwind is only for example, can be Astro component with CSS)

export const components = {
  p: (props) => <p class="[&:has(>img:only-child)]:mb-10 [&:has(>img:only-child)]:mt-6 [&>img]:mx-auto [&>img]:rounded-lg [&>img]:shadow-md" />
}

Obviously, this is suboptimal, and still doesn't allow for customised rendering (like captions)

@joe-bell
Copy link
Sponsor Contributor

joe-bell commented Sep 3, 2023

Unfortunately, I just ran into this trying to upgrade to astro@3.0


For anyone stuck, here's a friendly reminder that you can still import components/images in the meantime:

---
title: "Example Post"
---

import MDXImage from "../../components/mdx/img.astro";
import exampleImage from "../../assets/images/blog/example-image.jpg";

Here's some text or whatever

<MDXImage alt="Description of image" src={exampleImage} />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) feat: assets Related to the Assets feature (scope)
Projects
None yet
4 participants