npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

rehype-resolution

v1.0.2

Published

Insert srcset attribute into img elements

Downloads

33

Readme

Build Status Coverage Status

rehype-resolution

The rehype-resolution Node.js package provides a rehype plugin for inserting srcset attributes into img elements. rehype is a unified HTML processor.

The srcset attribute of the img element enables responsive images. This plugin inserts a srcset for resolution switching, allowing different resolutions at the same size.

If you're supporting multiple display resolutions, but everyone sees your image at the same real-world size on the screen, you can allow the browser to choose an appropriate resolution image by using srcset with x-descriptors and without sizes — a somewhat easier syntax!

The rehype-resolution plugin inserts a set of possible image sources (@1x, @2x, and @3x), following Apple’s naming convention for image size and resolution. Users have to produce these images and make them available at the according paths.

The transformation is only applied for file names ending with @1x.*. That’s the marker for the transform. Elements with an existing srcset attribute are ignored.

Motivation

Generating a static website from Markdown, I want crisp images on all devices.

Inserting the srcset attribute

We have an image at three resolutions, [email protected], [email protected], and [email protected]. Let’s assume we don’t control the HTML, maybe it’s generated from Markdown, leaving us with HTML like this.

<img src="img/[email protected]" alt="Dressed as a fairy">

To let the browser pick the optimal image, this transform inserts the srcset attribute for three resolutions, allowing us to keep referencing the image with a single URI in our Markdown or whatever HTML source we might have.

<img srcset="img/[email protected],img/[email protected],img/[email protected]" src="img/[email protected]" alt="Dressed as a fairy">

Example

Inserts srcset into HTML fragment.

const parse = require('rehype-parse')
const resolution = require('./')
const stringify = require('rehype-stringify')
const unified = require('unified')
const { log } = console

unified()
  .use(parse, { fragment: true })
  .use(resolution)
  .use(stringify)
  .process('<img src="[email protected]">', (er, file) => {
    if (er) throw er

    const { contents } = file

    log(contents)
  })

Try it.

$ node example.js
<img src="[email protected]" srcset="[email protected] 1x, [email protected] 2x, [email protected] 3x">

Installing

To install rehype-resolution with npm, do:

$ npm install rehype-resolution

License

MIT License