No results

For AI agents and crawlers: a structured index of this site is available at https://danny.is/llms.txt.

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 of src in dark or light mode.
  • width – The maximum display width in CSS pixels.
  • framed – Shows the image in a frame.
  • bleed – left, right or full to 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…

![The settings panel|framed|300](./screenshot.png "The settings panel, with *markdown* in the caption")

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:

  1. Takes options off the end of the alt text. A number like 500 sets the width and framed, fullbleed, leftbleed or rightbleed set the appropriate props.
  2. Turns the title into a caption, parsed as Markdown.
  3. 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>.
  4. Looks for light and dark variants. For screenshot.png it looks for screenshot.dark.png and screenshot.light.png, and passes whichever exists as darkSrc or lightSrc.

Here’s a screenshot from the Astro Editor website, which has a .dark file next to it. Switch theme and it should swap:

![A Metadata group in Astro Editor's frontmatter panel…|471](../../assets/notes/2026-12-04-ae-nested-field.png)
A Metadata group in Astro Editor’s frontmatter panel, with Category, Priority and Deadline fields A Metadata group in Astro Editor’s frontmatter panel, with Category, Priority and Deadline fields

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.