Markdown images with light and dark variants
A lot of the images on this site are screenshots, and nearly all of them go into posts as plain Markdown images. I’ve just reworked how those are rendered so I can do almost everything I need with markdownish syntax, including showing a different image in light and dark mode.
The BasicImage component
<BasicImage> is a thin wrapper around Astro’s <Picture> component. It renders an image inside a <figure> with an optional caption and serves AVIF and WebP with a JPEG fallback. It’s deliberately pretty dumb and accepts the following props:
src– The image.alt– Alt text. Required.caption– Optional caption text. Children work too, and can contain markup.darkSrc/lightSrc– Images to show instead ofsrcin dark or light mode.width– The maximum display width in CSS pixels.framed– Shows the image in a frame.bleed–left,rightorfullto break out of the text column in articles.
Everything else is passed straight through to Astro’s <Picture> component.
I might use it like this:
import shot from '@assets/notes/screenshot.png'import shotDark from '@assets/notes/screenshot.dark.png'
<BasicImage src={shot} darkSrc={shotDark} alt="The settings panel" width={300}> The settings panel, with *markdown* in the caption</BasicImage>Here src is a light image and darkSrc is its dark twin, so the first shows in light mode and the second in dark mode. lightSrc is the same thing the other way round, for when the original image is a dark one. Both images end up in the page and a little CSS hides whichever one doesn’t match the current theme (or the OS setting, if JavaScript is off). The hidden one is lazy-loaded, so it isn’t downloaded unless you switch theme, and print and RSS feeds always get src. There are examples of all of this in the styleguide.
Markdown images
I almost never write <BasicImage> by hand, so a Sätteri plugin turns every Markdown image that sits on its own line (in an MDX file) into a BasicImage. So given we have a light screenshot.png and a screenshot.dark.png next to it writing this…
becomes the equivalent of this…
import screenshot from './screenshot.png'import screenshotDark from './screenshot.dark.png'
<BasicImage src={screenshot} darkSrc={screenshotDark} alt="The settings panel" width={300} framed> The settings panel, with *markdown* in the caption</BasicImage>To get there the plugin does four things:
- Takes options off the end of the alt text. A number like
500sets the width andframed,fullbleed,leftbleedorrightbleedset the appropriate props. - Turns the title into a caption, parsed as Markdown.
- Imports local images, so a wrong path fails the build. Remote URLs and files in
public/stay as strings and are hotlinked as a plain<img>. - Looks for light and dark variants. For
screenshot.pngit looks forscreenshot.dark.pngandscreenshot.light.png, and passes whichever exists asdarkSrcorlightSrc.
Here’s a screenshot from the Astro Editor website, which has a .dark file next to it. Switch theme and it should swap:

Inline images, linked images and plain .md files are left to Astro as usual.
This replaces the MDX img → BasicImage remapping and the two HAST plugins I mentioned in Upgrading to Astro 7 and switching to Sätteri, which mostly existed to stop images ending up inside <p> tags.