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

markdown-it-image-size

v15.0.1

Published

Adds width and height to image tags rendered by markdown-it

Readme

markdown-it-image-size

npm version codecov

Automatically adds width and height attributes to img tags rendered by markdown-it. Supports both local and remote images.

If you'd rather set image sizes manually, check out @mdit/plugin-img-size.

Why

Browsers use the width and height attributes to determine aspect ratios of images. If the attributes are set, the browser can reserve space for the image even though it's not finished loading yet, thus preventing cumulative layout shifts after images load.

How to use

[!IMPORTANT]
Using Vite? Import markdown-it-image-size with require and not with import.

Basic usage

const MarkdownIt = require("markdown-it");
const { markdownItImageSize } = require("markdown-it-image-size");

const mdRenderer = MarkdownIt();
mdRenderer.use(markdownItImageSize);

Option: publicDir

Type: string Default: .

The publicDir option lets you specify a base URL for local images. This is useful when you're using a static site generator like Eleventy.

const MarkdownIt = require("markdown-it");
const { markdownItImageSize } = require("markdown-it-image-size");

const mdRenderer = MarkdownIt();
mdRenderer.use(markdownItImageSize, {
  publicDir: "/path/to/images",
});

Option: cache

Type: boolean Default: true

The cache option lets you disable caching of image dimensions. The cache is located in node_modules/markdown-it-image-size/.cache. Remove this directory to clear the cache.

const MarkdownIt = require("markdown-it");
const { markdownItImageSize } = require("markdown-it-image-size");

const mdRenderer = MarkdownIt();
mdRenderer.use(markdownItImageSize, {
  cache: false,
});

Option: overwriteAttrs

Type: boolean Default: false

The overwriteAttrs option lets you overwrite existing width and height attributes on img tags. This is useful when using another plugin which sets the attributes, such as @mdit/plugin-img-size.

const MarkdownIt = require("markdown-it");
const { markdownItImageSize } = require("markdown-it-image-size");
const { imgSize } = require("@mdit/plugin-img-size");

const mdRenderer = MarkdownIt();
mdRenderer
  .use(imgSize)
  .use(markdownItImageSize, {
    overwriteAttrs: true,
  });

const html = mdRenderer.render(`![alt text](/path/to/image.jpg =100x200)`);
console.log(html);

// The attributes are overwritten with the correct dimensions (350x700).
// <p><img src="/path/to/image.jpg" alt="alt text" width="350" height="700"></p>

Examples

See the demo directory for usage with Eleventy and Vitepress.

Development

This project uses tsup for bundling and Vitest for testing. It currently supports Node.js 20 and up. To get started, fork the repository and run the following commands:

# Install dependencies
npm install

# Build the project
npm run build

# Run tests
npm test

# Lint and format
npm run lint:fix

[!NOTE] The unit tests run on the build output, so make sure to run npm run build before running the tests if you have made changes to the source code.