Skip to main content

Adding Missing Attributes to Markdown Images with Rehype

  • Reduce image dimensions no larger than necessary for the user’s viewport
  • Use the best image format available in the user’s browser
  • Avoid Cumulative Layout Shift (CLS) when the page loads by immediately rendering a placeholder that matches the dimensions of the incoming image

Why Cloudinary images instead of Astro/Gatsby/Eleventy images?

  • Build times remain as fast as possible (in my experience with Gatsby, image processing via sharp greatly increased site build times)
    • I have a Likes page with hundreds of images, so waiting minutes for that page to render every time I push a copy update is not what I want
  • Smaller images delivered
  • Generous free tier
  • Should I use CloudFlare images instead Is it cheaper like Kent C. Dodds says?
    • Should I use their hosting as well?

Creating a Rehype plugin

  • Builds a Cloudinary URL
  • Renders the appropriate HTML markup based on whether a caption is included
  • Pulls image dimensions from Cloudinary?
  • Pulls image alt text from Cloudinary?
  • Accepts just the image nickname/id and doesn’t require the full URL to be entered in the shortcode?
  • Throws a build-time error if no image by that name is found in Cloudinary?
  • Don’t include dpr_2.0srcset works better with just supplying more images for multiple DPRs and let the browser automatically request the right one
    • I was confused about why the browser was requesting the images it was until I removed dpr_2.0

Inbox

Placeholders (follow up feature)

Further reading

Inbox