Skip to content

Commit 899b3b7

Browse files
onyxcodeswooorm
andauthoredOct 31, 2022
Add option.prefix
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>
1 parent 31483c0 commit 899b3b7

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed
 

‎index.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
* @typedef {import('hast').Root} Root
33
*/
44

5+
/**
6+
* @typedef Options
7+
* Configuration (optional).
8+
* @property {string} [prefix='']
9+
* Prefix to add in front of `id`s.
10+
*/
11+
512
import Slugger from 'github-slugger'
613
import {hasProperty} from 'hast-util-has-property'
714
import {headingRank} from 'hast-util-heading-rank'
@@ -13,15 +20,17 @@ const slugs = new Slugger()
1320
/**
1421
* Plugin to add `id`s to headings.
1522
*
16-
* @type {import('unified').Plugin<Array<void>, Root>}
23+
* @type {import('unified').Plugin<[Options?]|Array<void>, Root>}
1724
*/
18-
export default function rehypeSlug() {
25+
export default function rehypeSlug(options = {}) {
26+
const prefix = options.prefix || ''
27+
1928
return (tree) => {
2029
slugs.reset()
2130

2231
visit(tree, 'element', (node) => {
2332
if (headingRank(node) && node.properties && !hasProperty(node, 'id')) {
24-
node.properties.id = slugs.slug(toString(node))
33+
node.properties.id = prefix + slugs.slug(toString(node))
2534
}
2635
})
2736
}

‎readme.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* [Install](#install)
1818
* [Use](#use)
1919
* [API](#api)
20-
* [`unified().use(rehypeSlug)`](#unifieduserehypeslug)
20+
* [`unified().use(rehypeSlug[, options])`](#unifieduserehypeslug-options)
2121
* [Types](#types)
2222
* [Compatibility](#compatibility)
2323
* [Security](#security)
@@ -117,10 +117,17 @@ Now, running `node example.js` yields:
117117
This package exports no identifiers.
118118
The default export is `rehypeSlug`.
119119

120-
### `unified().use(rehypeSlug)`
120+
### `unified().use(rehypeSlug[, options])`
121121

122122
Add `id`s to headings.
123-
There are no options.
123+
124+
##### `options`
125+
126+
Configuration (optional).
127+
128+
###### `options.prefix`
129+
130+
Prefix to add in front of `id`s (`string`, default: `''`).
124131

125132
## Types
126133

‎test.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {rehype} from 'rehype'
33
import rehypeSlug from './index.js'
44

55
test('rehypeSlug', (t) => {
6-
t.plan(2)
6+
t.plan(4)
77

88
rehype()
99
.data('settings', {fragment: true})
@@ -38,4 +38,38 @@ test('rehypeSlug', (t) => {
3838
)
3939
}
4040
)
41+
42+
rehype()
43+
.data('settings', {fragment: true})
44+
.use(rehypeSlug, {prefix: 'test-'})
45+
.process(
46+
[
47+
'<section>',
48+
' <h1>Lorem ipsum 😪</h1>',
49+
' <h2>dolor—sit—amet</h2>',
50+
' <h3>consectetur &amp; adipisicing</h3>',
51+
' <h4>elit</h4>',
52+
' <h5>elit</h5>',
53+
' <p>sed</p>',
54+
'</section>'
55+
].join('\n'),
56+
(error, file) => {
57+
t.ifErr(error, 'shouldn’t throw')
58+
59+
t.equal(
60+
String(file),
61+
[
62+
'<section>',
63+
' <h1 id="test-lorem-ipsum-">Lorem ipsum 😪</h1>',
64+
' <h2 id="test-dolorsitamet">dolor—sit—amet</h2>',
65+
' <h3 id="test-consectetur--adipisicing">consectetur &#x26; adipisicing</h3>',
66+
' <h4 id="test-elit">elit</h4>',
67+
' <h5 id="test-elit-1">elit</h5>',
68+
' <p>sed</p>',
69+
'</section>'
70+
].join('\n'),
71+
'should match'
72+
)
73+
}
74+
)
4175
})

0 commit comments

Comments
 (0)
Please sign in to comment.