|
1 | 1 | ---
|
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 |
3 | 6 | 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 |
6 | 14 | title: Using plugins
|
7 | 15 | ---
|
8 | 16 |
|
9 | 17 | ## Using plugins
|
10 | 18 |
|
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. |
14 | 21 |
|
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 |
0 commit comments