1 | # unist-util-find [![Travis](https://img.shields.io/travis/blahah/unist-util-find.svg)](https://travis-ci.org/blahah/unist-util-find)
|
2 |
|
3 | [Unist](https://github.com/wooorm/unist) node finder utility. Useful for working with [remark](https://github.com/wooorm/remark), [rehype](https://github.com/wooorm/rehype) and [retext](https://github.com/wooorm/retext).
|
4 |
|
5 | ## Installation
|
6 |
|
7 | ```
|
8 | npm install --save unist-util-find
|
9 | ```
|
10 |
|
11 | ## Usage
|
12 |
|
13 | ### Example
|
14 |
|
15 | ```js
|
16 | var remark = require('remark')
|
17 | var find = require('unist-util-find')
|
18 |
|
19 | remark()
|
20 | .use(function () {
|
21 | return function (tree) {
|
22 | // string condition
|
23 | console.log(find(tree, 'value'))
|
24 |
|
25 | // object condition
|
26 | console.log(find(tree, { value: 'emphasis' }))
|
27 |
|
28 | // function condition
|
29 | console.log(find(tree, function (node) {
|
30 | return node.type === 'inlineCode'
|
31 | }))
|
32 | }
|
33 | })
|
34 | .processSync('Some _emphasis_, **strongness**, and `code`.')
|
35 |
|
36 | ```
|
37 |
|
38 | Result:
|
39 |
|
40 | ```
|
41 | // string condition: 'value'
|
42 | { type: 'text',
|
43 | value: 'Some ',
|
44 | position:
|
45 | Position {
|
46 | start: { line: 1, column: 1, offset: 0 },
|
47 | end: { line: 1, column: 6, offset: 5 },
|
48 | indent: [] } }
|
49 |
|
50 | // object condition: { value: 'emphasis' }
|
51 | { type: 'text',
|
52 | value: 'emphasis',
|
53 | position:
|
54 | Position {
|
55 | start: { line: 1, column: 7, offset: 6 },
|
56 | end: { line: 1, column: 15, offset: 14 },
|
57 | indent: [] } }
|
58 |
|
59 | // function condition: function (node) { return node.type === 'inlineCode' }
|
60 | { type: 'inlineCode',
|
61 | value: 'code',
|
62 | position:
|
63 | Position {
|
64 | start: { line: 1, column: 38, offset: 37 },
|
65 | end: { line: 1, column: 44, offset: 43 },
|
66 | indent: [] } }
|
67 | ```
|
68 |
|
69 | ### API
|
70 |
|
71 | #### `find(node, condition)`
|
72 |
|
73 | Return the first node that matches `condition`, or `undefined` if no node matches.
|
74 |
|
75 | - `node` ([`Node`](https://github.com/wooorm/unist#node)) - Node to search
|
76 | - `condition` (`string`, `object` or `function`) - Condition used to test each node. Behaviour depends on the type of the condition:
|
77 | - `string` finds first node with a truthy property matching `string`
|
78 | - `object` finds first node that has matching values for all properties of `object`
|
79 | - `function` finds first node for which `function` returns true when passed `node` as argument
|
80 |
|
81 | ## License
|
82 |
|
83 | MIT
|