Skip to content

Commit 0b7179e

Browse files
authored
Add improved docs
Closes GH-11. Reviewed-by: Christian Murphy <[email protected]> Reviewed-by: Remco Haszing <[email protected]>
1 parent 103e102 commit 0b7179e

File tree

2 files changed

+110
-36
lines changed

2 files changed

+110
-36
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const slugs = new Slugger()
1313
/**
1414
* Plugin to add `id`s to headings.
1515
*
16-
* @type {import('unified').Plugin<[], Root>}
16+
* @type {import('unified').Plugin<Array<void>, Root>}
1717
*/
1818
export default function rehypeSlug() {
1919
return (tree) => {

readme.md

Lines changed: 109 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,105 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

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.
1250

1351
## Install
1452

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][]:
1955

2056
```sh
2157
npm install rehype-slug
2258
```
2359

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+
2474
## Use
2575

26-
Say we have the following file, `fragment.html`:
76+
Say we have the following file `example.html`:
2777

2878
```html
29-
<h1>Lorem ipsum 😪</h1>
30-
<h2>dolor—sitamet</h2>
79+
<h1 id=some-id>Lorem ipsum</h1>
80+
<h2>Dolor sit amet 😪</h2>
3181
<h3>consectetur &amp; adipisicing</h3>
3282
<h4>elit</h4>
3383
<h5>elit</h5>
3484
```
3585

36-
And our script, `example.js`, looks as follows:
86+
And our module `example.js` looks as follows:
3787

3888
```js
39-
import fs from 'node:fs'
89+
import {read} from 'to-vfile'
4090
import {rehype} from 'rehype'
41-
import slug from 'rehype-slug'
91+
import rehypeSlug from 'rehype-slug'
4292

43-
const buf = fs.readFileSync('fragment.html')
93+
main()
4494

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+
}
52103
```
53104

54-
Now, running `node example` yields:
105+
Now, running `node example.js` yields:
55106

56107
```html
57-
<h1 id="lorem-ipsum-">Lorem ipsum 😪</h1>
58-
<h2 id="dolorsitamet">dolor—sitamet</h2>
108+
<h1 id="some-id">Lorem ipsum</h1>
109+
<h2 id="dolor-sit-amet-">Dolor sit amet 😪</h2>
59110
<h3 id="consectetur--adipisicing">consectetur &#x26; adipisicing</h3>
60111
<h4 id="elit">elit</h4>
61112
<h5 id="elit-1">elit</h5>
@@ -68,24 +119,37 @@ The default export is `rehypeSlug`.
68119

69120
### `unified().use(rehypeSlug)`
70121

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
72126

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+.
74139

75140
## Security
76141

77142
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.
84148

85149
## Related
86150

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
89153

90154
## Contribute
91155

@@ -131,6 +195,8 @@ abide by its terms.
131195

132196
[npm]: https://docs.npmjs.com/cli/install
133197

198+
[skypack]: https://www.skypack.dev
199+
134200
[health]: https://github.com/rehypejs/.github
135201

136202
[contributing]: https://github.com/rehypejs/.github/blob/HEAD/contributing.md
@@ -143,10 +209,18 @@ abide by its terms.
143209

144210
[author]: https://wooorm.com
145211

146-
[rehype]: https://github.com/rehypejs/rehype
212+
[typescript]: https://www.typescriptlang.org
147213

148-
[ghslug]: https://github.com/Flet/github-slugger
214+
[unified]: https://github.com/unifiedjs/unified
215+
216+
[rehype]: https://github.com/rehypejs/rehype
149217

150218
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
151219

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

Comments
 (0)