1.0.0 • Published 2 years ago

rehype-slugify v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

rehype-slugify

rehype plugin to add ids to headings with a custom slugify function.

Installation

npm install rehype-slugify

Usage

Given the html:

<h1>HELLO</h1>
<p>world!</p>

And our js script:

import fs from 'node:fs'
import {rehype} from 'rehype'
import slugify from 'rehype-slug'

const buf = fs.readFileSync('fragment.html')

rehype()
  .data('settings', {fragment: true})
  .use(slugify, {slugify: (content) => {
    return content.toLowerCase();
  }})
  .process(buf)
  .then((file) => {
    console.log(String(file))
  })

It will print out:

<h1 id="hello">HELLO</h1>
<p>world!</p>

API

Default export is rehypeSlugify. It requires the a slugify function in options, and throws an error if not provided.