Gathering detailed insights and metrics for @justfork/rehype-autolink-headings
Gathering detailed insights and metrics for @justfork/rehype-autolink-headings
Gathering detailed insights and metrics for @justfork/rehype-autolink-headings
Gathering detailed insights and metrics for @justfork/rehype-autolink-headings
plugin to add links to headings in HTML
npm install @justfork/rehype-autolink-headings
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
240 Stars
124 Commits
9 Forks
7 Watchers
1 Branches
14 Contributors
Updated on Jul 06, 2025
Latest Version
5.1.1
Package Id
@justfork/rehype-autolink-headings@5.1.1
Unpacked Size
13.38 kB
Size
4.98 kB
File Count
5
NPM Version
8.7.0
Node Version
14.17.2
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
rehype plugin to automatically add links to headings (h1-h6).
npm:
1npm install rehype-autolink-headings
Say we have the following file, fragment.html
:
1<h1>Lorem ipsum 😪</h1> 2<h2>dolor—sit—amet</h2> 3<h3>consectetur & adipisicing</h3> 4<h4>elit</h4> 5<h5>elit</h5>
And our script, example.js
, looks as follows:
1var fs = require('fs') 2var rehype = require('rehype') 3var slug = require('rehype-slug') 4var link = require('rehype-autolink-headings') 5 6var doc = fs.readFileSync('fragment.html') 7 8rehype() 9 .data('settings', {fragment: true}) 10 .use(slug) 11 .use(link) 12 .process(doc, function(err, file) { 13 if (err) throw err 14 console.log(String(file)) 15 })
Now, running node example
yields:
1<h1 id="lorem-ipsum-"><a aria-hidden="true" href="#lorem-ipsum-"><span class="icon icon-link"></span></a>Lorem ipsum 😪</h1> 2<h2 id="dolorsitamet"><a aria-hidden="true" href="#dolorsitamet"><span class="icon icon-link"></span></a>dolor—sit—amet</h2> 3<h3 id="consectetur--adipisicing"><a aria-hidden="true" href="#consectetur--adipisicing"><span class="icon icon-link"></span></a>consectetur & adipisicing</h3> 4<h4 id="elit"><a aria-hidden="true" href="#elit"><span class="icon icon-link"></span></a>elit</h4> 5<h5 id="elit-1"><a aria-hidden="true" href="#elit-1"><span class="icon icon-link"></span></a>elit</h5>
rehype().use(link[, options])
Add links to headings (h1-h6) with an id
.
Note: this plugin expects
id
s to already exist on headings. One way to add those automatically, isrehype-slug
(see example).
options
options.behavior
How to create links (string
, default: 'prepend'
).
'prepend'
— 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 headingSupplying wrap
will ignore any value defined by the content
option.
Supplying prepend
, append
, or wrap
will ignore the group
option.
options.properties
Extra properties to set on the link (Object?
).
Defaults to {ariaHidden: true, tabIndex: -1}
when in 'prepend'
or
'append'
mode.
options.content
hast nodes to insert in the link (Function|Node|Children
).
By default, the following is used:
1{ 2 type: 'element', 3 tagName: 'span', 4 properties: {className: ['icon', 'icon-link']}, 5 children: [] 6}
If behavior
is wrap
, then content
is ignored.
If content
is a function, it’s called with the current heading (Element
) and
should return one or more nodes:
1var toString = require('hast-util-to-string') 2var h = require('hastscript') 3 4// … 5 6function content(node) { 7 return [ 8 h('span.visually-hidden', 'Read the “', toString(node), '” section'), 9 h('span.icon.icon-link', {ariaHidden: true}) 10 ] 11}
options.group
hast node to wrap the heading and link with (Function|Node
), if
behavior
is before
or after
.
There is no default.
If behavior
is prepend
, append
, or wrap
, then group
is ignored.
If group
is a function, it’s called with the current heading (Element
) and
should return a hast node.
1var h = require('hastscript') 2 3// … 4 5function group(node) { 6 return h('.heading-' + node.charAt(1) + '-group') 7}
Use of rehype-autolink-headings
can open you up to a
cross-site scripting (XSS) attack if you pass user provided content in
properties
or content
.
Always be wary of user input and use rehype-sanitize
.
rehype-slug
— Add id
s to headingsrehype-highlight
— Syntax highlight 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 binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
Found 2/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
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 2025-06-30
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