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 🙏

© 2024 – Pkg Stats / Ryan Hefner

rehype-richer-figure

v1.1.0

Published

Rehype plugin to create a <figure> semantic structure

Downloads

377

Readme

rehype-richer-figure

What is this?

This package is a unified (rehype) plugin to assist in adding rich captions to figures.

unified is a project that transforms content with abstract syntax trees (ASTs). rehype adds support for HTML to unified.

The rehype-richer-figure plugin searches a unified AST for a specific non-semantic structure, and transforms it into a semantic structure.

It searches for and transforms this HTML:

<p><img alt="This is the alt text" src="Image.jpg" /></p>

<p>: This is the <em>Caption</em></p>

into this HTML:

<figure>
  <img alt="This is the alt text" src="Image.jpg" />
  <figcaption>This is the <em>Caption</em></figcaption>
</figure>

When should I use this?

rehype-richer-figure was written to be embedded in a Quartz application. Quartz is a static site generator that is most commonly used to generate HTML views of Obsidian vaults. It may be useful whenever you're using unified to convert Markdown to HTML and want a way to produce <figure> structures with rich captions.

When used as part of a complete markdown-to-HTML pipeline, it transforms this markdown:

![Pencil portrait of Harry Cust (1861-1917) by Violet Manners (1892)](NPG_D23400.jpg)

: **Harry Cust**, after _Violet Manners, Duchess of Rutland_, litho., 1892 (NPG D23400)

into this HTML:

<figure>
  <img
    alt="Pencil portrait of Harry Cust (1861-1917) by Violet Manners (1892)"
    src="NPG_D23400.jpg"
  />
  <figcaption>
    <strong>Harry Cust</strong>, after
    <em>Violet Manners, Duchess of Rutland</em>, litho., 1892 (NPG D23400)
  </figcaption>
</figure>

Behavior

This plugin preserves the <img> tag's existing HTML attributes (unless overwritten by configuration, see below), but the attributes of the <p> tags are lost. Non-Element nodes (eg Comment, Text) between the two <p> nodes are also lost.

Due to this plugin overloading the : syntax for definition lists used by some markdown extensions, I recommend running it early in your toolchain.

The blank line between the two lines of markdown is required.

Accepts an optional config object with the following optional properties:

{
  loading?: "eager" | "lazy"; // Controls the loading attribute of the `<img>` tag.
  figureClass?: string[]; // Classes to add to the <figure> element.
  wrap?: boolean; // If true, wrap a <div> around the <figure>
  wrapClass?: string[]; // Classes to add to the wrapping <div> element.
}

Example

import { unified } from "unified";
import rehypeParse from "rehype-parse";
import rehypeStringify from "rehype-stringify";
import rehypeRicherFigure from "rehype-richer-figure";

const html =
  '<p><img src="NPG_D23400.jpg" alt="Pencil portrait of Harry Cust (1861-1917) by Violet Manners (1892)">\n' +
  "<p>: <strong>Harry Cust</strong>, after <em>Violet Manners, Duchess of Rutland</em>, litho., 1892 (NPG D23400)</p>\n";

unified()
  .use(rehypeParse)
  .use(rehypeRicherFigure, { loading: "lazy" })
  .use(rehypeStringify)
  .process(html)
  .then((file) => {
    console.log(String(file));
  });

Compatibility

It works for me, and it's licensed MIT.

Related