8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** rehype** ] [ rehype ] plugin to add ` id ` s to headings.
11
+ ** [ rehype] [ ] ** plugin to add ` id ` s to headings.
12
+
13
+ ## Contents
14
+
15
+ * [ What is this?] ( #what-is-this )
16
+ * [ When should I use this?] ( #when-should-i-use-this )
17
+ * [ Install] ( #install )
18
+ * [ Use] ( #use )
19
+ * [ API] ( #api )
20
+ * [ ` unified().use(rehypeSlug) ` ] ( #unifieduserehypeslug )
21
+ * [ Types] ( #types )
22
+ * [ Compatibility] ( #compatibility )
23
+ * [ Security] ( #security )
24
+ * [ Related] ( #related )
25
+ * [ Contribute] ( #contribute )
26
+ * [ License] ( #license )
27
+
28
+ ## What is this?
29
+
30
+ This package is a [ unified] [ ] ([ rehype] [ ] ) plugin to add ` id ` s to headings.
31
+ It looks for headings (so ` <h1> ` through ` <h6> ` ) that do not yet have ` id ` s
32
+ and adds ` id ` attributes to them based on their textual content.
33
+ The algorithm that generates ` id ` s is [ ` github-slugger ` ] [ github-slugger ] , which
34
+ matches how GitHub works.
35
+
36
+ ** unified** is a project that transforms content with abstract syntax trees
37
+ (ASTs).
38
+ ** rehype** adds support for HTML to unified.
39
+ ** hast** is the HTML AST that rehype uses.
40
+ This is a rehype plugin that adds links to headings in the AST.
41
+
42
+ ## When should I use this?
43
+
44
+ This plugin is useful when you have relatively long documents and you want to be
45
+ able to link to particular sections.
46
+
47
+ A different plugin, [ ` rehype-autolink-headings ` ] [ rehype-autolink-headings ] , adds
48
+ links to these headings back to themselves, which is useful as it lets users
49
+ more easily link to particular sections.
12
50
13
51
## Install
14
52
15
- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
16
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
17
-
18
- [ npm] [ ] :
53
+ This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) .
54
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
19
55
20
56
``` sh
21
57
npm install rehype-slug
22
58
```
23
59
60
+ In Deno with [ Skypack] [ ] :
61
+
62
+ ``` js
63
+ import rehypeSlug from ' https://cdn.skypack.dev/rehype-slug@5?dts'
64
+ ```
65
+
66
+ In browsers with [ Skypack] [ ] :
67
+
68
+ ``` html
69
+ <script type =" module" >
70
+ import rehypeSlug from ' https://cdn.skypack.dev/rehype-slug@5?min'
71
+ </script >
72
+ ```
73
+
24
74
## Use
25
75
26
- Say we have the following file, ` fragment .html` :
76
+ Say we have the following file ` example .html` :
27
77
28
78
``` html
29
- <h1 >Lorem ipsum 😪 </h1 >
30
- <h2 >dolor— sit— amet</h2 >
79
+ <h1 id = some-id >Lorem ipsum</h1 >
80
+ <h2 >Dolor sit amet 😪 </h2 >
31
81
<h3 >consectetur & ; adipisicing</h3 >
32
82
<h4 >elit</h4 >
33
83
<h5 >elit</h5 >
34
84
```
35
85
36
- And our script, ` example.js ` , looks as follows:
86
+ And our module ` example.js ` looks as follows:
37
87
38
88
``` js
39
- import fs from ' node:fs '
89
+ import { read } from ' to-vfile '
40
90
import {rehype } from ' rehype'
41
- import slug from ' rehype-slug'
91
+ import rehypeSlug from ' rehype-slug'
42
92
43
- const buf = fs . readFileSync ( ' fragment.html ' )
93
+ main ( )
44
94
45
- rehype ()
46
- .data (' settings' , {fragment: true })
47
- .use (slug)
48
- .process (buf)
49
- .then ((file ) => {
50
- console .log (String (file))
51
- })
95
+ async function main () {
96
+ const file = await rehype ()
97
+ .data (' settings' , {fragment: true })
98
+ .use (rehypeSlug)
99
+ .process (await read (' example.html' ))
100
+
101
+ console .log (String (file))
102
+ }
52
103
```
53
104
54
- Now, running ` node example ` yields:
105
+ Now, running ` node example.js ` yields:
55
106
56
107
``` html
57
- <h1 id =" lorem-ipsum- " >Lorem ipsum 😪 </h1 >
58
- <h2 id =" dolorsitamet " >dolor— sit— amet</h2 >
108
+ <h1 id =" some-id " >Lorem ipsum</h1 >
109
+ <h2 id =" dolor-sit-amet- " >Dolor sit amet 😪 </h2 >
59
110
<h3 id =" consectetur--adipisicing" >consectetur & ; adipisicing</h3 >
60
111
<h4 id =" elit" >elit</h4 >
61
112
<h5 id =" elit-1" >elit</h5 >
@@ -68,24 +119,37 @@ The default export is `rehypeSlug`.
68
119
69
120
### ` unified().use(rehypeSlug) `
70
121
71
- Add ` id ` properties to h1-h6 headings that don’t already have one.
122
+ Add ` id ` s to headings.
123
+ There are no options.
124
+
125
+ ## Types
72
126
73
- Uses [ ** github-slugger** ] [ ghslug ] to create GitHub style ` id ` s.
127
+ This package is fully typed with [ TypeScript] [ ] .
128
+ There are no extra exported types.
129
+
130
+ ## Compatibility
131
+
132
+ Projects maintained by the unified collective are compatible with all maintained
133
+ versions of Node.js.
134
+ As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
135
+ Our projects sometimes work with older versions, but this is not guaranteed.
136
+
137
+ This plugin works with ` rehype-parse ` version 1+, ` rehype-stringify ` version 1+,
138
+ ` rehype ` version 1+, and ` unified ` version 4+.
74
139
75
140
## Security
76
141
77
142
Use of ` rehype-slug ` can open you up to a [ cross-site scripting (XSS)] [ xss ]
78
- attack as it sets ` id ` attributes on headings.
79
- In a browser, elements are retrievable by ` id ` with JavaScript and CSS.
80
- If a user injects a heading that slugs to an ` id ` you are already using,
81
- the user content may impersonate the website.
82
-
83
- Always be wary with user input and use [ ` rehype-sanitize ` ] [ sanitize ] .
143
+ attack as it sets ` id ` attributes on headings, which causes what is known
144
+ as “DOM clobbering”.
145
+ Please use [ ` rehype-sanitize ` ] [ rehype-sanitize ] and see its
146
+ [ Example: headings (DOM clobbering)] [ rehype-sanitize-example ] for information on
147
+ how to properly solve it.
84
148
85
149
## Related
86
150
87
- * [ ` remark-slug ` ] ( https://github.com/wooorm/remark-slug )
88
- — Add slugs to headings in markdown
151
+ * [ ` rehype-autolink-headings ` ] [ rehype-autolink-headings ]
152
+ — add links to headings with IDs back to themselves
89
153
90
154
## Contribute
91
155
@@ -131,6 +195,8 @@ abide by its terms.
131
195
132
196
[ npm ] : https://docs.npmjs.com/cli/install
133
197
198
+ [ skypack ] : https://www.skypack.dev
199
+
134
200
[ health ] : https://github.com/rehypejs/.github
135
201
136
202
[ contributing ] : https://github.com/rehypejs/.github/blob/HEAD/contributing.md
@@ -143,10 +209,18 @@ abide by its terms.
143
209
144
210
[ author ] : https://wooorm.com
145
211
146
- [ rehype ] : https://github.com/rehypejs/rehype
212
+ [ typescript ] : https://www.typescriptlang.org
147
213
148
- [ ghslug ] : https://github.com/Flet/github-slugger
214
+ [ unified ] : https://github.com/unifiedjs/unified
215
+
216
+ [ rehype ] : https://github.com/rehypejs/rehype
149
217
150
218
[ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
151
219
152
- [ sanitize ] : https://github.com/rehypejs/rehype-sanitize
220
+ [ github-slugger ] : https://github.com/Flet/github-slugger
221
+
222
+ [ rehype-autolink-headings ] : https://github.com/rehypejs/rehype-autolink-headings
223
+
224
+ [ rehype-sanitize ] : https://github.com/rehypejs/rehype-sanitize
225
+
226
+ [ rehype-sanitize-example ] : https://github.com/rehypejs/rehype-sanitize#example-headings-dom-clobbering
0 commit comments