A markdown-it plugin that wraps standalone photos in figures instead of paragraphs, using the title attribute as the figcaption.
Find a file
2025-09-04 12:16:16 -05:00
src refactor: add typescript 2025-09-04 10:48:51 -05:00
.gitignore chore: create package.json and add dependencies 2025-09-03 12:44:42 -05:00
.nvmrc chore: add .nvmrc file with a version of node supporting type stripping 2025-09-03 22:38:38 -05:00
LICENSE Initial commit 2025-09-03 12:44:09 -05:00
package.json refactor: add typescript 2025-09-04 10:48:51 -05:00
pnpm-lock.yaml refactor: add typescript 2025-09-04 10:48:51 -05:00
README.md docs: added usage and purpose to README.md 2025-09-04 12:16:16 -05:00
tsconfig.json refactor: add typescript 2025-09-04 10:48:51 -05:00

markdown-it-img-wrapper

NPM Version License

This changes the behavior of MarkdownIt to wrap standalone images in figures instead of paragraphs.

lorem ipsum...

![A picture.](./picture.jpg)

lorem ipsum...

<!-- Becomes -->

<p>lorem ipsum...</p>
<figure>
  <img alt="A picture." src="./picture.jpg">
</figure>
<p>lorem ipsum...</p>

In addition, if you provide a title attribute on the image, it will become a caption on the figure instead.

lorem ipsum...

![A picture.](./picture.jpg "Picture!")

lorem ipsum...

<!-- Becomes -->

<p>lorem ipsum...</p>
<figure>
  <img alt="A picture." src="./picture.jpg">
  <figcaption>Picture!</figcaption>
</figure>
<p>lorem ipsum...</p>

Installation

Install markdown-it-img-wrapper from npm with your favorite package manager.

Usage

import MarkdownIt from "markdown-it";
import imgWrapperPlugin from "markdown-it-img-wrapper";

const md = MarkdownIt();
md.use(imgWrapperPlugin);

const html = md.render('![](test.jpg)');

console.log(html); // <figure><img src="test.jpg" alt=""></figure>

Why Would I Need This?

By default, MarkdownIt wraps images inside of paragraphs even if they're the only thing on a line. That's compliant with the spec the project follows. To change that behavior, a plugin is necessary.

What if I Want Options?

This plugin is simple and opinionated. If you would like something similar but with more options and control over the transformation, markdown-it-implicit-figures is a good alternative.