EmDash Picture
Responsive <picture> image catalog, AVIF/PNG variant resolver, and Astro/Svelte components for EmDash CMS.
@nodeart.io/emdash-pictureOr open Registry in your EmDash admin and search for this name.
Overview
EmDash Picture upgrades EmDash image fields and Portable Text articles to responsive <picture> elements with AVIF (image/avif) and lossless PNG (image/png) srcset ladders via EmDash's /_image endpoint.
Key Features
- No upscaling:
computeVariantDimensions()clamps every srcset candidate to known source bounds and omitswidth/heightattributes when source dimensions are unavailable. - Canonical
/_imageURLs: Buildshref,w,h,q,fin EmDash's canonical order and omitsqfor PNG so Cloudflare Images keeps PNG output lossless. - Safe pass-through: SVGs, GIFs, and third-party external URLs stay untouched as plain
<img>fallbacks. - Multi-segment local storage keys: Supports both flat ULID storage keys and safe provider prefixes such as
/_emdash/api/media/file/editorial-svg/.... - Typed preset catalogs: Define site-specific widths, fits (
contain/cover), AVIF quality, andsizesrules withdefineImageCatalog(), or useDEFAULT_IMAGE_CATALOG(hero,wide,narrow,card,thumbnail,avatar,logo,icon). - UI & Edge caching helpers: Ships
<MediaPicture>components for Astro and Svelte 5 plusisImmutableMediaRequest()for Cloudflare Worker / middleware edge caching.
Permissions
Plugins get only the capabilities they declare and you approve. Your EmDash admin lists these again before anything installs.
FAQ
Why omit q for PNG fallbacks?
Cloudflare Images treats f=png with q as quantized lossy PNG8. Omitting q produces crisp lossless PNG fallbacks while <source type="image/avif"> carries the lossy compressed AVIF candidates (q=76 by default).
How does it handle seeded or historical blog images missing dimensions?
When width and height are omitted, resolveMedia() emits the preset's responsive width ladder without fabricating HTML width/height attributes. If you enrich inline Portable Text image nodes from EmDash's MediaRepository (getDb()), <MediaPicture> automatically clamps the ladder to true source dimensions and emits exact width/height attributes to prevent CLS.
Can absolute media URLs be rewritten to relative /_emdash/api/media/file/... paths?
Yes. Pass allowedHosts: ['example.com', '*.example.com'] to resolveMedia(), resolvePictureRenderProps(), or <MediaPicture> so legacy absolute URLs on your own domains resolve cleanly across preview and production environments.
What does the public HTTP route return?
GET /_emdash/api/plugins/emdash-picture/resolve?src=<url>&preset=card validates inputs and returns JSON with media, sources, and img props for client-side or dynamic integrations.
Security
- Zero capabilities requested (
capabilities: [],allowedHosts: [],storage: {}). - Rejects non-HTTP(S)/relative protocols (
javascript:,data:, protocol-relative//), path traversal (..), empty key segments, and untrusted external/_imageorigins. - Bounded public route inputs (
widthsmax 16 integers capped at4000px,quality1..100,sizes/altmax 512 chars), returning HTTP 400 on invalid queries.
Changelog
0.1.2
- Added
allowedHostsprop support on Astro and Svelte<MediaPicture>components and generated TypeScript declarations. - Added
$mediaseed-wrapper unwrapping,previewUrlsupport (for custommediaProviders), localid/mediaIdfallback whensrcis stripped, and multi-segment safe storage keys (such aseditorial-svg/...). - Expanded documentation and EmDash Marketplace examples.
0.1.1
- Published directly as a publisher PDS blob in EmDash Marketplace and via GitHub Actions OIDC with Sigstore provenance on npm.
0.1.0
- Initial standalone release with
defineImageCatalog,resolveMedia,resolvePictureRenderProps,<MediaPicture>for Astro/Svelte, and/_emdash/api/plugins/emdash-picture/resolve.
Installation
- Install the npm package in your EmDash Astro project (alongside registry installation for the resolve route):
bun add @nodeart/emdash-plugin-picture
- Register the plugin in
astro.config.ts:
import { defineConfig } from 'astro/config';
import emdash from 'emdash/astro';
import { emdashPicture } from '@nodeart/emdash-plugin-picture';
export default defineConfig({
integrations: [emdash({ plugins: [emdashPicture()] })]
});
- Render media fields in Astro or Svelte using
<MediaPicture>:
---
import { MediaPicture } from '@nodeart/emdash-plugin-picture/astro';
import { DEFAULT_IMAGE_CATALOG, resolveMedia } from '@nodeart/emdash-plugin-picture/resolve-media';
const picture = resolveMedia(post.data.featured_image, ['card'], DEFAULT_IMAGE_CATALOG);
---
<MediaPicture media={picture} preset="card" alt={post.data.title} />
<script lang="ts">
import MediaPicture from '@nodeart/emdash-plugin-picture/svelte/MediaPicture.svelte';
import type { MediaProps } from '@nodeart/emdash-plugin-picture/resolve-media';
let { picture, title }: { picture: MediaProps | null; title: string } = $props();
</script>
<MediaPicture media={picture} preset="card" alt={title} />
- For blog/article Portable Text bodies, pass a serializer for
type.imagethat callsresolveMedia(mediaInput, [preset])and renders<MediaPicture>(falling back to<PTImage>for external providers).
Keep exploring
More plugins like EmDash Picture
Browse by keyword, or see everything @nodeart.io publishes.