Skip to content

Commit f65a976

Browse files
committed
Add Using plugins guide
1 parent 41f6ac7 commit f65a976

File tree

2 files changed

+98
-7
lines changed

2 files changed

+98
-7
lines changed

doc/learn/using-plugins.md

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,105 @@
11
---
2-
description: Guide that shows how to use plugins and presets
2+
authorGithub: wooorm
3+
authorTwitter: wooorm
4+
author: Titus Wormer
5+
description: How to use plugins and presets
36
group: guide
4-
modified: 2024-08-05
5-
published: 2019-12-12
7+
modified: 2024-08-08
8+
published: 2024-08-08
9+
tags:
10+
- plugin
11+
- preset
12+
- unified
13+
- use
614
title: Using plugins
715
---
816

917
## Using plugins
1018

11-
Unfortunately, this guide is not yet written.
12-
We’re looking for help with that, if you want please edit this file on
13-
[GitHub][].
19+
You can use plugins and presets to extend unified by calling
20+
[`use`][unified-use] on a processor.
1421

15-
[github]: https://github.com/unifiedjs/unifiedjs.github.io/blob/main/doc/learn/using-plugins.md
22+
A small example is:
23+
24+
```js twoslash
25+
/// <reference types="node" />
26+
// ---cut---
27+
import rehypeDocument from 'rehype-document'
28+
import rehypeFormat from 'rehype-format'
29+
import rehypeStringify from 'rehype-stringify'
30+
import remarkParse from 'remark-parse'
31+
import remarkRehype from 'remark-rehype'
32+
import {read} from 'to-vfile'
33+
import {unified} from 'unified'
34+
35+
const file = await read('example.md')
36+
37+
await unified()
38+
.use(remarkParse)
39+
.use(remarkRehype)
40+
.use(rehypeDocument, {title: '👋🌍'})
41+
.use(rehypeFormat)
42+
.use(rehypeStringify)
43+
.process(file)
44+
45+
console.log(String(file))
46+
```
47+
48+
This example shows how several plugins are used.
49+
It shows that the order in which plugins are used is important.
50+
And it shows that plugins can be configured by passing options to them.
51+
In this case,
52+
`rehypeDocument` receives a `title` field.
53+
54+
Using presets is similar:
55+
56+
```js twoslash
57+
/// <reference types="node" />
58+
// ---cut---
59+
import rehypeParse from 'rehype-parse'
60+
import rehypePresetMinify from 'rehype-preset-minify'
61+
import rehypeStringify from 'rehype-stringify'
62+
import {read} from 'to-vfile'
63+
import {unified} from 'unified'
64+
65+
const file = await read('example.html')
66+
67+
await unified()
68+
.use(rehypeParse)
69+
.use(rehypePresetMinify)
70+
.use(rehypeStringify)
71+
.process(file)
72+
73+
console.log(String(file))
74+
```
75+
76+
Presets themselves cannot receive options.
77+
Sometimes,
78+
you still want to pass options to a particular plugin in a preset.
79+
To configure a plugin in a preset,
80+
use it after the preset,
81+
with the correct options:
82+
83+
```js twoslash
84+
/// <reference types="node" />
85+
// ---cut---
86+
import rehypeMinifyWhitespace from 'rehype-minify-whitespace'
87+
import rehypeParse from 'rehype-parse'
88+
import rehypePresetMinify from 'rehype-preset-minify'
89+
import rehypeStringify from 'rehype-stringify'
90+
import {read} from 'to-vfile'
91+
import {unified} from 'unified'
92+
93+
const file = await read('example.html')
94+
95+
await unified()
96+
.use(rehypeParse)
97+
.use(rehypePresetMinify)
98+
.use(rehypeMinifyWhitespace, {newlines: true})
99+
.use(rehypeStringify)
100+
.process(file)
101+
102+
console.log(String(file))
103+
```
104+
105+
[unified-use]: https://github.com/unifiedjs/unified#processoruseplugin-options

generate/plugin/rehype-link.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const ignore = [
1212
...defaultIgnore,
1313
'a',
1414
'code',
15+
'div', // Twoslash?
1516
'h1',
1617
'h2',
1718
'h3',

0 commit comments

Comments
 (0)