rehype plugin to transform an image with alt text to a figure with caption
npm install @microflash/rehype-figure


rehype plugin to transform an image with alt text to a figure with caption
> [!IMPORTANT]
> Converting an image with alt text to a figure with caption is an escape hatch. Alt text, title, and captions have different intended purposes, and you should eventually enhance your content to adopt them.
- What's this?
- Install
- Use
- API
- License
This package is a unified (rehype) plugin that takes an image node with alt text (e.g., !Alt text) and converts it to a figure element with caption.
``html`

This package is ESM only.
In Node.js (16.0+), install with npm:
`sh`
npm install @microflash/rehype-figure
> For Node.js versions below 16.0, stick to 1.x.x versions of this plugin.
In Deno, with esm.sh:
`js`
import rehypeFigure from "https://esm.sh/@microflash/rehype-figure";
In browsers, with esm.sh:
`html`
Say we have the following module example.js:
`js
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkGfm from "remark-gfm";
import remarkRehype from "remark-rehype";
import rehypeFigure from "@microflash/rehype-figure";
import rehypeStringify from "rehype-stringify";
main()
async function main() {
const file = await unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkRehype)
.use(rehypeFigure)
.use(rehypeStringify)
.process("!Alt text");
console.log(String(file));
}
`
Running that with node example.js yields:
`html`

The default export is rehypeFigure.
The following options are available. All of them are optional.
- className: class for the wrapped figure element
By default, no classes are added to the figure element.
- Run pnpm snapshot to generate snapshotspnpm test` to run tests
- Run