Skip to content

Commit

Permalink
Add option.prefix
Browse files Browse the repository at this point in the history
Related-to: GH-13.
Closes GH-14.
Closes GH-15.

Reviewed-by: Titus Wormer <tituswormer@gmail.com> 

Co-authored-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
onyxcodes and wooorm committed Oct 31, 2022
1 parent 31483c0 commit 899b3b7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
15 changes: 12 additions & 3 deletions index.js
Expand Up @@ -2,6 +2,13 @@
* @typedef {import('hast').Root} Root
*/

/**
* @typedef Options
* Configuration (optional).
* @property {string} [prefix='']
* Prefix to add in front of `id`s.
*/

import Slugger from 'github-slugger'
import {hasProperty} from 'hast-util-has-property'
import {headingRank} from 'hast-util-heading-rank'
Expand All @@ -13,15 +20,17 @@ const slugs = new Slugger()
/**
* Plugin to add `id`s to headings.
*
* @type {import('unified').Plugin<Array<void>, Root>}
* @type {import('unified').Plugin<[Options?]|Array<void>, Root>}
*/
export default function rehypeSlug() {
export default function rehypeSlug(options = {}) {
const prefix = options.prefix || ''

return (tree) => {
slugs.reset()

visit(tree, 'element', (node) => {
if (headingRank(node) && node.properties && !hasProperty(node, 'id')) {
node.properties.id = slugs.slug(toString(node))
node.properties.id = prefix + slugs.slug(toString(node))
}
})
}
Expand Down
13 changes: 10 additions & 3 deletions readme.md
Expand Up @@ -17,7 +17,7 @@
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`unified().use(rehypeSlug)`](#unifieduserehypeslug)
* [`unified().use(rehypeSlug[, options])`](#unifieduserehypeslug-options)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
Expand Down Expand Up @@ -117,10 +117,17 @@ Now, running `node example.js` yields:
This package exports no identifiers.
The default export is `rehypeSlug`.

### `unified().use(rehypeSlug)`
### `unified().use(rehypeSlug[, options])`

Add `id`s to headings.
There are no options.

##### `options`

Configuration (optional).

###### `options.prefix`

Prefix to add in front of `id`s (`string`, default: `''`).

## Types

Expand Down
36 changes: 35 additions & 1 deletion test.js
Expand Up @@ -3,7 +3,7 @@ import {rehype} from 'rehype'
import rehypeSlug from './index.js'

test('rehypeSlug', (t) => {
t.plan(2)
t.plan(4)

rehype()
.data('settings', {fragment: true})
Expand Down Expand Up @@ -38,4 +38,38 @@ test('rehypeSlug', (t) => {
)
}
)

rehype()
.data('settings', {fragment: true})
.use(rehypeSlug, {prefix: 'test-'})
.process(
[
'<section>',
' <h1>Lorem ipsum 😪</h1>',
' <h2>dolor—sit—amet</h2>',
' <h3>consectetur &amp; adipisicing</h3>',
' <h4>elit</h4>',
' <h5>elit</h5>',
' <p>sed</p>',
'</section>'
].join('\n'),
(error, file) => {
t.ifErr(error, 'shouldn’t throw')

t.equal(
String(file),
[
'<section>',
' <h1 id="test-lorem-ipsum-">Lorem ipsum 😪</h1>',
' <h2 id="test-dolorsitamet">dolor—sit—amet</h2>',
' <h3 id="test-consectetur--adipisicing">consectetur &#x26; adipisicing</h3>',
' <h4 id="test-elit">elit</h4>',
' <h5 id="test-elit-1">elit</h5>',
' <p>sed</p>',
'</section>'
].join('\n'),
'should match'
)
}
)
})

0 comments on commit 899b3b7

Please sign in to comment.