Gathering detailed insights and metrics for rehype-autolink-headings
Gathering detailed insights and metrics for rehype-autolink-headings
Gathering detailed insights and metrics for rehype-autolink-headings
Gathering detailed insights and metrics for rehype-autolink-headings
plugin to add links to headings in HTML
npm install rehype-autolink-headings
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
216 Stars
124 Commits
9 Forks
8 Watching
1 Branches
14 Contributors
Updated on 26 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-9.6%
48,315
Compared to previous day
Last week
5.5%
295,507
Compared to previous week
Last month
11.3%
1,180,087
Compared to previous month
Last year
36%
11,113,798
Compared to previous year
rehype plugin to add links from headings back to themselves.
This package is a unified (rehype) plugin to add links from headings
back to themselves.
It looks for headings (so <h1>
through <h6>
) that have id
properties,
and injects a link to themselves.
Similar functionality is applied by many places that render markdown.
For example, when browsing this readme on GitHub or npm, an anchor is added
to headings, which you can share to point people to a particular place in a
document.
unified is a project that transforms content with abstract syntax trees (ASTs). rehype adds support for HTML to unified. hast is the HTML AST that rehype uses. This is a rehype plugin that adds links to headings in the AST.
This plugin is useful when you have relatively long documents, where you want
users to be able to link to particular sections, and you already have id
properties set on all (or certain?) headings.
A different plugin, rehype-slug
, adds id
s to headings.
When a heading doesn’t already have an id
property, it creates a slug from
it, and adds that as the id
property.
When using both plugins together, all headings (whether explicitly with a
certain id
or automatically with a generate one) will get a link back to
themselves.
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install rehype-autolink-headings
In Deno with esm.sh
:
1import rehypeAutolinkHeadings from 'https://esm.sh/rehype-autolink-headings@7'
In browsers with esm.sh
:
1<script type="module"> 2 import rehypeAutolinkHeadings from 'https://esm.sh/rehype-autolink-headings@7?bundle' 3</script>
Say we have the following file example.html
:
1<h1>Solar System</h1> 2<h2>Formation and evolution</h2> 3<h2>Structure and composition</h2> 4<h3>Orbits</h3> 5<h3>Composition</h3> 6<h3>Distances and scales</h3> 7<h3>Interplanetary environment</h3> 8<p>…</p>
…and our module example.js
contains:
1import {rehype} from 'rehype' 2import rehypeAutolinkHeadings from 'rehype-autolink-headings' 3import rehypeSlug from 'rehype-slug' 4import {read} from 'to-vfile' 5 6const file = await rehype() 7 .data('settings', {fragment: true}) 8 .use(rehypeSlug) 9 .use(rehypeAutolinkHeadings) 10 .process(await read('example.html')) 11 12console.log(String(file))
…then running node example.js
yields:
1<h1 id="solar-system"><a aria-hidden="true" tabindex="-1" href="#solar-system"><span class="icon icon-link"></span></a>Solar System</h1> 2<h2 id="formation-and-evolution"><a aria-hidden="true" tabindex="-1" href="#formation-and-evolution"><span class="icon icon-link"></span></a>Formation and evolution</h2> 3<h2 id="structure-and-composition"><a aria-hidden="true" tabindex="-1" href="#structure-and-composition"><span class="icon icon-link"></span></a>Structure and composition</h2> 4<h3 id="orbits"><a aria-hidden="true" tabindex="-1" href="#orbits"><span class="icon icon-link"></span></a>Orbits</h3> 5<h3 id="composition"><a aria-hidden="true" tabindex="-1" href="#composition"><span class="icon icon-link"></span></a>Composition</h3> 6<h3 id="distances-and-scales"><a aria-hidden="true" tabindex="-1" href="#distances-and-scales"><span class="icon icon-link"></span></a>Distances and scales</h3> 7<h3 id="interplanetary-environment"><a aria-hidden="true" tabindex="-1" href="#interplanetary-environment"><span class="icon icon-link"></span></a>Interplanetary environment</h3> 8<p>…</p>
This package exports no identifiers.
The default export is rehypeAutolinkHeadings
.
unified().use(rehypeAutolinkHeadings[, options])
Add links from headings back to themselves.
options
(Options
, optional)
— configurationTransform (Transformer
).
This plugin only applies to headings with id
s.
Use rehype-slug
to generate id
s for headings that don’t have them.
Several behaviors are supported:
'prepend'
(default) — inject link before the heading text'append'
— inject link after the heading text'wrap'
— wrap the whole heading text with the link'before'
— insert link before the heading'after'
— insert link after the headingBehavior
Behavior (TypeScript type).
1type Behavior = 'after' | 'append' | 'before' | 'prepend' | 'wrap'
Build
Generate content (TypeScript type).
element
(Element
)
— current headingContent (Array<Node>
or Node
).
BuildProperties
Generate properties (TypeScript type).
element
(Element
)
— current headingProperties (Properties
).
Options
Configuration (TypeScript type).
behavior
(Behavior
, default: 'prepend'
)
— how to create linkscontent
(Array<Node>
, Node
, or Build
,
default: if 'wrap'
then undefined
, otherwise equivalent of
<span class="icon icon-link"></span>
)
— content to insert in the link;
if behavior
is 'wrap'
and Build
is passed, its result replaces the
existing content, otherwise the content is added after existing contentgroup
(Array<Node>
, Node
, or Build
,
optional)
— content to wrap the heading and link with, if behavior
is 'after'
or
'before'
headingProperties
(BuildProperties
or
Properties
, optional)
— extra properties to set on the headingproperties
(BuildProperties
or
Properties
, default:
{ariaHidden: true, tabIndex: -1}
if 'append'
or 'prepend'
, otherwise
undefined
)
— extra properties to set on the link when injectingtest
(Test
, optional)
— extra test for which headings are linkedThis example shows what each behavior generates by default.
1import {rehype} from 'rehype' 2import rehypeAutolinkHeadings from 'rehype-autolink-headings' 3 4const behaviors = ['after', 'append', 'before', 'prepend', 'wrap'] 5let index = -1 6while (++index < behaviors.length) { 7 const behavior = behaviors[index] 8 console.log( 9 String( 10 await rehype() 11 .data('settings', {fragment: true}) 12 .use(rehypeAutolinkHeadings, {behavior}) 13 .process('<h1 id="' + behavior + '">' + behavior + '</h1>') 14 ) 15 ) 16}
Yields:
1<h1 id="after">after</h1><a href="#after"><span class="icon icon-link"></span></a> 2<h1 id="append">append<a aria-hidden="true" tabindex="-1" href="#append"><span class="icon icon-link"></span></a></h1> 3<a href="#before"><span class="icon icon-link"></span></a><h1 id="before">before</h1> 4<h1 id="prepend"><a aria-hidden="true" tabindex="-1" href="#prepend"><span class="icon icon-link"></span></a>prepend</h1> 5<h1 id="wrap"><a href="#wrap">wrap</a></h1>
hastscript
The following example passes options.content
as a function, to generate an
accessible description specific to each link.
It uses hastscript
to build nodes.
1import {h} from 'hastscript' 2import {toString} from 'hast-util-to-string' 3import {rehype} from 'rehype' 4import rehypeAutolinkHeadings from 'rehype-autolink-headings' 5 6const file = await rehype() 7 .data('settings', {fragment: true}) 8 .use(rehypeAutolinkHeadings, { 9 content(node) { 10 return [ 11 h('span.visually-hidden', 'Read the “', toString(node), '” section'), 12 h('span.icon.icon-link', {ariaHidden: 'true'}) 13 ] 14 } 15 }) 16 .process('<h1 id="pluto">Pluto</h1>') 17 18console.log(String(file))
Yields:
1<h1 id="pluto"><a aria-hidden="true" tabindex="-1" href="#pluto"><span class="visually-hidden">Read the “Pluto” section</span><span class="icon icon-link" aria-hidden="true"></span></a>Pluto</h1>
The following example passes content
as nodes.
It uses hast-util-from-html-isomorphic
to
build nodes from a string of HTML.
1/** 2 * @import {ElementContent} from 'hast' 3 */ 4 5import {fromHtmlIsomorphic} from 'hast-util-from-html-isomorphic' 6import {rehype} from 'rehype' 7import rehypeAutolinkHeadings from 'rehype-autolink-headings' 8 9const file = await rehype() 10 .data('settings', {fragment: true}) 11 .use(rehypeAutolinkHeadings, { 12 content: /** @type {Array<ElementContent>} */ ( 13 fromHtmlIsomorphic( 14 '<svg height="10" width="10"><circle cx="5" cy="5" r="5" fill="black" /></svg>', 15 {fragment: true} 16 ).children 17 ) 18 }) 19 .process('<h1 id="makemake">Makemake</h1>') 20 21console.log(String(file))
Yields:
1<h1 id="makemake"><a aria-hidden="true" tabindex="-1" href="#makemake"><svg height="10" width="10"><circle cx="5" cy="5" r="5" fill="black"></circle></svg></a>Makemake</h1>
The following example passes group
as a function, to dynamically generate a
differing element that wraps the heading.
It uses hastscript
to build nodes.
1import {h} from 'hastscript' 2import {rehype} from 'rehype' 3import rehypeAutolinkHeadings from 'rehype-autolink-headings' 4 5const file = await rehype() 6 .data('settings', {fragment: true}) 7 .use(rehypeAutolinkHeadings, { 8 behavior: 'before', 9 group(node) { 10 return h('.heading-' + node.tagName.charAt(1) + '-group') 11 } 12 }) 13 .process('<h1 id="ceres">Ceres</h1>') 14 15console.log(String(file))
Yields:
1<div class="heading-1-group"><a href="#ceres"><span class="icon icon-link"></span></a><h1 id="ceres">Ceres</h1></div>
This package is fully typed with TypeScript.
It exports the additional types
Behavior
,
Build
,
BuildProperties
, and
Options
.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line,
rehype-autolink-headings@^7
, compatible with Node.js 16.
This plugin works with rehype-parse
version 1+, rehype-stringify
version 1+,
rehype
version 1+, and unified
version 4+.
Use of rehype-autolink-headings
can open you up to a
cross-site scripting (XSS) attack if you pass user provided content in
content
, group
, or properties
.
Always be wary of user input and use rehype-sanitize
.
rehype-slug
— add id
s to headingsrehype-highlight
— apply syntax highlighting to code blocksrehype-toc
— add a table of contents (TOC)See contributing.md
in rehypejs/.github
for ways
to get started.
See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
Reason
9 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 7
Reason
Found 2/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More