Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.md 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. # [![unified][logo]][site]
  2. [![Build][build-badge]][build]
  3. [![Coverage][coverage-badge]][coverage]
  4. [![Downloads][downloads-badge]][downloads]
  5. [![Size][size-badge]][size]
  6. [![Sponsors][sponsors-badge]][collective]
  7. [![Backers][backers-badge]][collective]
  8. [![Chat][chat-badge]][chat]
  9. **unified** is an interface for processing text using syntax trees.
  10. It’s what powers [**remark**][remark] (Markdown), [**retext**][retext] (natural
  11. language), and [**rehype**][rehype] (HTML), and allows for processing between
  12. formats.
  13. ## Intro
  14. **unified** enables new exciting projects like [Gatsby][] to pull in Markdown,
  15. [MDX][] to embed [JSX][], and [Prettier][] to format it.
  16. It’s used in about 500k projects on GitHub and has about 25m downloads each
  17. month on npm: you’re probably using it.
  18. Some notable users are [Node.js][], [Vercel][], [Netlify][], [GitHub][],
  19. [Mozilla][], [WordPress][], [Adobe][], [Facebook][], [Google][], and many more.
  20. * To read about what we are up to, follow us [Twitter][]
  21. * For a less technical and more practical introduction to unified, visit
  22. [`unifiedjs.com`][site] and peruse its [Learn][] section
  23. * Browse [awesome unified][awesome] to find out more about the ecosystem
  24. * Questions?
  25. Get help on [Discussions][chat]!
  26. * Check out [Contribute][] below to find out how to help out, or become a
  27. backer or sponsor on [OpenCollective][collective]
  28. ## Sponsors
  29. Support this effort and give back by sponsoring on [OpenCollective][collective]!
  30. <!--lint ignore no-html-->
  31. <table>
  32. <tr valign="middle">
  33. <td width="20%" align="center" colspan="2">
  34. <a href="https://www.gatsbyjs.org">Gatsby</a> 🥇<br><br>
  35. <a href="https://www.gatsbyjs.org"><img src="https://avatars1.githubusercontent.com/u/12551863?s=256&v=4" width="128"></a>
  36. </td>
  37. <td width="20%" align="center" colspan="2">
  38. <a href="https://vercel.com">Vercel</a> 🥇<br><br>
  39. <a href="https://vercel.com"><img src="https://avatars1.githubusercontent.com/u/14985020?s=256&v=4" width="128"></a>
  40. </td>
  41. <td width="20%" align="center" colspan="2">
  42. <a href="https://www.netlify.com">Netlify</a><br><br>
  43. <!--OC has a sharper image-->
  44. <a href="https://www.netlify.com"><img src="https://images.opencollective.com/netlify/4087de2/logo/256.png" width="128"></a>
  45. </td>
  46. <td width="10%" align="center">
  47. <a href="https://www.holloway.com">Holloway</a><br><br>
  48. <a href="https://www.holloway.com"><img src="https://avatars1.githubusercontent.com/u/35904294?s=128&v=4" width="64"></a>
  49. </td>
  50. <td width="10%" align="center">
  51. <a href="https://themeisle.com">ThemeIsle</a><br><br>
  52. <a href="https://themeisle.com"><img src="https://avatars1.githubusercontent.com/u/58979018?s=128&v=4" width="64"></a>
  53. </td>
  54. <td width="10%" align="center">
  55. <a href="https://boosthub.io">Boost Hub</a><br><br>
  56. <a href="https://boosthub.io"><img src="https://images.opencollective.com/boosthub/6318083/logo/128.png" width="64"></a>
  57. </td>
  58. <td width="10%" align="center">
  59. <a href="https://expo.io">Expo</a><br><br>
  60. <a href="https://expo.io"><img src="https://avatars1.githubusercontent.com/u/12504344?s=128&v=4" width="64"></a>
  61. </td>
  62. </tr>
  63. <tr valign="middle">
  64. <td width="100%" align="center" colspan="10">
  65. <br>
  66. <a href="https://opencollective.com/unified"><strong>You?</strong></a>
  67. <br><br>
  68. </td>
  69. </tr>
  70. </table>
  71. ## Install
  72. [npm][]:
  73. ```sh
  74. npm install unified
  75. ```
  76. This package comes with types.
  77. If you’re using TypeScript, make sure to also install
  78. [`@types/unist`][ts-unist].
  79. ## Use
  80. ```js
  81. var unified = require('unified')
  82. var markdown = require('remark-parse')
  83. var remark2rehype = require('remark-rehype')
  84. var doc = require('rehype-document')
  85. var format = require('rehype-format')
  86. var html = require('rehype-stringify')
  87. var report = require('vfile-reporter')
  88. unified()
  89. .use(markdown)
  90. .use(remark2rehype)
  91. .use(doc, {title: '👋🌍'})
  92. .use(format)
  93. .use(html)
  94. .process('# Hello world!', function (err, file) {
  95. console.error(report(err || file))
  96. console.log(String(file))
  97. })
  98. ```
  99. Yields:
  100. ```txt
  101. no issues found
  102. ```
  103. ```html
  104. <!doctype html>
  105. <html lang="en">
  106. <head>
  107. <meta charset="utf-8">
  108. <title>👋🌍</title>
  109. <meta name="viewport" content="width=device-width, initial-scale=1">
  110. </head>
  111. <body>
  112. <h1>Hello world!</h1>
  113. </body>
  114. </html>
  115. ```
  116. ## Contents
  117. * [Description](#description)
  118. * [API](#api)
  119. * [`processor()`](#processor)
  120. * [`processor.use(plugin[, options])`](#processoruseplugin-options)
  121. * [`processor.parse(file)`](#processorparsefile)
  122. * [`processor.stringify(node[, file])`](#processorstringifynode-file)
  123. * [`processor.run(node[, file][, done])`](#processorrunnode-file-done)
  124. * [`processor.runSync(node[, file])`](#processorrunsyncnode-file)
  125. * [`processor.process(file[, done])`](#processorprocessfile-done)
  126. * [`processor.processSync(file|value)`](#processorprocesssyncfilevalue)
  127. * [`processor.data([key[, value]])`](#processordatakey-value)
  128. * [`processor.freeze()`](#processorfreeze)
  129. * [`Plugin`](#plugin)
  130. * [`function attacher([options])`](#function-attacheroptions)
  131. * [`function transformer(node, file[, next])`](#function-transformernode-file-next)
  132. * [`Preset`](#preset)
  133. * [Contribute](#contribute)
  134. * [Acknowledgments](#acknowledgments)
  135. * [License](#license)
  136. ## Description
  137. **unified** is an interface for processing text using syntax trees.
  138. Syntax trees are a representation of text understandable to programs.
  139. Those programs, called [*plugin*][plugin]s, take these trees and inspect and
  140. modify them.
  141. To get to the syntax tree from text, there is a [*parser*][parser].
  142. To get from that back to text, there is a [*compiler*][compiler].
  143. This is the [*process*][process] of a *processor*.
  144. ```ascii
  145. | ........................ process ........................... |
  146. | .......... parse ... | ... run ... | ... stringify ..........|
  147. +--------+ +----------+
  148. Input ->- | Parser | ->- Syntax Tree ->- | Compiler | ->- Output
  149. +--------+ | +----------+
  150. X
  151. |
  152. +--------------+
  153. | Transformers |
  154. +--------------+
  155. ```
  156. ###### Processors
  157. Every **processor** implements another processor.
  158. To create a processor, call another processor.
  159. The new processor is configured to work the same as its ancestor.
  160. But when the descendant processor is configured in the future it does not affect
  161. the ancestral processor.
  162. When processors are exposed from a module (for example, `unified` itself) they
  163. should not be configured directly, as that would change their behavior for all
  164. module users.
  165. Those processors are [*frozen*][freeze] and they should be called to create a
  166. new processor before they are used.
  167. ###### Syntax trees
  168. The **syntax trees** used in **unified** are [**unist**][unist] nodes.
  169. A [**node**][node] is a plain JavaScript objects with a `type` field.
  170. The semantics of nodes and format of syntax trees is defined by other projects.
  171. There are several [*utilities*][unist-utilities] for working with nodes.
  172. * [**hast**][hast] — HTML
  173. * [**mdast**][mdast] — Markdown
  174. * [**nlcst**][nlcst] — Natural language
  175. * [**xast**][xast] — XML
  176. ###### List of processors
  177. The following projects process different [*syntax tree*][syntax-tree] formats.
  178. They parse text to a syntax tree and compile that back to text.
  179. These processors can be used as is, or their parser and compiler can be mixed
  180. and matched with **unified** and plugins to process between different syntaxes.
  181. * [**rehype**][rehype] ([*hast*][hast]) — HTML
  182. * [**remark**][remark] ([*mdast*][mdast]) — Markdown
  183. * [**retext**][retext] ([*nlcst*][nlcst]) — Natural language
  184. ###### List of plugins
  185. The below [**plugins**][plugin] work with **unified**, on all [*syntax
  186. tree*][syntax-tree] formats:
  187. * [`unified-diff`](https://github.com/unifiedjs/unified-diff)
  188. — Ignore messages for unchanged lines in Travis
  189. * [`unified-message-control`](https://github.com/unifiedjs/unified-message-control)
  190. — Enable, disable, and ignore messages
  191. See [**remark**][remark-plugins], [**rehype**][rehype-plugins], and
  192. [**retext**][retext-plugins] for their lists of plugins.
  193. ###### File
  194. When processing a document, **metadata** is often gathered about that document.
  195. [**vfile**][vfile] is a virtual file format that stores data, metadata, and
  196. messages about files for **unified** and its plugins.
  197. There are several [*utilities*][vfile-utilities] for working with these files.
  198. ###### Configuration
  199. [*Processors*][processors] are configured with [*plugin*][plugin]s or
  200. with the [`data`][data] method.
  201. ###### Integrations
  202. **unified** can integrate with the file system with [`unified-engine`][engine].
  203. CLI apps can be created with [`unified-args`][args], Gulp plugins with
  204. [`unified-engine-gulp`][gulp], and Atom Linters with
  205. [`unified-engine-atom`][atom].
  206. [`unified-stream`][stream] provides a streaming interface.
  207. ###### Programming interface
  208. The API provided by **unified** allows multiple files to be processed and gives
  209. access to *metadata* (such as lint messages):
  210. ```js
  211. var unified = require('unified')
  212. var markdown = require('remark-parse')
  213. var styleGuide = require('remark-preset-lint-markdown-style-guide')
  214. var remark2retext = require('remark-retext')
  215. var english = require('retext-english')
  216. var equality = require('retext-equality')
  217. var remark2rehype = require('remark-rehype')
  218. var html = require('rehype-stringify')
  219. var report = require('vfile-reporter')
  220. unified()
  221. .use(markdown)
  222. .use(styleGuide)
  223. .use(remark2retext, unified().use(english).use(equality))
  224. .use(remark2rehype)
  225. .use(html)
  226. .process('*Emphasis* and _stress_, you guys!', function (err, file) {
  227. console.error(report(err || file))
  228. console.log(String(file))
  229. })
  230. ```
  231. Yields:
  232. ```txt
  233. 1:16-1:24 warning Emphasis should use `*` as a marker emphasis-marker remark-lint
  234. 1:30-1:34 warning `guys` may be insensitive, use `people`, `persons`, `folks` instead gals-men retext-equality
  235. ⚠ 2 warnings
  236. ```
  237. ```html
  238. <p><em>Emphasis</em> and <em>stress</em>, you guys!</p>
  239. ```
  240. ###### Processing between syntaxes
  241. [*Processors*][processors] can be combined in two modes.
  242. **Bridge** mode transforms the [*syntax tree*][syntax-tree] from one format
  243. (*origin*) to another (*destination*).
  244. Another processor runs on the destination tree.
  245. Finally, the original processor continues transforming the origin tree.
  246. **Mutate** mode also transforms the syntax tree from one format to another.
  247. But the original processor continues transforming the destination tree.
  248. In the previous example (“Programming interface”), `remark-retext` is used in
  249. *bridge* mode: the origin syntax tree is kept after [**retext**][retext] is
  250. done; whereas `remark-rehype` is used in *mutate* mode: it sets a new syntax
  251. tree and discards the origin tree.
  252. * [`remark-retext`][remark-retext]
  253. * [`remark-rehype`][remark-rehype]
  254. * [`rehype-retext`][rehype-retext]
  255. * [`rehype-remark`][rehype-remark]
  256. ## API
  257. ### `processor()`
  258. [*Processor*][processors] describing how to *process* text.
  259. ###### Returns
  260. `Function` — New [*unfrozen*][freeze] processor that is configured to work the
  261. same as its ancestor.
  262. When the descendant processor is configured in the future it does not affect the
  263. ancestral processor.
  264. ###### Example
  265. The following example shows how a new processor can be created (from the remark
  266. processor) and linked to **stdin**(4) and **stdout**(4).
  267. ```js
  268. var remark = require('remark')
  269. var concat = require('concat-stream')
  270. process.stdin.pipe(concat(onconcat))
  271. function onconcat(buf) {
  272. var doc = remark().processSync(buf).toString()
  273. process.stdout.write(doc)
  274. }
  275. ```
  276. ### `processor.use(plugin[, options])`
  277. [*Configure*][configuration] the processor to use a [*plugin*][plugin] and
  278. optionally configure that plugin with options.
  279. If the processor is already using this plugin, the previous plugin configuration
  280. is changed based on the options that are passed in.
  281. The plugin is not added a second time.
  282. ###### Signatures
  283. * `processor.use(plugin[, options])`
  284. * `processor.use(preset)`
  285. * `processor.use(list)`
  286. ###### Parameters
  287. * `plugin` ([`Attacher`][plugin])
  288. * `options` (`*`, optional) — Configuration for `plugin`
  289. * `preset` (`Object`) — Object with an optional `plugins` (set to `list`),
  290. and/or an optional `settings` object
  291. * `list` (`Array`) — List of plugins, presets, and pairs (`plugin` and
  292. `options` in an array)
  293. ###### Returns
  294. `processor` — The processor that `use` was called on.
  295. ###### Note
  296. `use` cannot be called on [*frozen*][freeze] processors.
  297. Call the processor first to create a new unfrozen processor.
  298. ###### Example
  299. There are many ways to pass plugins to `.use()`.
  300. The below example gives an overview.
  301. ```js
  302. var unified = require('unified')
  303. unified()
  304. // Plugin with options:
  305. .use(pluginA, {x: true, y: true})
  306. // Passing the same plugin again merges configuration (to `{x: true, y: false, z: true}`):
  307. .use(pluginA, {y: false, z: true})
  308. // Plugins:
  309. .use([pluginB, pluginC])
  310. // Two plugins, the second with options:
  311. .use([pluginD, [pluginE, {}]])
  312. // Preset with plugins and settings:
  313. .use({plugins: [pluginF, [pluginG, {}]], settings: {position: false}})
  314. // Settings only:
  315. .use({settings: {position: false}})
  316. ```
  317. ### `processor.parse(file)`
  318. Parse text to a [*syntax tree*][syntax-tree].
  319. ###### Parameters
  320. * `file` ([`VFile`][vfile]) — [*File*][file], any value accepted by `vfile()`
  321. ###### Returns
  322. [`Node`][node] — Parsed [*syntax tree*][syntax-tree] representing `file`.
  323. ###### Note
  324. `parse` freezes the processor if not already [*frozen*][freeze].
  325. `parse` performs the [*parse phase*][description], not the *run phase* or other
  326. phases.
  327. ###### Example
  328. The below example shows how `parse` can be used to create a syntax tree from a
  329. file.
  330. ```js
  331. var unified = require('unified')
  332. var markdown = require('remark-parse')
  333. var tree = unified().use(markdown).parse('# Hello world!')
  334. console.log(tree)
  335. ```
  336. Yields:
  337. ```js
  338. {
  339. type: 'root',
  340. children: [
  341. {type: 'heading', depth: 1, children: [Array], position: [Position]}
  342. ],
  343. position: {
  344. start: {line: 1, column: 1, offset: 0},
  345. end: {line: 1, column: 15, offset: 14}
  346. }
  347. }
  348. ```
  349. #### `processor.Parser`
  350. A **parser** handles the parsing of text to a [*syntax tree*][syntax-tree].
  351. Used in the [*parse phase*][description] and called with a `string` and
  352. [`VFile`][vfile] representation of the text to parse.
  353. `Parser` can be a function, in which case it must return a [`Node`][node]: the
  354. syntax tree representation of the given file.
  355. `Parser` can also be a constructor function (a function with a `parse` field, or
  356. other fields, in its `prototype`), in which case it’s constructed with `new`.
  357. Instances must have a `parse` method that is called without arguments and must
  358. return a [`Node`][node].
  359. ### `processor.stringify(node[, file])`
  360. Compile a [*syntax tree*][syntax-tree].
  361. ###### Parameters
  362. * `node` ([`Node`][node]) — [*Syntax tree*][syntax-tree] to compile
  363. * `file` ([`VFile`][vfile], optional) — [*File*][file], any value accepted by
  364. `vfile()`
  365. ###### Returns
  366. `string` or `Buffer` (see notes) — Textual representation of the [*syntax
  367. tree*][syntax-tree]
  368. ###### Note
  369. `stringify` freezes the processor if not already [*frozen*][freeze].
  370. `stringify` performs the [*stringify phase*][description], not the *run phase*
  371. or other phases.
  372. unified typically compiles by serializing: most [*compiler*][compiler]s return
  373. `string` (or `Buffer`).
  374. Some compilers, such as the one configured with [`rehype-react`][rehype-react],
  375. return other values (in this case, a React tree).
  376. If you’re using a compiler doesn’t serialize, expect different result values.
  377. When using TypeScript, cast the type on your side.
  378. ###### Example
  379. The below example shows how `stringify` can be used to serialize a syntax tree.
  380. ```js
  381. var unified = require('unified')
  382. var html = require('rehype-stringify')
  383. var h = require('hastscript')
  384. var tree = h('h1', 'Hello world!')
  385. var doc = unified().use(html).stringify(tree)
  386. console.log(doc)
  387. ```
  388. Yields:
  389. ```html
  390. <h1>Hello world!</h1>
  391. ```
  392. #### `processor.Compiler`
  393. A **compiler** handles the compiling of a [*syntax tree*][syntax-tree] to text.
  394. Used in the [*stringify phase*][description] and called with a [`Node`][node]
  395. and [`VFile`][file] representation of syntax tree to compile.
  396. `Compiler` can be a function, in which case it should return a `string`: the
  397. textual representation of the syntax tree.
  398. `Compiler` can also be a constructor function (a function with a `compile`
  399. field, or other fields, in its `prototype`), in which case it’s constructed with
  400. `new`.
  401. Instances must have a `compile` method that is called without arguments and
  402. should return a `string`.
  403. ### `processor.run(node[, file][, done])`
  404. Run [*transformers*][transformer] on a [*syntax tree*][syntax-tree].
  405. ###### Parameters
  406. * `node` ([`Node`][node]) — [*Syntax tree*][syntax-tree] to run on
  407. * `file` ([`VFile`][vfile], optional) — [*File*][file], any value accepted by
  408. `vfile()`
  409. * `done` ([`Function`][run-done], optional) — Callback
  410. ###### Returns
  411. [`Promise`][promise] if `done` is not given.
  412. The returned promise is rejected with a fatal error, or resolved with the
  413. transformed [*syntax tree*][syntax-tree].
  414. ###### Note
  415. `run` freezes the processor if not already [*frozen*][freeze].
  416. `run` performs the [*run phase*][description], not other phases.
  417. #### `function done(err[, node, file])`
  418. Callback called when [*transformers*][transformer] are done.
  419. Called with either an error or results.
  420. ###### Parameters
  421. * `err` (`Error`, optional) — Fatal error
  422. * `node` ([`Node`][node], optional) — Transformed [*syntax tree*][syntax-tree]
  423. * `file` ([`VFile`][vfile], optional) — [*File*][file]
  424. ###### Example
  425. The below example shows how `run` can be used to transform a syntax tree.
  426. ```js
  427. var unified = require('unified')
  428. var references = require('remark-reference-links')
  429. var u = require('unist-builder')
  430. var tree = u('root', [
  431. u('paragraph', [
  432. u('link', {href: 'https://example.com'}, [u('text', 'Example Domain')])
  433. ])
  434. ])
  435. unified()
  436. .use(references)
  437. .run(tree, function (err, tree) {
  438. if (err) throw err
  439. console.log(tree)
  440. })
  441. ```
  442. Yields:
  443. ```js
  444. {
  445. type: 'root',
  446. children: [
  447. {type: 'paragraph', children: [Array]},
  448. {type: 'definition', identifier: '1', title: undefined, url: undefined}
  449. ]
  450. }
  451. ```
  452. ### `processor.runSync(node[, file])`
  453. Run [*transformers*][transformer] on a [*syntax tree*][syntax-tree].
  454. An error is thrown if asynchronous [*plugin*][plugin]s are configured.
  455. ###### Parameters
  456. * `node` ([`Node`][node]) — [*Syntax tree*][syntax-tree] to run on
  457. * `file` ([`VFile`][vfile], optional) — [*File*][file], any value accepted by
  458. `vfile()`
  459. ###### Returns
  460. [`Node`][node] — Transformed [*syntax tree*][syntax-tree].
  461. ###### Note
  462. `runSync` freezes the processor if not already [*frozen*][freeze].
  463. `runSync` performs the [*run phase*][description], not other phases.
  464. ### `processor.process(file[, done])`
  465. [*Process*][description] the given [*file*][file] as configured on the
  466. processor.
  467. ###### Parameters
  468. * `file` ([`VFile`][vfile]) — [*File*][file], any value accepted by `vfile()`
  469. * `done` ([`Function`][process-done], optional) — Callback
  470. ###### Returns
  471. [`Promise`][promise] if `done` is not given.
  472. The returned promise is rejected with a fatal error, or resolved with the
  473. processed [*file*][file].
  474. The parsed, transformed, and compiled value is exposed on
  475. [`file.contents`][vfile-contents] or `file.result` (see notes).
  476. ###### Note
  477. `process` freezes the processor if not already [*frozen*][freeze].
  478. `process` performs the [*parse*, *run*, and *stringify* phases][description].
  479. unified typically compiles by serializing: most [*compiler*][compiler]s return
  480. `string` (or `Buffer`).
  481. Some compilers, such as the one configured with [`rehype-react`][rehype-react],
  482. return other values (in this case, a React tree).
  483. If you’re using a compiler that serializes, the result is available at
  484. `file.contents`.
  485. Otherwise, the result is available at `file.result`.
  486. ###### Example
  487. The below example shows how `process` can be used to process a file, whether
  488. transformers are asynchronous or not, with promises.
  489. ```js
  490. var unified = require('unified')
  491. var markdown = require('remark-parse')
  492. var remark2rehype = require('remark-rehype')
  493. var doc = require('rehype-document')
  494. var format = require('rehype-format')
  495. var html = require('rehype-stringify')
  496. unified()
  497. .use(markdown)
  498. .use(remark2rehype)
  499. .use(doc, {title: '👋🌍'})
  500. .use(format)
  501. .use(html)
  502. .process('# Hello world!')
  503. .then(
  504. function (file) {
  505. console.log(String(file))
  506. },
  507. function (err) {
  508. console.error(String(err))
  509. }
  510. )
  511. ```
  512. Yields:
  513. ```html
  514. <!doctype html>
  515. <html lang="en">
  516. <head>
  517. <meta charset="utf-8">
  518. <title>👋🌍</title>
  519. <meta name="viewport" content="width=device-width, initial-scale=1">
  520. </head>
  521. <body>
  522. <h1>Hello world!</h1>
  523. </body>
  524. </html>
  525. ```
  526. #### `function done(err, file)`
  527. Callback called when the [*process*][description] is done.
  528. Called with a fatal error, if any, and a [*file*][file].
  529. ###### Parameters
  530. * `err` (`Error`, optional) — Fatal error
  531. * `file` ([`VFile`][vfile]) — Processed [*file*][file]
  532. ###### Example
  533. The below example shows how `process` can be used to process a file, whether
  534. transformers are asynchronous or not, with a callback.
  535. ```js
  536. var unified = require('unified')
  537. var parse = require('remark-parse')
  538. var stringify = require('remark-stringify')
  539. var github = require('remark-github')
  540. var report = require('vfile-reporter')
  541. unified()
  542. .use(parse)
  543. .use(github)
  544. .use(stringify)
  545. .process('@wooorm', function (err, file) {
  546. console.error(report(err || file))
  547. console.log(String(file))
  548. })
  549. ```
  550. Yields:
  551. ```txt
  552. no issues found
  553. ```
  554. ```markdown
  555. [**@wooorm**](https://github.com/wooorm)
  556. ```
  557. ### `processor.processSync(file|value)`
  558. [*Process*][description] the given [*file*][file] as configured on the
  559. processor.
  560. An error is thrown if asynchronous [*plugin*][plugin]s are configured.
  561. ###### Parameters
  562. * `file` ([`VFile`][vfile]) — [*File*][file], any value accepted by `vfile()`
  563. ###### Returns
  564. ([`VFile`][vfile]) — Processed [*file*][file]
  565. The parsed, transformed, and compiled value is exposed on
  566. [`file.contents`][vfile-contents] or `file.result` (see notes).
  567. ###### Note
  568. `processSync` freezes the processor if not already [*frozen*][freeze].
  569. `processSync` performs the [*parse*, *run*, and *stringify*
  570. phases][description].
  571. unified typically compiles by serializing: most [*compiler*][compiler]s return
  572. `string` (or `Buffer`).
  573. Some compilers, such as the one configured with [`rehype-react`][rehype-react],
  574. return other values (in this case, a React tree).
  575. If you’re using a compiler that serializes, the result is available at
  576. `file.contents`.
  577. Otherwise, the result is available at `file.result`.
  578. ###### Example
  579. The below example shows how `processSync` can be used to process a file, if all
  580. transformers are synchronous.
  581. ```js
  582. var unified = require('unified')
  583. var markdown = require('remark-parse')
  584. var remark2rehype = require('remark-rehype')
  585. var doc = require('rehype-document')
  586. var format = require('rehype-format')
  587. var html = require('rehype-stringify')
  588. var processor = unified()
  589. .use(markdown)
  590. .use(remark2rehype)
  591. .use(doc, {title: '👋🌍'})
  592. .use(format)
  593. .use(html)
  594. console.log(processor.processSync('# Hello world!').toString())
  595. ```
  596. Yields:
  597. ```html
  598. <!doctype html>
  599. <html lang="en">
  600. <head>
  601. <meta charset="utf-8">
  602. <title>👋🌍</title>
  603. <meta name="viewport" content="width=device-width, initial-scale=1">
  604. </head>
  605. <body>
  606. <h1>Hello world!</h1>
  607. </body>
  608. </html>
  609. ```
  610. ### `processor.data([key[, value]])`
  611. [*Configure*][configuration] the processor with information available to all
  612. [*plugin*][plugin]s.
  613. Information is stored in an in-memory key-value store.
  614. Typically, options can be given to a specific plugin, but sometimes it makes
  615. sense to have information shared with several plugins.
  616. For example, a list of HTML elements that are self-closing, which is needed
  617. during all [*phases*][description] of the *process*.
  618. ###### Signatures
  619. * `processor = processor.data(key, value)`
  620. * `processor = processor.data(values)`
  621. * `value = processor.data(key)`
  622. * `info = processor.data()`
  623. ###### Parameters
  624. * `key` (`string`, optional) — Identifier
  625. * `value` (`*`, optional) — Value to set
  626. * `values` (`Object`, optional) — Values to set
  627. ###### Returns
  628. * `processor` — If setting, the processor that `data` is called on
  629. * `value` (`*`) — If getting, the value at `key`
  630. * `info` (`Object`) — Without arguments, the key-value store
  631. ###### Note
  632. Setting information cannot occur on [*frozen*][freeze] processors.
  633. Call the processor first to create a new unfrozen processor.
  634. ###### Example
  635. The following example show how to get and set information:
  636. ```js
  637. var unified = require('unified')
  638. var processor = unified().data('alpha', 'bravo')
  639. processor.data('alpha') // => 'bravo'
  640. processor.data() // {alpha: 'bravo'}
  641. processor.data({charlie: 'delta'})
  642. processor.data() // {charlie: 'delta'}
  643. ```
  644. ### `processor.freeze()`
  645. **Freeze** a processor.
  646. *Frozen* processors are meant to be extended and not to be configured directly.
  647. Once a processor is frozen it cannot be *unfrozen*.
  648. New processors working the same way can be created by calling the processor.
  649. It’s possible to freeze processors explicitly by calling `.freeze()`.
  650. Processors freeze implicitly when [`.parse()`][parse], [`.run()`][run],
  651. [`.runSync()`][run-sync], [`.stringify()`][stringify], [`.process()`][process],
  652. or [`.processSync()`][process-sync] are called.
  653. ###### Returns
  654. `processor` — The processor that `freeze` was called on.
  655. ###### Example
  656. The following example, `index.js`, shows how rehype prevents extensions to
  657. itself:
  658. ```js
  659. var unified = require('unified')
  660. var parse = require('rehype-parse')
  661. var stringify = require('rehype-stringify')
  662. module.exports = unified().use(parse).use(stringify).freeze()
  663. ```
  664. The below example, `a.js`, shows how that processor can be used and configured.
  665. ```js
  666. var rehype = require('rehype')
  667. var format = require('rehype-format')
  668. // …
  669. rehype()
  670. .use(format)
  671. // …
  672. ```
  673. The below example, `b.js`, shows a similar looking example that operates on the
  674. frozen rehype interface because it does not call `rehype`.
  675. If this behavior was allowed it would result in unexpected behavior so an
  676. error is thrown.
  677. **This is invalid**:
  678. ```js
  679. var rehype = require('rehype')
  680. var format = require('rehype-format')
  681. // …
  682. rehype
  683. .use(format)
  684. // …
  685. ```
  686. Yields:
  687. ```txt
  688. ~/node_modules/unified/index.js:440
  689. throw new Error(
  690. ^
  691. Error: Cannot invoke `use` on a frozen processor.
  692. Create a new processor first, by invoking it: use `processor()` instead of `processor`.
  693. at assertUnfrozen (~/node_modules/unified/index.js:440:11)
  694. at Function.use (~/node_modules/unified/index.js:172:5)
  695. at Object.<anonymous> (~/b.js:6:4)
  696. ```
  697. ## `Plugin`
  698. **Plugins** [*configure*][configuration] the processors they are applied on in
  699. the following ways:
  700. * They change the processor: such as the [*parser*][parser], the
  701. [*compiler*][compiler], or configuring [*data*][data]
  702. * They specify how to handle [*syntax trees*][syntax-tree] and [*files*][file]
  703. Plugins are a concept.
  704. They materialize as [`attacher`][attacher]s.
  705. ###### Example
  706. `move.js`:
  707. ```js
  708. module.exports = move
  709. function move(options) {
  710. var expected = (options || {}).extname
  711. if (!expected) {
  712. throw new Error('Missing `extname` in options')
  713. }
  714. return transformer
  715. function transformer(tree, file) {
  716. if (file.extname && file.extname !== expected) {
  717. file.extname = expected
  718. }
  719. }
  720. }
  721. ```
  722. `index.md`:
  723. ```markdown
  724. # Hello, world!
  725. ```
  726. `index.js`:
  727. ```js
  728. var unified = require('unified')
  729. var parse = require('remark-parse')
  730. var remark2rehype = require('remark-rehype')
  731. var stringify = require('rehype-stringify')
  732. var vfile = require('to-vfile')
  733. var report = require('vfile-reporter')
  734. var move = require('./move')
  735. unified()
  736. .use(parse)
  737. .use(remark2rehype)
  738. .use(move, {extname: '.html'})
  739. .use(stringify)
  740. .process(vfile.readSync('index.md'), function (err, file) {
  741. console.error(report(err || file))
  742. if (file) {
  743. vfile.writeSync(file) // Written to `index.html`.
  744. }
  745. })
  746. ```
  747. Yields:
  748. ```txt
  749. index.md: no issues found
  750. ```
  751. `index.html`:
  752. ```html
  753. <h1>Hello, world!</h1>
  754. ```
  755. ### `function attacher([options])`
  756. **Attachers** are materialized [*plugin*][plugin]s.
  757. An attacher is a function that can receive options and
  758. [*configures*][configuration] the processor.
  759. Attachers change the processor, such as the [*parser*][parser], the
  760. [*compiler*][compiler], configuring [*data*][data], or by specifying how the
  761. [*syntax tree*][syntax-tree] or [*file*][file] are handled.
  762. ###### Context
  763. The context object (`this`) is set to the processor the attacher is applied on.
  764. ###### Parameters
  765. * `options` (`*`, optional) — Configuration
  766. ###### Returns
  767. [`transformer`][transformer] — Optional.
  768. ###### Note
  769. Attachers are called when the processor is [*frozen*][freeze], not when they are
  770. applied.
  771. ### `function transformer(node, file[, next])`
  772. **Transformers** handle [*syntax tree*][syntax-tree]s and [*file*][file]s.
  773. A transformer is a function that is called each time a syntax tree and file are
  774. passed through the [*run phase*][description].
  775. If an error occurs (either because it’s thrown, returned, rejected, or passed to
  776. [`next`][next]), the process stops.
  777. The *run phase* is handled by [`trough`][trough], see its documentation for the
  778. exact semantics of these functions.
  779. ###### Parameters
  780. * `node` ([`Node`][node]) — [*Syntax tree*][syntax-tree] to handle
  781. * `file` ([`VFile`][vfile]) — [*File*][file] to handle
  782. * `next` ([`Function`][next], optional)
  783. ###### Returns
  784. * `void` — If nothing is returned, the next transformer keeps using same tree.
  785. * `Error` — Fatal error to stop the process
  786. * `node` ([`Node`][node]) — New [*syntax tree*][syntax-tree].
  787. If returned, the next transformer is given this new tree
  788. * `Promise` — Returned to perform an asynchronous operation.
  789. The promise **must** be resolved (optionally with a [`Node`][node]) or
  790. rejected (optionally with an `Error`)
  791. #### `function next(err[, tree[, file]])`
  792. If the signature of a [*transformer*][transformer] includes `next` (the third
  793. argument), the transformer **may** perform asynchronous operations, and **must**
  794. call `next()`.
  795. ###### Parameters
  796. * `err` (`Error`, optional) — Fatal error to stop the process
  797. * `node` ([`Node`][node], optional) — New [*syntax tree*][syntax-tree].
  798. If given, the next transformer is given this new tree
  799. * `file` ([`VFile`][vfile], optional) — New [*file*][file].
  800. If given, the next transformer is given this new file
  801. ## `Preset`
  802. **Presets** are sharable [*configuration*][configuration].
  803. They can contain [*plugins*][plugin] and settings.
  804. ###### Example
  805. `preset.js`:
  806. ```js
  807. exports.settings = {bullet: '*', emphasis: '*', fences: true}
  808. exports.plugins = [
  809. require('remark-preset-lint-recommended'),
  810. require('remark-preset-lint-consistent'),
  811. require('remark-comment-config'),
  812. [require('remark-toc'), {maxDepth: 3, tight: true}],
  813. require('remark-license')
  814. ]
  815. ```
  816. `readme.md`:
  817. ```markdown
  818. # Hello, world!
  819. _Emphasis_ and **importance**.
  820. ## Table of contents
  821. ## API
  822. ## License
  823. ```
  824. `index.js`:
  825. ```js
  826. var remark = require('remark')
  827. var vfile = require('to-vfile')
  828. var report = require('vfile-reporter')
  829. var preset = require('./preset')
  830. remark()
  831. .use(preset)
  832. .process(vfile.readSync('readme.md'), function (err, file) {
  833. console.error(report(err || file))
  834. if (file) {
  835. vfile.writeSync(file)
  836. }
  837. })
  838. ```
  839. Yields:
  840. ```txt
  841. readme.md: no issues found
  842. ```
  843. `readme.md` now contains:
  844. ```markdown
  845. # Hello, world!
  846. *Emphasis* and **importance**.
  847. ## Table of contents
  848. * [API](#api)
  849. * [License](#license)
  850. ## API
  851. ## License
  852. [MIT](license) © [Titus Wormer](https://wooorm.com)
  853. ```
  854. ## Contribute
  855. See [`contributing.md`][contributing] in [`unifiedjs/.github`][health] for ways
  856. to get started.
  857. See [`support.md`][support] for ways to get help.
  858. Ideas for new plugins and tools can be posted in [`unifiedjs/ideas`][ideas].
  859. A curated list of awesome unified resources can be found in [**awesome
  860. unified**][awesome].
  861. This project has a [code of conduct][coc].
  862. By interacting with this repository, organization, or community you agree to
  863. abide by its terms.
  864. ## Acknowledgments
  865. Preliminary work for unified was done [in 2014][preliminary] for
  866. [**retext**][retext] and inspired by [`ware`][ware].
  867. Further incubation happened in [**remark**][remark].
  868. The project was finally [externalised][] in 2015 and [published][] as `unified`.
  869. The project was authored by [**@wooorm**](https://github.com/wooorm).
  870. Although `unified` since moved its plugin architecture to [`trough`][trough],
  871. thanks to [**@calvinfo**](https://github.com/calvinfo),
  872. [**@ianstormtaylor**](https://github.com/ianstormtaylor), and others for their
  873. work on [`ware`][ware], as it was a huge initial inspiration.
  874. ## License
  875. [MIT][license] © [Titus Wormer][author]
  876. <!-- Definitions -->
  877. [logo]: https://raw.githubusercontent.com/unifiedjs/unified/93862e5/logo.svg?sanitize=true
  878. [build-badge]: https://github.com/unifiedjs/unified/workflows/main/badge.svg
  879. [build]: https://github.com/unifiedjs/unified/actions
  880. [coverage-badge]: https://img.shields.io/codecov/c/github/unifiedjs/unified.svg
  881. [coverage]: https://codecov.io/github/unifiedjs/unified
  882. [downloads-badge]: https://img.shields.io/npm/dm/unified.svg
  883. [downloads]: https://www.npmjs.com/package/unified
  884. [size-badge]: https://img.shields.io/bundlephobia/minzip/unified.svg
  885. [size]: https://bundlephobia.com/result?p=unified
  886. [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
  887. [backers-badge]: https://opencollective.com/unified/backers/badge.svg
  888. [collective]: https://opencollective.com/unified
  889. [chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
  890. [chat]: https://github.com/unifiedjs/unified/discussions
  891. [health]: https://github.com/unifiedjs/.github
  892. [contributing]: https://github.com/unifiedjs/.github/blob/HEAD/contributing.md
  893. [support]: https://github.com/unifiedjs/.github/blob/HEAD/support.md
  894. [coc]: https://github.com/unifiedjs/.github/blob/HEAD/code-of-conduct.md
  895. [awesome]: https://github.com/unifiedjs/awesome-unified
  896. [license]: license
  897. [author]: https://wooorm.com
  898. [npm]: https://docs.npmjs.com/cli/install
  899. [ts-unist]: https://www.npmjs.com/package/@types/unist
  900. [site]: https://unifiedjs.com
  901. [twitter]: https://twitter.com/unifiedjs
  902. [learn]: https://unifiedjs.com/learn/
  903. [rehype]: https://github.com/rehypejs/rehype
  904. [remark]: https://github.com/remarkjs/remark
  905. [retext]: https://github.com/retextjs/retext
  906. [hast]: https://github.com/syntax-tree/hast
  907. [mdast]: https://github.com/syntax-tree/mdast
  908. [nlcst]: https://github.com/syntax-tree/nlcst
  909. [xast]: https://github.com/syntax-tree/xast
  910. [unist]: https://github.com/syntax-tree/unist
  911. [engine]: https://github.com/unifiedjs/unified-engine
  912. [args]: https://github.com/unifiedjs/unified-args
  913. [gulp]: https://github.com/unifiedjs/unified-engine-gulp
  914. [atom]: https://github.com/unifiedjs/unified-engine-atom
  915. [remark-rehype]: https://github.com/remarkjs/remark-rehype
  916. [remark-retext]: https://github.com/remarkjs/remark-retext
  917. [rehype-retext]: https://github.com/rehypejs/rehype-retext
  918. [rehype-remark]: https://github.com/rehypejs/rehype-remark
  919. [unist-utilities]: https://github.com/syntax-tree/unist#list-of-utilities
  920. [vfile]: https://github.com/vfile/vfile
  921. [vfile-contents]: https://github.com/vfile/vfile#vfilecontents
  922. [vfile-utilities]: https://github.com/vfile/vfile#related-tools
  923. [node]: https://github.com/syntax-tree/unist#node
  924. [description]: #description
  925. [syntax-tree]: #syntax-trees
  926. [configuration]: #configuration
  927. [file]: #file
  928. [processors]: #processors
  929. [process]: #processorprocessfile-done
  930. [process-sync]: #processorprocesssyncfilevalue
  931. [parse]: #processorparsefile
  932. [parser]: #processorparser
  933. [stringify]: #processorstringifynode-file
  934. [run]: #processorrunnode-file-done
  935. [run-sync]: #processorrunsyncnode-file
  936. [compiler]: #processorcompiler
  937. [data]: #processordatakey-value
  938. [attacher]: #function-attacheroptions
  939. [transformer]: #function-transformernode-file-next
  940. [next]: #function-nexterr-tree-file
  941. [freeze]: #processorfreeze
  942. [plugin]: #plugin
  943. [run-done]: #function-doneerr-node-file
  944. [process-done]: #function-doneerr-file
  945. [contribute]: #contribute
  946. [rehype-react]: https://github.com/rhysd/rehype-react
  947. [trough]: https://github.com/wooorm/trough#function-fninput-next
  948. [promise]: https://developer.mozilla.org/Web/JavaScript/Reference/Global_Objects/Promise
  949. [remark-plugins]: https://github.com/remarkjs/remark/blob/HEAD/doc/plugins.md#list-of-plugins
  950. [rehype-plugins]: https://github.com/rehypejs/rehype/blob/HEAD/doc/plugins.md#list-of-plugins
  951. [retext-plugins]: https://github.com/retextjs/retext/blob/HEAD/doc/plugins.md#list-of-plugins
  952. [stream]: https://github.com/unifiedjs/unified-stream
  953. [ideas]: https://github.com/unifiedjs/ideas
  954. [preliminary]: https://github.com/retextjs/retext/commit/8fcb1f#diff-168726dbe96b3ce427e7fedce31bb0bc
  955. [externalised]: https://github.com/remarkjs/remark/commit/9892ec#diff-168726dbe96b3ce427e7fedce31bb0bc
  956. [published]: https://github.com/unifiedjs/unified/commit/2ba1cf
  957. [ware]: https://github.com/segmentio/ware
  958. [gatsby]: https://www.gatsbyjs.org
  959. [mdx]: https://mdxjs.com
  960. [jsx]: https://reactjs.org/docs/jsx-in-depth.html
  961. [prettier]: https://prettier.io
  962. [node.js]: https://nodejs.org
  963. [vercel]: https://vercel.com
  964. [netlify]: https://www.netlify.com
  965. [github]: https://github.com
  966. [mozilla]: https://www.mozilla.org
  967. [wordpress]: https://wordpress.com
  968. [adobe]: https://www.adobe.com
  969. [facebook]: https://www.facebook.com
  970. [google]: https://www.google.com