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-heading-slug

v1.0.3

Published

A rehype plugin that automatically adds unique id attributes to heading elements (h1–h6), supporting explicit slug notation, Unicode normalization, duplicate and invalid slug handling, flexible id assignment strategies, and compatibility with MDX. Powered

Readme

rehype-heading-slug

build status npm version codecov

A rehype plugin that automatically adds id attributes to heading elements (<h1><h6>). This plugin utilizes github-slugger and supports explicit slug notation, Unicode normalization, handling of duplicates and invalid slugs, and provides flexible strategies for managing existing id attributes and empty headings.

Overview

rehype-heading-slug is a unified (rehype) plugin designed to assign unique id attributes to HTML headings. Key features include:

  • Explicit slug notation: Specify a custom id directly by appending {#custom-slug} to headings.
  • Unicode normalization: Optionally normalize accented or non-ASCII characters to ASCII in slugs.
  • Duplicate and invalid slug handling: Define your preferred approach to managing duplicates or invalid slugs.
  • Flexible id assignment: Control behaviors for existing id attributes and empty heading elements.

When Should You Use This Plugin?

This plugin is especially useful if you require:

  • Anchor links for headings in HTML documents
  • Explicit control over heading slugs
  • Unicode normalization support for multilingual headings
  • Custom handling for duplicate or invalid slugs
  • Detailed control over existing id attributes and empty headings

Installation

npm install rehype-heading-slug

Usage Example

Given the following HTML snippet:

<h1>Introduction</h1>
<h2>日本語の見出し</h2>
<h2>Duplicate</h2>
<h2>Duplicate</h2>
<h3>Explicit {#custom-slug}</h3>

And JavaScript code:

import { rehype } from "rehype";
import rehypeHeadingSlug from "rehype-heading-slug";

const file = await rehype()
  .data("settings", { fragment: true })
  .use(rehypeHeadingSlug, { normalizeUnicode: true })
  .process(
    "<h1>Introduction</h1><h2>日本語の見出し</h2><h2>Duplicate</h2><h2>Duplicate</h2><h3>Explicit {#custom-slug}</h3>",
  );

console.log(String(file));

This will produce:

<h1 id="introduction">Introduction</h1>
<h2 id="日本語の見出し">日本語の見出し</h2>
<h2 id="duplicate">Duplicate</h2>
<h2 id="duplicate-1">Duplicate</h2>
<h3 id="custom-slug">Explicit</h3>

Note: MDX does not allow curly braces ({}) within headings. If you use MDX, configure a custom slug format via the slugRegex option.

MDX Compatibility Example

To avoid MDX parse errors, you can use a different slug notation, such as [#custom-slug]:

import { rehype } from "rehype";
import rehypeHeadingSlug from "rehype-heading-slug";

const mdxSlugRegex = /\s*\[#([A-Za-z0-9\-_]+)\]\s*$/;

const file = await rehype()
  .data("settings", { fragment: true })
  .use(rehypeHeadingSlug, { slugRegex: mdxSlugRegex })
  .process("<h2>My heading [#my-slug]</h2>");

console.log(String(file));
// <h2 id="my-slug">My heading</h2>

API

rehype().use(rehypeHeadingSlug[, options])

Assigns id attributes to heading elements (<h1><h6>), supporting explicit slugs and extensive configuration options.

Options

All options are optional:

| Name | Type | Default | Description | | ------------------------ | ------- | ---------------------------------------- | -------------------------------------------------------------------------------------------------------------------- | | slugRegex | RegExp | /\{#([a-zA-Z0-9_\-\u00C0-\uFFFF]+)\}$/ | Regular expression matching explicit slug notation | | maintainCase | boolean | false | Maintain original case in slugs (true) or convert to lowercase (false) | | duplicateSlugHandling | string | 'numbering' | Strategy for duplicate slugs: 'numbering' (suffix with numbers) or 'error' (throw an error) | | invalidSlugHandling | string | 'convert' | Strategy for invalid explicit slugs: 'convert' (auto-fix) or 'error' | | normalizeUnicode | boolean | false | Normalize Unicode characters (e.g., é to e) | | trimWhitespace | boolean | true | Trim whitespace from headings and slugs | | existingIdHandling | string | 'explicit' | Manage existing id attributes: 'always', 'never', 'explicit' (overwrite only if explicit slug), or 'error' | | assignIdToEmptyHeading | boolean | false | Assign an id to empty headings (true or false). If true, assigns ids like h2-1, h3-2, etc. | | strictSlugRegex | boolean | false | If true, require that slugRegex contains a capture group. Throws if not. |

For full details, refer to lib/index.d.ts.

Security

⚠️ Important: Adding id attributes can introduce cross-site scripting (XSS) risks through DOM clobbering. Always use rehype-sanitize and refer to its example for best practices.

Related Plugins

AI-Assisted Development Notice

This project was mainly developed using GitHub Copilot and other generative AI tools. The majority of the source code and documentation is AI-generated.

Human Oversight

  • Role: Primarily review and validate test cases for accuracy and relevance.
  • Responsibility: Users and contributors should thoroughly review and test AI-generated content before use in critical or production environments.

⚠️ Disclaimer: AI-generated content may have unintended inaccuracies or biases. Exercise caution and validate thoroughly for critical applications.

License

MIT License © adhi-jp