-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MDX] Support
img
component prop for optimized images (#8468)
Co-authored-by: Erika <[email protected]>
- Loading branch information
1 parent
ecc65ab
commit a8d72ce
Showing
11 changed files
with
177 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
'@astrojs/mdx': minor | ||
--- | ||
|
||
Support the `img` component export for optimized images. This allows you to customize how optimized images are styled and rendered. | ||
|
||
When rendering an optimized image, Astro will pass the `ImageMetadata` object to your `img` component as the `src` prop. For unoptimized images (i.e. images using URLs or absolute paths), Astro will continue to pass the `src` as a string. | ||
|
||
This example handles both cases and applies custom styling: | ||
|
||
```astro | ||
--- | ||
// src/components/MyImage.astro | ||
import type { ImageMetadata } from 'astro'; | ||
import { Image } from 'astro:assets'; | ||
type Props = { | ||
src: string | ImageMetadata; | ||
alt: string; | ||
}; | ||
const { src, alt } = Astro.props; | ||
--- | ||
{ | ||
typeof src === 'string' ? ( | ||
<img class="custom-styles" src={src} alt={alt} /> | ||
) : ( | ||
<Image class="custom-styles" {src} {alt} /> | ||
) | ||
} | ||
<style> | ||
.custom-styles { | ||
border: 1px solid red; | ||
} | ||
</style> | ||
``` | ||
|
||
Now, this components can be applied to the `img` component props object or file export: | ||
|
||
```md | ||
import MyImage from '../../components/MyImage.astro'; | ||
|
||
export const components = { img: MyImage }; | ||
|
||
# My MDX article | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/integrations/mdx/test/fixtures/mdx-images/public/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions
5
packages/integrations/mdx/test/fixtures/mdx-images/src/components/Component.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Optimized image: | ||
![Houston](../assets/houston.webp) | ||
|
||
Public image: | ||
![Astro logo](/favicon.svg) |
25 changes: 25 additions & 0 deletions
25
packages/integrations/mdx/test/fixtures/mdx-images/src/components/MyImage.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
import type { ImageMetadata } from 'astro'; | ||
import { Image } from 'astro:assets'; | ||
type Props = { | ||
src: string | ImageMetadata; | ||
alt: string; | ||
}; | ||
const { src, alt } = Astro.props; | ||
--- | ||
|
||
{ | ||
typeof src === 'string' ? ( | ||
<img data-my-image src={src} alt={alt} /> | ||
) : ( | ||
<Image data-my-image {src} {alt} /> | ||
) | ||
} | ||
|
||
<style> | ||
[data-my-image] { | ||
border: 1px solid red; | ||
} | ||
</style> |
5 changes: 5 additions & 0 deletions
5
packages/integrations/mdx/test/fixtures/mdx-images/src/content/blog/entry.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Optimized image: | ||
![Houston](../../assets/houston.webp) | ||
|
||
Public image: | ||
![Astro logo](/favicon.svg) |
19 changes: 19 additions & 0 deletions
19
packages/integrations/mdx/test/fixtures/mdx-images/src/pages/content-collection.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
import { getEntry } from 'astro:content'; | ||
import MyImage from 'src/components/MyImage.astro'; | ||
const entry = await getEntry('blog', 'entry'); | ||
const { Content } = await entry.render(); | ||
--- | ||
|
||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Renderer</title> | ||
</head> | ||
<body> | ||
<Content components={{ img: MyImage }} /> | ||
</body> | ||
</html> |
16 changes: 16 additions & 0 deletions
16
packages/integrations/mdx/test/fixtures/mdx-images/src/pages/esm-import.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
import MDX from '../components/Component.mdx'; | ||
import MyImage from 'src/components/MyImage.astro'; | ||
--- | ||
|
||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Renderer</title> | ||
</head> | ||
<body> | ||
<MDX components={{ img: MyImage }} /> | ||
</body> | ||
</html> |
9 changes: 9 additions & 0 deletions
9
packages/integrations/mdx/test/fixtures/mdx-images/src/pages/with-components.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import MyImage from '../components/MyImage.astro'; | ||
|
||
export const components = { img: MyImage }; | ||
|
||
Optimized image: | ||
![Houston](../assets/houston.webp) | ||
|
||
Public image: | ||
![Astro logo](/favicon.svg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters