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 🙏

© 2026 – Pkg Stats / Ryan Hefner

rehype-grouping-figure

v0.1.0

Published

rehype plugin that groups images followed by blockquote into a figure and figcaption

Readme

rehype-grouping-figure

rehype plugin that groups one or more images into a <figure> when they are immediately followed by a <blockquote>, turning that blockquote into a <figcaption>.

Contents

  • What is this?
  • When should I use this?
  • Install
  • Use
  • API
    • unified().use(rehypeGroupingFigure[, options])
  • Examples
  • Types
  • Compatibility
  • Security
  • License

What is this?

This is a unified (rehype) plugin.

It walks the hast tree and looks for this pattern:

  1. A paragraph (<p>) that contains only image elements (<img>) and optional whitespace text nodes
  2. Immediately followed by a blockquote (<blockquote>) (whitespace text nodes between them are ignored)

When found, it rewrites both nodes into:

  • <figure class="group">
  • all matched <img> elements inside the figure
  • <figcaption> containing the original blockquote children

When should I use this?

Use this plugin when your markdown/HTML authoring style places captions in blockquotes after image-only paragraphs, and you want semantic figure markup in output HTML.

You probably shouldn't use it if:

  • your captions are not represented as blockquotes
  • you need configurable class names or matching rules (the current API is intentionally minimal)

Install

This package is ESM only. In Node.js (version 20.19+):

npm install rehype-grouping-figure
pnpm add rehype-grouping-figure

Use

Say we have the following markdown:

![A beautiful cat](./cat.jpg)

> This is a caption for the cat image

...and a script example.js:

import {unified} from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeStringify from 'rehype-stringify'
import rehypeGroupingFigure from 'rehype-grouping-figure'

const file = await unified()
  .use(remarkParse)
  .use(remarkRehype)
  .use(rehypeGroupingFigure)
  .use(rehypeStringify)
  .process('![A beautiful cat](./cat.jpg)\n\n> This is a caption for the cat image')

console.log(String(file))

...running node example.js yields:

<figure class="group"><img src="./cat.jpg" alt="A beautiful cat"><figcaption><p>This is a caption for the cat image</p></figcaption></figure>

API

This package exports:

  • the default identifier rehypeGroupingFigure
  • the named schema export rehypeGroupingFigureSanitizeSchema
  • the TypeScript type Options

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

Groups image-only paragraphs followed by blockquotes into figure/figcaption markup.

Parameters
  • options (Options, optional) — currently empty; reserved for future configuration
Returns

Transformer function.

rehypeGroupingFigureSanitizeSchema

Schema extension object for rehype-sanitize so generated figure / figcaption markup and figure.group are allowed.

Examples

Two images + caption

Input:

![A beautiful cat](./cat.jpg)
![A playful dog](./dog.jpg)

> These are our beloved pets

Output:

<figure class="group">
  <img src="./cat.jpg" alt="A beautiful cat">
  <img src="./dog.jpg" alt="A playful dog">
  <figcaption>
    <p>These are our beloved pets</p>
  </figcaption>
</figure>

Multi-paragraph captions are preserved

Input:

![Nature scene](./nature.jpg)

> This is a multi-line caption.
>
> It spans multiple paragraphs and provides
> detailed information about the image.
>
> **Bold text** and *italic text* are also supported.

Output figcaption keeps all blockquote content structure.

Not grouped when content appears in between

If any non-whitespace content appears between the image paragraph and blockquote, no grouping happens.

Types

This package is fully typed with TypeScript.

Compatibility

  • Node.js 20.19+
  • unified 11+

Security

This plugin only reshapes existing HTML structure (p + img + blockquote -> figure + figcaption) and does not fetch external data or execute code.

If you sanitize downstream with rehype-sanitize, merge in the exported schema:

import rehypeSanitize, {defaultSchema} from 'rehype-sanitize'
import rehypeGroupingFigure, {
  rehypeGroupingFigureSanitizeSchema
} from 'rehype-grouping-figure'

unified()
  .use(rehypeGroupingFigure)
  .use(rehypeSanitize, {
    ...defaultSchema,
    tagNames: [
      ...(defaultSchema.tagNames || []),
      ...rehypeGroupingFigureSanitizeSchema.tagNames
    ],
    attributes: {
      ...defaultSchema.attributes,
      ...rehypeGroupingFigureSanitizeSchema.attributes
    },
    ancestors: {
      ...defaultSchema.ancestors,
      ...rehypeGroupingFigureSanitizeSchema.ancestors
    }
  })

License

MIT © Ari Palo