rehype-picture

rehype plugin to wrap images in pictures

Usage no npm install needed!

<script type="module">
  import rehypePicture from 'https://cdn.skypack.dev/rehype-picture';
</script>

README

rehype-picture

Build Coverage Downloads Size Sponsors Backers Chat

rehype plugin to wrap images in pictures.

Contents

What is this?

This package is a unified (rehype) plugin to change images (<img>) into pictures (<picture>). This lets you use a single image source in your content which is then automatically turned into a picture with several sources.

unified is a project that transforms content with abstract syntax trees (ASTs). rehype adds support for HTML to unified. hast is the HTML AST that rehype uses. This is a rehype plugin that changes images in the tree.

When should I use this?

This plugin is useful when you have the same images in different file formats. For example, when you have a build step that generates alternative image files (say, .webps) from source files (say, .jpgs). Then, when you link to the source images, you can use this plugin to generate the markup for both.

Install

This package is ESM only. In Node.js (version 12.20+, 14.14+, or 16.0+), install with npm:

npm install rehype-picture

In Deno with Skypack:

import rehypePicture from 'https://cdn.skypack.dev/rehype-picture@4?dts'

In browsers with Skypack:

<script type="module">
  import rehypePicture from 'https://cdn.skypack.dev/rehype-picture@4?min'
</script>

Use

import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import rehypePicture from 'rehype-picture'
import rehypeStringify from 'rehype-stringify'

main()

async function main() {
  const file = await unified()
    .use(rehypeParse, {fragment: true})
    .use(rehypePicture, {
      jpg: {webp: 'image/webp'},
      png: {svg: 'image/svg+xml'}
    })
    .use(rehypeStringify)
    .process('<img src="cat.jpg">\n<img src="logo.png">')

  console.log(String(file))
}

Yields:

<picture><source srcset="cat.webp" type="image/webp"><img src="cat.jpg"></picture>
<picture><source srcset="logo.svg" type="image/svg+xml"><img src="logo.png"></picture>

API

This package exports no identifiers. The default export is rehypePicture.

unified().use(rehypePicture[, options])

Wrap images in pictures.

options

Configuration that maps search extensions (without dot) to sources (Record<string, Sources>?).

Sources

Object mapping new extensions (without dot) to mime types.

Types

This package is fully typed with TypeScript. It exports Options and Sources types, which specify the interfaces of the accepted values.

Compatibility

Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.

This plugin works with rehype-parse version 3+, rehype-stringify version 3+, rehype version 4+, and unified version 6+.

Security

Although this plugin should be safe to use, be careful with user input images as it’s often possible to hide JavaScript inside them (such as GIFs, WebPs, and SVGs). User provided images open you up to a cross-site scripting (XSS) attack.

Contribute

See contributing.md in rehypejs/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer