Here’s part two of my Apex posts for the day: Multiple image formats from a single image syntax.

Modern browsers support a bunch of image formats, like WebP and AVIF, that are smaller and sharper than good old PNG and JPEG. The trick is serving them without breaking older browsers. Apex makes that easy with a few attributes on your image syntax.

Apex isn’t going to generate the additional images themselves, of course, that’s up to you. But if you have a series of images in the same directory, inserting them properly is super simple now. Take a directory with:

  • image.png
  • image@2x.png
  • image.webp
  • image@2x.webp
  • image.avif
  • image@2x.avif

Now you can generate a <picture> (and optionally a <figure> with captions) with a single line of Markdown.

WebP and AVIF

Add webp or avif after the URL and Apex wraps the image in a <picture> element with the right <source> tags. Browsers that support the format use it; everyone else falls back to the main image.

![Hero image](img/hero.png webp)
![Hero AVIF](img/hero.png avif)

You can combine both for maximum compatibility:

![Modern formats](img/banner.jpg webp avif)

Both attributes work with @2x for retina. So ![Retina](img/hero.png webp @2x) produces a srcset with img.webp at 1x and img@2x.webp at 2x. Same deal for avif. Apex also recognizes @3x, if you need that.

Auto discovery

If you’d rather not list every format by hand, use the auto attribute:

![Profile menu](img/app-pass-1-profile-menu.jpg auto)

When base_directory is set (e.g. from the document path or --base-directory), Apex checks the filesystem for existing variants. For images it looks for 2x, 3x, webp, and avif. It only emits <source> elements for files that actually exist, so you can add formats over time without touching the Markdown.

You can also use * as the extension, which does the same thing:

![Profile menu](img/app-pass-1-profile-menu.*)

That scans for jpg, png, gif, webp, and avif (plus 2x and 3x variants) for images, and mp4, webm, ogg, mov, and m4v for videos. ![](image.*) is equivalent to ![](image.png auto).

Video: same syntax, different element

Video URLs in image syntax get special treatment. If the URL ends in mp4, mov, webm, ogg, ogv, or m4v, Apex emits a <video> tag instead of <img>:

![Demo video](media/demo.mp4)

That becomes <video><source src="media/demo.mp4" type="video/mp4"></video>. No extra syntax needed.

To add alternative formats for broader browser support, tack on the format name:

![Demo with WebM](media/demo.mp4 webm)
![Demo with OGG](media/intro.mp4 ogg)

Apex adds <source> elements for each format you specify. The primary URL (e.g. demo.mp4) stays as the fallback; the attributes add webm, ogg, or whatever before it. So ![](video.mp4 ogg) gives you an ogg source plus the mp4 fallback.

Like I said, using auto or demo.* as a video URL will generate markup for all the existing formats.

What This Means

It means proper, accessible markup, and optimized web pages. It won’t mean much if your output goal is PDF or DOCX, but it’s great for web production.

The auto and ` *` syntaxes mean you can write the Markdown once, and then every time you add a new format or resolution, the correct markup will be generated the next time you render, without touching the Markdown.

Quick reference

Syntax Result
![alt](url webp) <picture> with WebP source
![alt](url avif) <picture> with AVIF source
![alt](url webp @2x) WebP with retina srcset
![alt](url auto) Discovers formats from disk (requires base_directory)
![alt](url.*) Same as auto—scans jpg/png/gif/webp/avif and video formats
![alt](video.mp4) <video> with mp4 source
![alt](video.mp4 webm) <video> with webm + mp4 fallback

Same Markdown image syntax, better output. Check out the Apex wiki for more details.