node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

rehype-autolink-headings

rehype-autolink-headings Build Status Coverage Status

Automatically add links to headings (h1-h6) with rehype.

Installation

npm:

npm install rehype-autolink-headings

Usage

Say we have the following fragment:

<h1>Lorem ipsum 😪</h1>
<h2>dolor—sit—amet</h2>
<h3>consectetur &amp; adipisicing</h3>
<h4>elit</h4>
<h5>elit</h5>

And our script, example.js, looks as follows:

var fs = require('fs');
var rehype = require('rehype');
var slug = require('rehype-slug');
var link = require('rehype-autolink-headings');
 
var doc = fs.readFileSync('fragment.html');
 
rehype()
  .data('settings', {fragment: true})
  .use(slug)
  .use(link)
  .process(doc, function (err, file) {
    if (err) throw err;
    console.log(String(file));
  });

Now, running node example yields:

<h1 id="lorem-ipsum-"><a aria-hidden="true" href="#lorem-ipsum-"><span class="icon icon-link"></span></a>Lorem ipsum 😪</h1>
<h2 id="dolorsitamet"><a aria-hidden="true" href="#dolorsitamet"><span class="icon icon-link"></span></a>dolor—sit—amet</h2>
<h3 id="consectetur--adipisicing"><a aria-hidden="true" href="#consectetur--adipisicing"><span class="icon icon-link"></span></a>consectetur &#x26; adipisicing</h3>
<h4 id="elit"><a aria-hidden="true" href="#elit"><span class="icon icon-link"></span></a>elit</h4>
<h5 id="elit-1"><a aria-hidden="true" href="#elit-1"><span class="icon icon-link"></span></a>elit</h5>

API

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

Adds links to headings (h1-h6) with an id

options
options.behavior

string, default: prepend — How to add a link:

  • 'prepend' and 'append' inserts a link with content in it respectively before or after the heading contents
  • 'wrap' wraps a link around the current heading contents.
options.properties

Object, default: {} if 'wrap', {ariaHidden: true} otherwise — Properties for the added link.

options.content

Node or Array.<Node>, default: a span element with icon and icon-link classes — Content to add in link. Ignored if 'wrap'

Related

License

MIT © Titus Wormer