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-slug-anchor-sectionize

v0.2.0

Published

rehype plugin which wraps headings and their descendants in nested <section> elements and applies slugged anchor links to them.

Downloads

2,404

Readme

rehype-slug-anchor-sectionize

rehype plugin which wraps headings and their descendants in nested <section> elements and applies slugged anchor links to them.

Contents

What is this?

This package is a unified (rehype) plugin to wrap headings and their descendants in nested <section> elements and apply slugged anchor links to them.

unified is a project that transforms content with abstract syntax trees (ASTs). rehype adds support for HTML to unified. vfile is the virtual file interface used in unified. hast is the HTML AST that rehype uses.

This is a rehype plugin that inspects and transforms hast.

When should I use this?

The plugin tries to minimize the configuration effort and complexity by combining the following already existing similar plugin approaches:

  • sectionize or section
  • section-headings
  • heading-anchor
  • autolink-headings
  • slug

It is useful if you want to use IntersectionObserver or similar to observe changes in the intersection of a target element with an ancestor.

Demo

Inspect the DOM tree and heading elements on any of my Blog posts.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install rehype-slug-anchor-sectionize

In Deno, with esm.sh:

import rehypeSlugAnchorSectionize from 'https://esm.sh/rehype-slug-anchor-sectionize'

In browsers, with esm.sh:

<script type="module">
  import rehypeSlugAnchorSectionize from 'https://esm.sh/rehype-slug-anchor-sectionize?bundle'
</script>

Use

Say our module example.js looks as follows:

import { unified } from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeSlugAnchorSectionize from 'rehype-slug-anchor-sectionize'
import rehypeStringify from 'rehype-stringify'

const file = await unified()
  .use(remarkParse)
  .use(remarkRehype)
  .use(rehypeSlugAnchorSectionize)
  .use(rehypeStringify).process(`## Heading 2.1

Paragraph 1

## Heading 2.2

### Heading 3.1

Paragraph 3

## Heading 2.3

Paragraph 4`)

console.log(value)

…now running node example.js yields HTML Output::

<section id="heading-21-section">
  <h2 id="heading-21" role="presentation">
    <a href="#heading-21" class="heading-anchor">
      <button type="button" class="heading-anchor__btn">#</button>
    </a>
    <span role="heading" aria-level="2">Heading 2.1</span>
  </h2>
  <p>Paragraph 1</p>
</section>

<section id="heading-22-section">
  <h2 id="heading-22" role="presentation">
    <a href="#heading-22" class="heading-anchor">
      <button type="button" class="heading-anchor__btn">#</button>
    </a>
    <span role="heading" aria-level="2">Heading 2.2</span>
  </h2>
  <p>Paragraph 2</p>
  <section id="heading-31-section">
    <h3 id="heading-31" role="presentation">
      <a href="#heading-31" class="heading-anchor">
        <button type="button" class="heading-anchor__btn">#</button>
      </a>
      <span role="heading" aria-level="3">Heading 3.1</span>
    </h3>
    <p>Paragraph 3</p>
  </section>
</section>

<section id="heading-23-section">
  <h2 id="heading-23" role="presentation">
    <a href="#heading-23" class="heading-anchor">
      <button type="button" class="heading-anchor__btn">#</button>
    </a>
    <span role="heading" aria-level="2">Heading 2.3</span>
  </h2>
  <p>Paragraph 4</p>
</section>

Styling

The following styles should be added as minimal setup.

h2 a[class="heading-anchor"],
h3 a[class="heading-anchor"] {
  margin-left: -2rem;
}

.heading-anchor__btn {
  all: unset; /** replace with buttom reset */
  width: 2rem;
  opacity: 0;
  color: #eee;
}

h2:hover a[class="heading-anchor"] .heading-anchor__btn,
h3:hover a[class="heading-anchor"] .heading-anchor__btn {
  opacity: 1;
  cursor: pointer;
}

You can change the class names by providing a custom property for the element class attributes.

API

This package exports no identifiers. The default export is rehypeSlugAnchorSectionize.

unified().use(rehypeSlugAnchorSectionize, options?)

  • Wraps headings and their descendants in <section> elements.
  • In addition it applies slugged anchor-id's to the <section> and heading elements.
options

Configuration (optional).

options.depth

The heading level depth to sectionize. (number, default: 3).

options.wrapperTagName

The wrapper Element tagName. (string, default: section).

options.wrapperProperties

The wrapper Element properties. (Element['properties'], default: undefined).

options.wrapperSlugAdditive

Adds an additive at end of the wrappers id. (string, default: -section).

options.linkProperties

The link Element properties. (Element['properties'], default: { className: 'heading-anchor' }).

options.buttonProperties

The button Element properties. (Element['properties'], default: { className: 'heading-anchor__btn' }).

Types

This package is fully typed with TypeScript. The additional type RehypeSlugAnchorSectionizeOptions is exported.

Security

As improper use of HTML can open you up to a [cross-site scripting (XSS)][xss] attacks, use of rehype can also be unsafe. Use [rehype-sanitize][rehype-sanitize] to make the tree safe.

Use of rehype plugins could also open you up to other attacks. Carefully assess each plugin and the risks involved in using them.

License

MIT