Gathering detailed insights and metrics for rehype-slug-anchor-sectionize
Gathering detailed insights and metrics for rehype-slug-anchor-sectionize
Gathering detailed insights and metrics for rehype-slug-anchor-sectionize
Gathering detailed insights and metrics for rehype-slug-anchor-sectionize
rehype-slug
rehype plugin to add `id` attributes to headings
@hbsnow/rehype-sectionize
rehype plugin that wraps section based on heading
rehype-external-links
rehype plugin to automatically add `target` and `rel` attributes to external links
remark-rehype
remark plugin that turns markdown into HTML to support rehype
rehype plugin which wraps headings and their descendants in nested <section> elements and applies slugged anchor links to them
npm install rehype-slug-anchor-sectionize
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1 Stars
138 Commits
1 Watching
3 Branches
1 Contributors
Updated on 25 Nov 2024
Minified
Minified + Gzipped
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-37.4%
92
Compared to previous day
Last week
35.6%
671
Compared to previous week
Last month
34.1%
2,572
Compared to previous month
Last year
1,927.1%
16,399
Compared to previous year
rehype plugin which wraps headings and their descendants in nested <section>
elements and applies slugged anchor links
to them.
unified().use(rehypeSlugAnchorSectionize, options?)
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.
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.
Inspect the DOM tree and heading elements on any of my Blog posts.
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install rehype-slug-anchor-sectionize
In Deno, with esm.sh:
1import rehypeSlugAnchorSectionize from 'https://esm.sh/rehype-slug-anchor-sectionize'
In browsers, with esm.sh:
1<script type="module"> 2 import rehypeSlugAnchorSectionize from 'https://esm.sh/rehype-slug-anchor-sectionize?bundle' 3</script>
Say our module example.js
looks as follows:
1import { unified } from 'unified' 2import remarkParse from 'remark-parse' 3import remarkRehype from 'remark-rehype' 4import rehypeSlugAnchorSectionize from 'rehype-slug-anchor-sectionize' 5import rehypeStringify from 'rehype-stringify' 6 7const file = await unified() 8 .use(remarkParse) 9 .use(remarkRehype) 10 .use(rehypeSlugAnchorSectionize) 11 .use(rehypeStringify).process(`## Heading 2.1 12 13Paragraph 1 14 15## Heading 2.2 16 17### Heading 3.1 18 19Paragraph 3 20 21## Heading 2.3 22 23Paragraph 4`) 24 25console.log(value)
…now running node example.js
yields HTML Output::
1<section id="heading-21-section"> 2 <h2 id="heading-21" role="presentation"> 3 <a href="#heading-21" class="heading-anchor"> 4 <button type="button" class="heading-anchor__btn">#</button> 5 </a> 6 <span role="heading" aria-level="2">Heading 2.1</span> 7 </h2> 8 <p>Paragraph 1</p> 9</section> 10 11<section id="heading-22-section"> 12 <h2 id="heading-22" role="presentation"> 13 <a href="#heading-22" class="heading-anchor"> 14 <button type="button" class="heading-anchor__btn">#</button> 15 </a> 16 <span role="heading" aria-level="2">Heading 2.2</span> 17 </h2> 18 <p>Paragraph 2</p> 19 <section id="heading-31-section"> 20 <h3 id="heading-31" role="presentation"> 21 <a href="#heading-31" class="heading-anchor"> 22 <button type="button" class="heading-anchor__btn">#</button> 23 </a> 24 <span role="heading" aria-level="3">Heading 3.1</span> 25 </h3> 26 <p>Paragraph 3</p> 27 </section> 28</section> 29 30<section id="heading-23-section"> 31 <h2 id="heading-23" role="presentation"> 32 <a href="#heading-23" class="heading-anchor"> 33 <button type="button" class="heading-anchor__btn">#</button> 34 </a> 35 <span role="heading" aria-level="2">Heading 2.3</span> 36 </h2> 37 <p>Paragraph 4</p> 38</section>
The following styles should be added as minimal setup.
1h2 a[class="heading-anchor"], 2h3 a[class="heading-anchor"] { 3 margin-left: -2rem; 4} 5 6.heading-anchor__btn { 7 all: unset; /** replace with buttom reset */ 8 width: 2rem; 9 opacity: 0; 10 color: #eee; 11} 12 13h2:hover a[class="heading-anchor"] .heading-anchor__btn, 14h3:hover a[class="heading-anchor"] .heading-anchor__btn { 15 opacity: 1; 16 cursor: pointer; 17}
You can change the class names by providing a custom property
for the element class attributes.
This package exports no identifiers.
The default export is rehypeSlugAnchorSectionize
.
unified().use(rehypeSlugAnchorSectionize, options?)
<section>
elements.<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' }
).
This package is fully typed with TypeScript.
The additional type RehypeSlugAnchorSectionizeOptions
is exported.
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.
No vulnerabilities found.
No security vulnerabilities found.