• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..16-Feb-2021-

types/H16-Feb-2021-40181

changelog.mdH A D16-Feb-2021123 63

licenseH A D16-Feb-20211.1 KiB2217

package.jsonH A D16-Feb-20212.8 KiB112111

readme.mdH A D16-Feb-202137.7 KiB1,395975

readme.md

1# [![unified][logo]][site]
2
3[![GitHub CI][github-ci-badge]][github-ci]
4[![Build][build-badge]][build]
5[![Coverage][coverage-badge]][coverage]
6[![Downloads][downloads-badge]][downloads]
7[![Size][size-badge]][size]
8[![Sponsors][sponsors-badge]][collective]
9[![Backers][backers-badge]][collective]
10[![Chat][chat-badge]][chat]
11
12**unified** is an interface for processing text using syntax trees.
13It’s what powers [**remark**][remark] (Markdown), [**retext**][retext] (natural
14language), and [**rehype**][rehype] (HTML), and allows for processing between
15formats.
16
17## Intro
18
19**unified** enables new exciting projects like [Gatsby][] to pull in Markdown,
20[MDX][] to embed [JSX][], and [Prettier][] to format it.
21It’s used in about 350k projects on GitHub and has about 15m downloads each
22month on npm: you’re probably using it.
23Some notable users are [Node.js][], [ZEIT][], [Netlify][], [GitHub][],
24[Mozilla][], [WordPress][], [Adobe][], [Facebook][], [Google][], and many more.
25
26*   To read about what we are up to, follow us [Twitter][]
27*   For a less technical and more practical introduction to unified, visit
28    [`unifiedjs.com`][site] and peruse its [Learn][] section
29*   Browse [awesome unified][awesome] to find out more about the ecosystem
30*   Questions?
31    Get help on [our Spectrum community][spectrum]!
32*   Check out [Contribute][] below to find out how to help out, or become a
33    backer or sponsor on [OpenCollective][collective]
34
35## Sponsors
36
37<!--lint ignore no-html maximum-line-length-->
38
39<table>
40  <tr valign="top">
41    <td width="33.33%" align="center" colspan="2">
42      <a href="https://www.gatsbyjs.org">Gatsby</a><br>��<br><br>
43      <a href="https://www.gatsbyjs.org"><img src="https://avatars1.githubusercontent.com/u/12551863?s=900&v=4"></a>
44    </td>
45    <td width="33.33%" align="center" colspan="2">
46      <a href="https://zeit.co">ZEIT</a><br>��<br><br>
47      <!--OC has a sharper image-->
48      <a href="https://zeit.co"><img src="https://images.opencollective.com/zeit/d8a5bee/logo/512.png"></a>
49    </td>
50    <td width="33.33%" align="center" colspan="2">
51      <a href="https://www.netlify.com">Netlify</a><br>��<br><br>
52      <!--OC has a sharper image-->
53      <a href="https://www.netlify.com"><img src="https://images.opencollective.com/netlify/4087de2/logo/512.png"></a>
54    </td>
55  </tr>
56  <tr valign="top">
57    <td width="16.67%" align="center">
58      <a href="https://www.holloway.com">Holloway</a><br><br><br>
59      <a href="https://www.holloway.com"><img src="https://avatars1.githubusercontent.com/u/35904294?s=300&v=4"></a>
60    </td>
61    <td width="16.67%" align="center">
62      <a href="https://themeisle.com">ThemeIsle</a><br>��<br><br>
63      <a href="https://themeisle.com"><img src="https://twitter-avatar.now.sh/themeisle"></a>
64    </td>
65    <td width="16.67%" align="center">
66      <a href="https://boostio.co">BoostIO</a><br>��<br><br>
67      <a href="https://boostio.co"><img src="https://avatars1.githubusercontent.com/u/13612118?s=300&v=4"></a>
68    </td>
69    <td width="50%" align="center" colspan="3">
70      <br><br><br><br>
71      <a href="https://opencollective.com/unified"><strong>You?</strong></a>
72    </td>
73  </tr>
74</table>
75
76## Install
77
78[npm][]:
79
80```sh
81npm install unified
82```
83
84This package comes with types.
85If you’re using TypeScript, make sure to also install
86[`@types/unist`][ts-unist].
87
88## Use
89
90```js
91var unified = require('unified')
92var markdown = require('remark-parse')
93var remark2rehype = require('remark-rehype')
94var doc = require('rehype-document')
95var format = require('rehype-format')
96var html = require('rehype-stringify')
97var report = require('vfile-reporter')
98
99unified()
100  .use(markdown)
101  .use(remark2rehype)
102  .use(doc, {title: '����'})
103  .use(format)
104  .use(html)
105  .process('# Hello world!', function (err, file) {
106    console.error(report(err || file))
107    console.log(String(file))
108  })
109```
110
111Yields:
112
113```txt
114no issues found
115```
116
117```html
118<!doctype html>
119<html lang="en">
120  <head>
121    <meta charset="utf-8">
122    <title>����</title>
123    <meta name="viewport" content="width=device-width, initial-scale=1">
124  </head>
125  <body>
126    <h1>Hello world!</h1>
127  </body>
128</html>
129```
130
131## Contents
132
133*   [Description](#description)
134*   [API](#api)
135    *   [`processor()`](#processor)
136    *   [`processor.use(plugin[, options])`](#processoruseplugin-options)
137    *   [`processor.parse(file)`](#processorparsefile)
138    *   [`processor.stringify(node[, file])`](#processorstringifynode-file)
139    *   [`processor.run(node[, file][, done])`](#processorrunnode-file-done)
140    *   [`processor.runSync(node[, file])`](#processorrunsyncnode-file)
141    *   [`processor.process(file[, done])`](#processorprocessfile-done)
142    *   [`processor.processSync(file|value)`](#processorprocesssyncfilevalue)
143    *   [`processor.data([key[, value]])`](#processordatakey-value)
144    *   [`processor.freeze()`](#processorfreeze)
145*   [`Plugin`](#plugin)
146    *   [`function attacher([options])`](#function-attacheroptions)
147    *   [`function transformer(node, file[, next])`](#function-transformernode-file-next)
148*   [`Preset`](#preset)
149*   [Contribute](#contribute)
150*   [Acknowledgments](#acknowledgments)
151*   [License](#license)
152
153## Description
154
155**unified** is an interface for processing text using syntax trees.
156Syntax trees are a representation of text understandable to programs.
157Those programs, called [*plugin*][plugin]s, take these trees and inspect and
158modify them.
159To get to the syntax tree from text, there is a [*parser*][parser].
160To get from that back to text, there is a [*compiler*][compiler].
161This is the [*process*][process] of a *processor*.
162
163```ascii
164| ........................ process ........................... |
165| .......... parse ... | ... run ... | ... stringify ..........|
166
167          +--------+                     +----------+
168Input ->- | Parser | ->- Syntax Tree ->- | Compiler | ->- Output
169          +--------+          |          +----------+
170                              X
171                              |
172                       +--------------+
173                       | Transformers |
174                       +--------------+
175```
176
177###### Processors
178
179Every **processor** implements another processor.
180To create a processor, call another processor.
181The new processor is configured to work the same as its ancestor.
182But when the descendant processor is configured in the future it does not affect
183the ancestral processor.
184
185When processors are exposed from a module (for example, `unified` itself) they
186should not be configured directly, as that would change their behavior for all
187module users.
188Those processors are [*frozen*][freeze] and they should be called to create a
189new processor before they are used.
190
191###### Syntax trees
192
193The **syntax trees** used in **unified** are [**unist**][unist] nodes.
194A [**node**][node] is a plain JavaScript objects with a `type` field.
195The semantics of nodes and format of syntax trees is defined by other projects.
196
197There are several [*utilities*][unist-utilities] for working with nodes.
198
199*   [**hast**][hast] — HTML
200*   [**mdast**][mdast] — Markdown
201*   [**nlcst**][nlcst] — Natural language
202*   [**xast**][xast] — XML
203
204###### List of processors
205
206The following projects process different [*syntax tree*][syntax-tree] formats.
207They parse text to a syntax tree and compile that back to text.
208These processors can be used as is, or their parser and compiler can be mixed
209and matched with **unified** and plugins to process between different syntaxes.
210
211*   [**rehype**][rehype] ([*hast*][hast]) — HTML
212*   [**remark**][remark] ([*mdast*][mdast]) — Markdown
213*   [**retext**][retext] ([*nlcst*][nlcst]) — Natural language
214
215###### List of plugins
216
217The below [**plugins**][plugin] work with **unified**, on all [*syntax
218tree*][syntax-tree] formats:
219
220*   [`unified-diff`](https://github.com/unifiedjs/unified-diff)
221    — Ignore messages for unchanged lines in Travis
222*   [`unified-message-control`](https://github.com/unifiedjs/unified-message-control)
223    — Enable, disable, and ignore messages
224
225See [**remark**][remark-plugins], [**rehype**][rehype-plugins], and
226[**retext**][retext-plugins] for their lists of plugins.
227
228###### File
229
230When processing a document, **metadata** is often gathered about that document.
231[**vfile**][vfile] is a virtual file format that stores data, metadata, and
232messages about files for **unified** and its plugins.
233
234There are several [*utilities*][vfile-utilities] for working with these files.
235
236###### Configuration
237
238[*Processors*][processors] are configured with [*plugin*][plugin]s or
239with the [`data`][data] method.
240
241###### Integrations
242
243**unified** can integrate with the file system with [`unified-engine`][engine].
244CLI apps can be created with [`unified-args`][args], Gulp plugins with
245[`unified-engine-gulp`][gulp], and Atom Linters with
246[`unified-engine-atom`][atom].
247
248[`unified-stream`][stream] provides a streaming interface.
249
250###### Programming interface
251
252The API provided by **unified** allows multiple files to be processed and gives
253access to *metadata* (such as lint messages):
254
255```js
256var unified = require('unified')
257var markdown = require('remark-parse')
258var styleGuide = require('remark-preset-lint-markdown-style-guide')
259var remark2retext = require('remark-retext')
260var english = require('retext-english')
261var equality = require('retext-equality')
262var remark2rehype = require('remark-rehype')
263var html = require('rehype-stringify')
264var report = require('vfile-reporter')
265
266unified()
267  .use(markdown)
268  .use(styleGuide)
269  .use(remark2retext, unified().use(english).use(equality))
270  .use(remark2rehype)
271  .use(html)
272  .process('*Emphasis* and _stress_, you guys!', function (err, file) {
273    console.error(report(err || file))
274    console.log(String(file))
275  })
276```
277
278Yields:
279
280```txt
281  1:16-1:24  warning  Emphasis should use `*` as a marker                                  emphasis-marker  remark-lint
282  1:30-1:34  warning  `guys` may be insensitive, use `people`, `persons`, `folks` instead  gals-men         retext-equality
283
284⚠ 2 warnings
285```
286
287```html
288<p><em>Emphasis</em> and <em>stress</em>, you guys!</p>
289```
290
291###### Processing between syntaxes
292
293[*Processors*][processors] can be combined in two modes.
294
295**Bridge** mode transforms the [*syntax tree*][syntax-tree] from one format
296(*origin*) to another (*destination*).
297Another processor runs on the destination tree.
298Finally, the original processor continues transforming the origin tree.
299
300**Mutate** mode also transforms the syntax tree from one format to another.
301But the original processor continues transforming the destination tree.
302
303In the previous example (“Programming interface”), `remark-retext` is used in
304*bridge* mode: the origin syntax tree is kept after [**retext**][retext] is
305done; whereas `remark-rehype` is used in *mutate* mode: it sets a new syntax
306tree and discards the origin tree.
307
308*   [`remark-retext`][remark-retext]
309*   [`remark-rehype`][remark-rehype]
310*   [`rehype-retext`][rehype-retext]
311*   [`rehype-remark`][rehype-remark]
312
313## API
314
315### `processor()`
316
317[*Processor*][processors] describing how to *process* text.
318
319###### Returns
320
321`Function` — New [*unfrozen*][freeze] processor that is configured to work the
322same as its ancestor.
323When the descendant processor is configured in the future it does not affect the
324ancestral processor.
325
326###### Example
327
328The following example shows how a new processor can be created (from the remark
329processor) and linked to **stdin**(4) and **stdout**(4).
330
331```js
332var remark = require('remark')
333var concat = require('concat-stream')
334
335process.stdin.pipe(concat(onconcat))
336
337function onconcat(buf) {
338  var doc = remark().processSync(buf).toString()
339
340  process.stdout.write(doc)
341}
342```
343
344### `processor.use(plugin[, options])`
345
346[*Configure*][configuration] the processor to use a [*plugin*][plugin] and
347optionally configure that plugin with options.
348
349If the processor is already using this plugin, the previous plugin configuration
350is changed based on the options that are passed in.
351The plugin is not added a second time.
352
353###### Signatures
354
355*   `processor.use(plugin[, options])`
356*   `processor.use(preset)`
357*   `processor.use(list)`
358
359###### Parameters
360
361*   `plugin` ([`Attacher`][plugin])
362*   `options` (`*`, optional) — Configuration for `plugin`
363*   `preset` (`Object`) — Object with an optional `plugins` (set to `list`),
364    and/or an optional `settings` object
365*   `list` (`Array`) — List of plugins, presets, and pairs (`plugin` and
366    `options` in an array)
367
368###### Returns
369
370`processor` — The processor that `use` was called on.
371
372###### Note
373
374`use` cannot be called on [*frozen*][freeze] processors.
375Call the processor first to create a new unfrozen processor.
376
377###### Example
378
379There are many ways to pass plugins to `.use()`.
380The below example gives an overview.
381
382```js
383var unified = require('unified')
384
385unified()
386  // Plugin with options:
387  .use(pluginA, {x: true, y: true})
388  // Passing the same plugin again merges configuration (to `{x: true, y: false, z: true}`):
389  .use(pluginA, {y: false, z: true})
390  // Plugins:
391  .use([pluginB, pluginC])
392  // Two plugins, the second with options:
393  .use([pluginD, [pluginE, {}]])
394  // Preset with plugins and settings:
395  .use({plugins: [pluginF, [pluginG, {}]], settings: {position: false}})
396  // Settings only:
397  .use({settings: {position: false}})
398```
399
400### `processor.parse(file)`
401
402Parse text to a [*syntax tree*][syntax-tree].
403
404###### Parameters
405
406*   `file` ([`VFile`][vfile]) — [*File*][file], any value accepted by `vfile()`
407
408###### Returns
409
410[`Node`][node] — Parsed [*syntax tree*][syntax-tree] representing `file`.
411
412###### Note
413
414`parse` freezes the processor if not already [*frozen*][freeze].
415
416`parse` performs the [*parse phase*][description], not the *run phase* or other
417phases.
418
419###### Example
420
421The below example shows how `parse` can be used to create a syntax tree from a
422file.
423
424```js
425var unified = require('unified')
426var markdown = require('remark-parse')
427
428var tree = unified().use(markdown).parse('# Hello world!')
429
430console.log(tree)
431```
432
433Yields:
434
435```js
436{
437  type: 'root',
438  children: [
439    {type: 'heading', depth: 1, children: [Array], position: [Position]}
440  ],
441  position: {
442    start: {line: 1, column: 1, offset: 0},
443    end: {line: 1, column: 15, offset: 14}
444  }
445}
446```
447
448#### `processor.Parser`
449
450A **parser** handles the parsing of text to a [*syntax tree*][syntax-tree].
451Used in the [*parse phase*][description] and called with a `string` and
452[`VFile`][vfile] representation of the text to parse.
453
454`Parser` can be a function, in which case it must return a [`Node`][node]: the
455syntax tree representation of the given file.
456
457`Parser` can also be a constructor function (a function with a `parse` field, or
458other fields, in its `prototype`), in which case it’s constructed with `new`.
459Instances must have a `parse` method that is called without arguments and must
460return a [`Node`][node].
461
462### `processor.stringify(node[, file])`
463
464Compile a [*syntax tree*][syntax-tree].
465
466###### Parameters
467
468*   `node` ([`Node`][node]) — [*Syntax tree*][syntax-tree] to compile
469*   `file` ([`VFile`][vfile], optional) — [*File*][file], any value accepted by
470    `vfile()`
471
472###### Returns
473
474`string` or `Buffer` (see notes) — Textual representation of the [*syntax
475tree*][syntax-tree]
476
477###### Note
478
479`stringify` freezes the processor if not already [*frozen*][freeze].
480
481`stringify` performs the [*stringify phase*][description], not the *run phase*
482or other phases.
483
484unified typically compiles by serializing: most [*compiler*][compiler]s return
485`string` (or `Buffer`).
486Some compilers, such as the one configured with [`rehype-react`][rehype-react],
487return other values (in this case, a React tree).
488If you’re using a compiler doesn’t serialize, expect different result values.
489When using TypeScript, cast the type on your side.
490
491###### Example
492
493The below example shows how `stringify` can be used to serialize a syntax tree.
494
495```js
496var unified = require('unified')
497var html = require('rehype-stringify')
498var h = require('hastscript')
499
500var tree = h('h1', 'Hello world!')
501
502var doc = unified().use(html).stringify(tree)
503
504console.log(doc)
505```
506
507Yields:
508
509```html
510<h1>Hello world!</h1>
511```
512
513#### `processor.Compiler`
514
515A **compiler** handles the compiling of a [*syntax tree*][syntax-tree] to text.
516Used in the [*stringify phase*][description] and called with a [`Node`][node]
517and [`VFile`][file] representation of syntax tree to compile.
518
519`Compiler` can be a function, in which case it should return a `string`: the
520textual representation of the syntax tree.
521
522`Compiler` can also be a constructor function (a function with a `compile`
523field, or other fields, in its `prototype`), in which case it’s constructed with
524`new`.
525Instances must have a `compile` method that is called without arguments and
526should return a `string`.
527
528### `processor.run(node[, file][, done])`
529
530Run [*transformers*][transformer] on a [*syntax tree*][syntax-tree].
531
532###### Parameters
533
534*   `node` ([`Node`][node]) — [*Syntax tree*][syntax-tree] to run on
535*   `file` ([`VFile`][vfile], optional) — [*File*][file], any value accepted by
536    `vfile()`
537*   `done` ([`Function`][run-done], optional) — Callback
538
539###### Returns
540
541[`Promise`][promise] if `done` is not given.
542The returned promise is rejected with a fatal error, or resolved with the
543transformed [*syntax tree*][syntax-tree].
544
545###### Note
546
547`run` freezes the processor if not already [*frozen*][freeze].
548
549`run` performs the [*run phase*][description], not other phases.
550
551#### `function done(err[, node, file])`
552
553Callback called when [*transformers*][transformer] are done.
554Called with either an error or results.
555
556###### Parameters
557
558*   `err` (`Error`, optional) — Fatal error
559*   `node` ([`Node`][node], optional) — Transformed [*syntax tree*][syntax-tree]
560*   `file` ([`VFile`][vfile], optional) — [*File*][file]
561
562###### Example
563
564The below example shows how `run` can be used to transform a syntax tree.
565
566```js
567var unified = require('unified')
568var references = require('remark-reference-links')
569var u = require('unist-builder')
570
571var tree = u('root', [
572  u('paragraph', [
573    u('link', {href: 'https://example.com'}, [u('text', 'Example Domain')])
574  ])
575])
576
577unified()
578  .use(references)
579  .run(tree, function (err, tree) {
580    if (err) throw err
581    console.log(tree)
582  })
583```
584
585Yields:
586
587```js
588{
589  type: 'root',
590  children: [
591    {type: 'paragraph', children: [Array]},
592    {type: 'definition', identifier: '1', title: undefined, url: undefined}
593  ]
594}
595```
596
597### `processor.runSync(node[, file])`
598
599Run [*transformers*][transformer] on a [*syntax tree*][syntax-tree].
600
601An error is thrown if asynchronous [*plugin*][plugin]s are configured.
602
603###### Parameters
604
605*   `node` ([`Node`][node]) — [*Syntax tree*][syntax-tree] to run on
606*   `file` ([`VFile`][vfile], optional) — [*File*][file], any value accepted by
607    `vfile()`
608
609###### Returns
610
611[`Node`][node] — Transformed [*syntax tree*][syntax-tree].
612
613###### Note
614
615`runSync` freezes the processor if not already [*frozen*][freeze].
616
617`runSync` performs the [*run phase*][description], not other phases.
618
619### `processor.process(file[, done])`
620
621[*Process*][description] the given [*file*][file] as configured on the
622processor.
623
624###### Parameters
625
626*   `file` ([`VFile`][vfile]) — [*File*][file], any value accepted by `vfile()`
627*   `done` ([`Function`][process-done], optional) — Callback
628
629###### Returns
630
631[`Promise`][promise] if `done` is not given.
632The returned promise is rejected with a fatal error, or resolved with the
633processed [*file*][file].
634
635The parsed, transformed, and compiled value is exposed on
636[`file.contents`][vfile-contents] or `file.result` (see notes).
637
638###### Note
639
640`process` freezes the processor if not already [*frozen*][freeze].
641
642`process` performs the [*parse*, *run*, and *stringify* phases][description].
643
644Be aware that [*compiler*][compiler]s typically, but not always, return
645`string`.
646Some compilers, such as the one configured with [`rehype-react`][rehype-react],
647return other values (in this case, a React tree).
648When using TypeScript, cast the type of [`file.contents`][vfile-contents] on
649your side.
650
651unified typically compiles by serializing: most [*compiler*][compiler]s return
652`string` (or `Buffer`).
653Some compilers, such as the one configured with [`rehype-react`][rehype-react],
654return other values (in this case, a React tree).
655If you’re using a compiler that serializes, the result is available at
656`file.contents`.
657Otherwise, the result is available at `file.result`.
658
659###### Example
660
661The below example shows how `process` can be used to process a file, whether
662transformers are asynchronous or not, with promises.
663
664```js
665var unified = require('unified')
666var markdown = require('remark-parse')
667var remark2rehype = require('remark-rehype')
668var doc = require('rehype-document')
669var format = require('rehype-format')
670var html = require('rehype-stringify')
671
672unified()
673  .use(markdown)
674  .use(remark2rehype)
675  .use(doc, {title: '����'})
676  .use(format)
677  .use(html)
678  .process('# Hello world!')
679  .then(
680    function (file) {
681      console.log(String(file))
682    },
683    function (err) {
684      console.error(String(err))
685    }
686  )
687```
688
689Yields:
690
691```html
692<!doctype html>
693<html lang="en">
694  <head>
695    <meta charset="utf-8">
696    <title>����</title>
697    <meta name="viewport" content="width=device-width, initial-scale=1">
698  </head>
699  <body>
700    <h1>Hello world!</h1>
701  </body>
702</html>
703```
704
705#### `function done(err, file)`
706
707Callback called when the [*process*][description] is done.
708Called with a fatal error, if any, and a [*file*][file].
709
710###### Parameters
711
712*   `err` (`Error`, optional) — Fatal error
713*   `file` ([`VFile`][vfile]) — Processed [*file*][file]
714
715###### Example
716
717The below example shows how `process` can be used to process a file, whether
718transformers are asynchronous or not, with a callback.
719
720```js
721var unified = require('unified')
722var parse = require('remark-parse')
723var stringify = require('remark-stringify')
724var github = require('remark-github')
725var report = require('vfile-reporter')
726
727unified()
728  .use(parse)
729  .use(github)
730  .use(stringify)
731  .process('@wooorm', function (err, file) {
732    console.error(report(err || file))
733    console.log(String(file))
734  })
735```
736
737Yields:
738
739```txt
740no issues found
741```
742
743```markdown
744[**@wooorm**](https://github.com/wooorm)
745```
746
747### `processor.processSync(file|value)`
748
749[*Process*][description] the given [*file*][file] as configured on the
750processor.
751
752An error is thrown if asynchronous [*plugin*][plugin]s are configured.
753
754###### Parameters
755
756*   `file` ([`VFile`][vfile]) — [*File*][file], any value accepted by `vfile()`
757
758###### Returns
759
760([`VFile`][vfile]) — Processed [*file*][file]
761
762The parsed, transformed, and compiled value is exposed on
763[`file.contents`][vfile-contents] or `file.result` (see notes).
764
765###### Note
766
767`processSync` freezes the processor if not already [*frozen*][freeze].
768
769`processSync` performs the [*parse*, *run*, and *stringify*
770phases][description].
771
772unified typically compiles by serializing: most [*compiler*][compiler]s return
773`string` (or `Buffer`).
774Some compilers, such as the one configured with [`rehype-react`][rehype-react],
775return other values (in this case, a React tree).
776If you’re using a compiler that serializes, the result is available at
777`file.contents`.
778Otherwise, the result is available at `file.result`.
779
780###### Example
781
782The below example shows how `processSync` can be used to process a file, if all
783transformers are synchronous.
784
785```js
786var unified = require('unified')
787var markdown = require('remark-parse')
788var remark2rehype = require('remark-rehype')
789var doc = require('rehype-document')
790var format = require('rehype-format')
791var html = require('rehype-stringify')
792
793var processor = unified()
794  .use(markdown)
795  .use(remark2rehype)
796  .use(doc, {title: '����'})
797  .use(format)
798  .use(html)
799
800console.log(processor.processSync('# Hello world!').toString())
801```
802
803Yields:
804
805```html
806<!doctype html>
807<html lang="en">
808  <head>
809    <meta charset="utf-8">
810    <title>����</title>
811    <meta name="viewport" content="width=device-width, initial-scale=1">
812  </head>
813  <body>
814    <h1>Hello world!</h1>
815  </body>
816</html>
817```
818
819### `processor.data([key[, value]])`
820
821[*Configure*][configuration] the processor with information available to all
822[*plugin*][plugin]s.
823Information is stored in an in-memory key-value store.
824
825Typically, options can be given to a specific plugin, but sometimes it makes
826sense to have information shared with several plugins.
827For example, a list of HTML elements that are self-closing, which is needed
828during all [*phases*][description] of the *process*.
829
830###### Signatures
831
832*   `processor = processor.data(key, value)`
833*   `processor = processor.data(values)`
834*   `value = processor.data(key)`
835*   `info = processor.data()`
836
837###### Parameters
838
839*   `key` (`string`, optional) — Identifier
840*   `value` (`*`, optional) — Value to set
841*   `values` (`Object`, optional) — Values to set
842
843###### Returns
844
845*   `processor` — If setting, the processor that `data` is called on
846*   `value` (`*`) — If getting, the value at `key`
847*   `info` (`Object`) — Without arguments, the key-value store
848
849###### Note
850
851Setting information cannot occur on [*frozen*][freeze] processors.
852Call the processor first to create a new unfrozen processor.
853
854###### Example
855
856The following example show how to get and set information:
857
858```js
859var unified = require('unified')
860
861var processor = unified().data('alpha', 'bravo')
862
863processor.data('alpha') // => 'bravo'
864
865processor.data() // {alpha: 'bravo'}
866
867processor.data({charlie: 'delta'})
868
869processor.data() // {charlie: 'delta'}
870```
871
872### `processor.freeze()`
873
874**Freeze** a processor.
875*Frozen* processors are meant to be extended and not to be configured directly.
876
877Once a processor is frozen it cannot be *unfrozen*.
878New processors working the same way can be created by calling the processor.
879
880It’s possible to freeze processors explicitly by calling `.freeze()`.
881Processors freeze implicitly when [`.parse()`][parse], [`.run()`][run],
882[`.runSync()`][run-sync], [`.stringify()`][stringify], [`.process()`][process],
883or [`.processSync()`][process-sync] are called.
884
885###### Returns
886
887`processor` — The processor that `freeze` was called on.
888
889###### Example
890
891The following example, `index.js`, shows how rehype prevents extensions to
892itself:
893
894```js
895var unified = require('unified')
896var parse = require('rehype-parse')
897var stringify = require('rehype-stringify')
898
899module.exports = unified().use(parse).use(stringify).freeze()
900```
901
902The below example, `a.js`, shows how that processor can be used and configured.
903
904```js
905var rehype = require('rehype')
906var format = require('rehype-format')
907// …
908
909rehype()
910  .use(format)
911  // …
912```
913
914The below example, `b.js`, shows a similar looking example that operates on the
915frozen rehype interface because it does not call `rehype`.
916If this behavior was allowed it would result in unexpected behavior so an
917error is thrown.
918**This is invalid**:
919
920```js
921var rehype = require('rehype')
922var format = require('rehype-format')
923// …
924
925rehype
926  .use(format)
927  // …
928```
929
930Yields:
931
932```txt
933~/node_modules/unified/index.js:440
934    throw new Error(
935    ^
936
937Error: Cannot invoke `use` on a frozen processor.
938Create a new processor first, by invoking it: use `processor()` instead of `processor`.
939    at assertUnfrozen (~/node_modules/unified/index.js:440:11)
940    at Function.use (~/node_modules/unified/index.js:172:5)
941    at Object.<anonymous> (~/b.js:6:4)
942```
943
944## `Plugin`
945
946**Plugins** [*configure*][configuration] the processors they are applied on in
947the following ways:
948
949*   They change the processor: such as the [*parser*][parser], the
950    [*compiler*][compiler], or configuring [*data*][data]
951*   They specify how to handle [*syntax trees*][syntax-tree] and [*files*][file]
952
953Plugins are a concept.
954They materialize as [`attacher`][attacher]s.
955
956###### Example
957
958`move.js`:
959
960```js
961module.exports = move
962
963function move(options) {
964  var expected = (options || {}).extname
965
966  if (!expected) {
967    throw new Error('Missing `extname` in options')
968  }
969
970  return transformer
971
972  function transformer(tree, file) {
973    if (file.extname && file.extname !== expected) {
974      file.extname = expected
975    }
976  }
977}
978```
979
980`index.md`:
981
982```markdown
983# Hello, world!
984```
985
986`index.js`:
987
988```js
989var unified = require('unified')
990var parse = require('remark-parse')
991var remark2rehype = require('remark-rehype')
992var stringify = require('rehype-stringify')
993var vfile = require('to-vfile')
994var report = require('vfile-reporter')
995var move = require('./move')
996
997unified()
998  .use(parse)
999  .use(remark2rehype)
1000  .use(move, {extname: '.html'})
1001  .use(stringify)
1002  .process(vfile.readSync('index.md'), function (err, file) {
1003    console.error(report(err || file))
1004    if (file) {
1005      vfile.writeSync(file) // Written to `index.html`.
1006    }
1007  })
1008```
1009
1010Yields:
1011
1012```txt
1013index.md: no issues found
1014```
1015
1016`index.html`:
1017
1018```html
1019<h1>Hello, world!</h1>
1020```
1021
1022### `function attacher([options])`
1023
1024**Attachers** are materialized [*plugin*][plugin]s.
1025An attacher is a function that can receive options and
1026[*configures*][configuration] the processor.
1027
1028Attachers change the processor, such as the [*parser*][parser], the
1029[*compiler*][compiler], configuring [*data*][data], or by specifying how the
1030[*syntax tree*][syntax-tree] or [*file*][file] are handled.
1031
1032###### Context
1033
1034The context object (`this`) is set to the processor the attacher is applied on.
1035
1036###### Parameters
1037
1038*   `options` (`*`, optional) — Configuration
1039
1040###### Returns
1041
1042[`transformer`][transformer] — Optional.
1043
1044###### Note
1045
1046Attachers are called when the processor is [*frozen*][freeze], not when they are
1047applied.
1048
1049### `function transformer(node, file[, next])`
1050
1051**Transformers** handle [*syntax tree*][syntax-tree]s and [*file*][file]s.
1052A transformer is a function that is called each time a syntax tree and file are
1053passed through the [*run phase*][description].
1054If an error occurs (either because it’s thrown, returned, rejected, or passed to
1055[`next`][next]), the process stops.
1056
1057The *run phase* is handled by [`trough`][trough], see its documentation for the
1058exact semantics of these functions.
1059
1060###### Parameters
1061
1062*   `node` ([`Node`][node]) — [*Syntax tree*][syntax-tree] to handle
1063*   `file` ([`VFile`][vfile]) — [*File*][file] to handle
1064*   `next` ([`Function`][next], optional)
1065
1066###### Returns
1067
1068*   `void` — If nothing is returned, the next transformer keeps using same tree.
1069*   `Error` — Fatal error to stop the process
1070*   `node` ([`Node`][node]) — New [*syntax tree*][syntax-tree].
1071    If returned, the next transformer is given this new tree
1072*   `Promise` — Returned to perform an asynchronous operation.
1073    The promise **must** be resolved (optionally with a [`Node`][node]) or
1074    rejected (optionally with an `Error`)
1075
1076#### `function next(err[, tree[, file]])`
1077
1078If the signature of a [*transformer*][transformer] includes `next` (the third
1079argument), the transformer **may** perform asynchronous operations, and **must**
1080call `next()`.
1081
1082###### Parameters
1083
1084*   `err` (`Error`, optional) — Fatal error to stop the process
1085*   `node` ([`Node`][node], optional) — New [*syntax tree*][syntax-tree].
1086    If given, the next transformer is given this new tree
1087*   `file` ([`VFile`][vfile], optional) — New [*file*][file].
1088    If given, the next transformer is given this new file
1089
1090## `Preset`
1091
1092**Presets** are sharable [*configuration*][configuration].
1093They can contain [*plugins*][plugin] and settings.
1094
1095###### Example
1096
1097`preset.js`:
1098
1099```js
1100exports.settings = {bullet: '*', emphasis: '*', fences: true}
1101
1102exports.plugins = [
1103  require('remark-preset-lint-recommended'),
1104  require('remark-preset-lint-consistent'),
1105  require('remark-comment-config'),
1106  [require('remark-toc'), {maxDepth: 3, tight: true}],
1107  require('remark-license')
1108]
1109```
1110
1111`readme.md`:
1112
1113```markdown
1114# Hello, world!
1115
1116_Emphasis_ and **importance**.
1117
1118## Table of contents
1119
1120## API
1121
1122## License
1123```
1124
1125`index.js`:
1126
1127```js
1128var remark = require('remark')
1129var vfile = require('to-vfile')
1130var report = require('vfile-reporter')
1131var preset = require('./preset')
1132
1133remark()
1134  .use(preset)
1135  .process(vfile.readSync('readme.md'), function (err, file) {
1136    console.error(report(err || file))
1137
1138    if (file) {
1139      vfile.writeSync(file)
1140    }
1141  })
1142```
1143
1144Yields:
1145
1146```txt
1147readme.md: no issues found
1148```
1149
1150`readme.md` now contains:
1151
1152```markdown
1153# Hello, world!
1154
1155*Emphasis* and **importance**.
1156
1157## Table of contents
1158
1159*   [API](#api)
1160*   [License](#license)
1161
1162## API
1163
1164## License
1165
1166[MIT](license) © [Titus Wormer](https://wooorm.com)
1167```
1168
1169## Contribute
1170
1171See [`contributing.md`][contributing] in [`unifiedjs/.github`][health] for ways
1172to get started.
1173See [`support.md`][support] for ways to get help.
1174Ideas for new plugins and tools can be posted in [`unifiedjs/ideas`][ideas].
1175
1176A curated list of awesome unified resources can be found in [**awesome
1177unified**][awesome].
1178
1179This project has a [code of conduct][coc].
1180By interacting with this repository, organization, or community you agree to
1181abide by its terms.
1182
1183## Acknowledgments
1184
1185Preliminary work for unified was done [in 2014][preliminary] for
1186[**retext**][retext] and inspired by [`ware`][ware].
1187Further incubation happened in [**remark**][remark].
1188The project was finally [externalised][] in 2015 and [published][] as `unified`.
1189The project was authored by [**@wooorm**](https://github.com/wooorm).
1190
1191Although `unified` since moved its plugin architecture to [`trough`][trough],
1192thanks to [**@calvinfo**](https://github.com/calvinfo),
1193[**@ianstormtaylor**](https://github.com/ianstormtaylor), and others for their
1194work on [`ware`][ware], as it was a huge initial inspiration.
1195
1196## License
1197
1198[MIT][license] © [Titus Wormer][author]
1199
1200<!-- Definitions -->
1201
1202[logo]: https://raw.githubusercontent.com/unifiedjs/unified/39917ea/logo.svg?sanitize=true
1203
1204[github-ci-badge]: https://github.com/unifiedjs/unified/workflows/CI/badge.svg
1205
1206[github-ci]: https://github.com/unifiedjs/unified/actions
1207
1208[build-badge]: https://img.shields.io/travis/unifiedjs/unified.svg
1209
1210[build]: https://travis-ci.org/unifiedjs/unified
1211
1212[coverage-badge]: https://img.shields.io/codecov/c/github/unifiedjs/unified.svg
1213
1214[coverage]: https://codecov.io/github/unifiedjs/unified
1215
1216[downloads-badge]: https://img.shields.io/npm/dm/unified.svg
1217
1218[downloads]: https://www.npmjs.com/package/unified
1219
1220[size-badge]: https://img.shields.io/bundlephobia/minzip/unified.svg
1221
1222[size]: https://bundlephobia.com/result?p=unified
1223
1224[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
1225
1226[backers-badge]: https://opencollective.com/unified/backers/badge.svg
1227
1228[collective]: https://opencollective.com/unified
1229
1230[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg
1231
1232[chat]: https://spectrum.chat/unified
1233
1234[health]: https://github.com/unifiedjs/.github
1235
1236[contributing]: https://github.com/unifiedjs/.github/blob/master/contributing.md
1237
1238[support]: https://github.com/unifiedjs/.github/blob/master/support.md
1239
1240[coc]: https://github.com/unifiedjs/.github/blob/master/code-of-conduct.md
1241
1242[awesome]: https://github.com/unifiedjs/awesome-unified
1243
1244[license]: license
1245
1246[author]: https://wooorm.com
1247
1248[npm]: https://docs.npmjs.com/cli/install
1249
1250[ts-unist]: https://www.npmjs.com/package/@types/unist
1251
1252[site]: https://unifiedjs.com
1253
1254[twitter]: https://twitter.com/unifiedjs
1255
1256[learn]: https://unifiedjs.com/learn/
1257
1258[spectrum]: https://spectrum.chat/unified
1259
1260[rehype]: https://github.com/rehypejs/rehype
1261
1262[remark]: https://github.com/remarkjs/remark
1263
1264[retext]: https://github.com/retextjs/retext
1265
1266[hast]: https://github.com/syntax-tree/hast
1267
1268[mdast]: https://github.com/syntax-tree/mdast
1269
1270[nlcst]: https://github.com/syntax-tree/nlcst
1271
1272[xast]: https://github.com/syntax-tree/xast
1273
1274[unist]: https://github.com/syntax-tree/unist
1275
1276[engine]: https://github.com/unifiedjs/unified-engine
1277
1278[args]: https://github.com/unifiedjs/unified-args
1279
1280[gulp]: https://github.com/unifiedjs/unified-engine-gulp
1281
1282[atom]: https://github.com/unifiedjs/unified-engine-atom
1283
1284[remark-rehype]: https://github.com/remarkjs/remark-rehype
1285
1286[remark-retext]: https://github.com/remarkjs/remark-retext
1287
1288[rehype-retext]: https://github.com/rehypejs/rehype-retext
1289
1290[rehype-remark]: https://github.com/rehypejs/rehype-remark
1291
1292[unist-utilities]: https://github.com/syntax-tree/unist#list-of-utilities
1293
1294[vfile]: https://github.com/vfile/vfile
1295
1296[vfile-contents]: https://github.com/vfile/vfile#vfilecontents
1297
1298[vfile-utilities]: https://github.com/vfile/vfile#related-tools
1299
1300[node]: https://github.com/syntax-tree/unist#node
1301
1302[description]: #description
1303
1304[syntax-tree]: #syntax-trees
1305
1306[configuration]: #configuration
1307
1308[file]: #file
1309
1310[processors]: #processors
1311
1312[process]: #processorprocessfile-done
1313
1314[process-sync]: #processorprocesssyncfilevalue
1315
1316[parse]: #processorparsefile
1317
1318[parser]: #processorparser
1319
1320[stringify]: #processorstringifynode-file
1321
1322[run]: #processorrunnode-file-done
1323
1324[run-sync]: #processorrunsyncnode-file
1325
1326[compiler]: #processorcompiler
1327
1328[data]: #processordatakey-value
1329
1330[attacher]: #function-attacheroptions
1331
1332[transformer]: #function-transformernode-file-next
1333
1334[next]: #function-nexterr-tree-file
1335
1336[freeze]: #processorfreeze
1337
1338[plugin]: #plugin
1339
1340[run-done]: #function-doneerr-node-file
1341
1342[process-done]: #function-doneerr-file
1343
1344[contribute]: #contribute
1345
1346[rehype-react]: https://github.com/rhysd/rehype-react
1347
1348[trough]: https://github.com/wooorm/trough#function-fninput-next
1349
1350[promise]: https://developer.mozilla.org/Web/JavaScript/Reference/Global_Objects/Promise
1351
1352[remark-plugins]: https://github.com/remarkjs/remark/blob/master/doc/plugins.md#list-of-plugins
1353
1354[rehype-plugins]: https://github.com/rehypejs/rehype/blob/master/doc/plugins.md#list-of-plugins
1355
1356[retext-plugins]: https://github.com/retextjs/retext/blob/master/doc/plugins.md#list-of-plugins
1357
1358[stream]: https://github.com/unifiedjs/unified-stream
1359
1360[ideas]: https://github.com/unifiedjs/ideas
1361
1362[preliminary]: https://github.com/retextjs/retext/commit/8fcb1f#diff-168726dbe96b3ce427e7fedce31bb0bc
1363
1364[externalised]: https://github.com/remarkjs/remark/commit/9892ec#diff-168726dbe96b3ce427e7fedce31bb0bc
1365
1366[published]: https://github.com/unifiedjs/unified/commit/2ba1cf
1367
1368[ware]: https://github.com/segmentio/ware
1369
1370[gatsby]: https://www.gatsbyjs.org
1371
1372[mdx]: https://mdxjs.com
1373
1374[jsx]: https://reactjs.org/docs/jsx-in-depth.html
1375
1376[prettier]: https://prettier.io
1377
1378[node.js]: https://nodejs.org
1379
1380[zeit]: https://zeit.co
1381
1382[netlify]: https://www.netlify.com
1383
1384[github]: https://github.com
1385
1386[mozilla]: https://www.mozilla.org
1387
1388[wordpress]: https://wordpress.com
1389
1390[adobe]: https://www.adobe.com
1391
1392[facebook]: https://www.facebook.com
1393
1394[google]: https://www.google.com
1395