1 | # unist-util-visit [![Build Status](https://img.shields.io/travis/wooorm/unist-util-visit.svg)](https://travis-ci.org/wooorm/unist-util-visit) [![Coverage Status](https://img.shields.io/codecov/c/github/wooorm/unist-util-visit.svg)](https://codecov.io/github/wooorm/unist-util-visit?branch=master)
|
2 |
|
3 | [**Unist**](https://github.com/wooorm/unist) node visitor. Useful when working
|
4 | with [**mdast**](https://github.com/wooorm/mdast) or
|
5 | [**retext**](https://github.com/wooorm/retext).
|
6 |
|
7 | ## Installation
|
8 |
|
9 | [npm](https://docs.npmjs.com/cli/install):
|
10 |
|
11 | ```bash
|
12 | npm install unist-util-visit
|
13 | ```
|
14 |
|
15 | **unist-util-visit** is also available for [bower](http://bower.io/#install-packages),
|
16 | [component](https://github.com/componentjs/component), and
|
17 | [duo](http://duojs.org/#getting-started), and as an AMD, CommonJS, and globals
|
18 | module, [uncompressed](unist-util-visit.js) and
|
19 | [compressed](unist-util-visit.min.js).
|
20 |
|
21 | ## Usage
|
22 |
|
23 | ```js
|
24 | var mdast = require('mdast');
|
25 | var visit = require('unist-util-visit');
|
26 |
|
27 | mdast().use(function (ast) {
|
28 | visit(ast, 'text', console.log.bind(console));
|
29 | }).process('Some *emphasis*, **strongness**, and `code`.');
|
30 | ```
|
31 |
|
32 | Yields:
|
33 |
|
34 | ```js
|
35 | {'type': 'text', 'value': 'Some '}
|
36 | {'type': 'text', 'value': 'emphasis'}
|
37 | {'type': 'text', 'value': ', '}
|
38 | {'type': 'text', 'value': 'strongness'}
|
39 | {'type': 'text', 'value': ', and '}
|
40 | {'type': 'text', 'value': '.'}
|
41 | ```
|
42 |
|
43 | ## API
|
44 |
|
45 | ### visit([node](https://github.com/wooorm/unist#unist-nodes)\[, type\], callback\[, reverse\])
|
46 |
|
47 | > `visit` is synchronous.
|
48 |
|
49 | Visit nodes. Optionally by node type. Optionally in reverse.
|
50 |
|
51 | * `node` (`Node`)
|
52 | — [**Unist** node](https://github.com/wooorm/unist#unist-nodes);
|
53 |
|
54 | * `type` (`string`, optional)
|
55 | — Optional node type to invoke `callback` for. By default, all nodes are
|
56 | visited.
|
57 |
|
58 | * `callback` (`function(node, index?, parent?)`)
|
59 | — Callback invoked when a node (matching `type`?) is found. Invoked with
|
60 | the node, its `index` in `parent` (or `null`), and its `parent` (or `null`).
|
61 |
|
62 | Can return `false` to immediately stop checking.
|
63 |
|
64 | * `reverse` (`boolean`, default: `false`, optional)
|
65 | — When falsey, checking starts at the first child and continues through
|
66 | to later children. When truthy, this is reversed.
|
67 |
|
68 | This **does not** mean checking starts at the deepest node and continues
|
69 | on to the highest node.
|
70 |
|
71 | ## License
|
72 |
|
73 | [MIT](LICENSE) © [Titus Wormer](http://wooorm.com)
|