File tree 3 files changed +57
-7
lines changed
3 files changed +57
-7
lines changed Original file line number Diff line number Diff line change 2
2
* @typedef {import('hast').Root } Root
3
3
*/
4
4
5
+ /**
6
+ * @typedef Options
7
+ * Configuration (optional).
8
+ * @property {string } [prefix='']
9
+ * Prefix to add in front of `id`s.
10
+ */
11
+
5
12
import Slugger from 'github-slugger'
6
13
import { hasProperty } from 'hast-util-has-property'
7
14
import { headingRank } from 'hast-util-heading-rank'
@@ -13,15 +20,17 @@ const slugs = new Slugger()
13
20
/**
14
21
* Plugin to add `id`s to headings.
15
22
*
16
- * @type {import('unified').Plugin<Array<void>, Root> }
23
+ * @type {import('unified').Plugin<[Options?]| Array<void>, Root> }
17
24
*/
18
- export default function rehypeSlug ( ) {
25
+ export default function rehypeSlug ( options = { } ) {
26
+ const prefix = options . prefix || ''
27
+
19
28
return ( tree ) => {
20
29
slugs . reset ( )
21
30
22
31
visit ( tree , 'element' , ( node ) => {
23
32
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 ) )
25
34
}
26
35
} )
27
36
}
Original file line number Diff line number Diff line change 17
17
* [ Install] ( #install )
18
18
* [ Use] ( #use )
19
19
* [ API] ( #api )
20
- * [ ` unified().use(rehypeSlug) ` ] ( #unifieduserehypeslug )
20
+ * [ ` unified().use(rehypeSlug[, options] ) ` ] ( #unifieduserehypeslug-options )
21
21
* [ Types] ( #types )
22
22
* [ Compatibility] ( #compatibility )
23
23
* [ Security] ( #security )
@@ -117,10 +117,17 @@ Now, running `node example.js` yields:
117
117
This package exports no identifiers.
118
118
The default export is ` rehypeSlug ` .
119
119
120
- ### ` unified().use(rehypeSlug) `
120
+ ### ` unified().use(rehypeSlug[, options] ) `
121
121
122
122
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: ` '' ` ).
124
131
125
132
## Types
126
133
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import {rehype} from 'rehype'
3
3
import rehypeSlug from './index.js'
4
4
5
5
test ( 'rehypeSlug' , ( t ) => {
6
- t . plan ( 2 )
6
+ t . plan ( 4 )
7
7
8
8
rehype ( )
9
9
. data ( 'settings' , { fragment : true } )
@@ -38,4 +38,38 @@ test('rehypeSlug', (t) => {
38
38
)
39
39
}
40
40
)
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 & 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 & 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
+ )
41
75
} )
You can’t perform that action at this time.
0 commit comments