|
1 |
| -# unist-util-find-after [](https://travis-ci.org/wooorm/unist-util-find-after) [](https://codecov.io/github/wooorm/unist-util-find-after?branch=master) |
| 1 | +# unist-util-find-after [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] |
2 | 2 |
|
3 |
| -[**Unist**](https://github.com/wooorm/unist) utility to find a node after |
4 |
| -another node. Useful when working with [**mdast**](https://github.com/wooorm/mdast) |
5 |
| -or [**retext**](https://github.com/wooorm/retext). |
| 3 | +[**Unist**][unist] utility to find a node after another node. |
6 | 4 |
|
7 | 5 | ## Installation
|
8 | 6 |
|
9 |
| -[npm](https://docs.npmjs.com/cli/install): |
| 7 | +[npm][]: |
10 | 8 |
|
11 | 9 | ```bash
|
12 | 10 | npm install unist-util-find-after
|
13 | 11 | ```
|
14 | 12 |
|
15 |
| -**unist-util-find-after** 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-find-after.js) and |
19 |
| -[compressed](unist-util-find-after.min.js). |
20 |
| - |
21 | 13 | ## Usage
|
22 | 14 |
|
23 | 15 | ```js
|
24 |
| -var mdast = require('mdast'); |
| 16 | +var remark = require('remark'); |
25 | 17 | var findAfter = require('unist-util-find-after');
|
26 |
| -var inspect = require('unist-util-inspect'); |
27 |
| - |
28 |
| -function log(node) { |
29 |
| - console.log(node && inspect(node)); |
30 |
| -} |
31 |
| - |
32 |
| -mdast.use(function () { |
33 |
| - return function (ast) { |
34 |
| - var paragraph = ast.children[0]; |
35 |
| - var children = paragraph.children; |
36 |
| - |
37 |
| - log(findAfter(paragraph, 1)); |
38 |
| - log(findAfter(paragraph, children[1])); |
39 |
| - log(findAfter(paragraph, children[1], 'strong')); |
40 |
| - log(findAfter(paragraph, children[1], children[0])); |
41 |
| - log(findAfter(paragraph, children[1], function (node, n) { |
42 |
| - return n === 5; |
43 |
| - })); |
44 |
| - }; |
45 |
| -}).process('Some *emphasis*, **strongness**, and `code`.'); |
| 18 | + |
| 19 | +var tree = remark().parse('Some _emphasis_, **importance**, and `code`.'); |
| 20 | +var paragraph = tree.children[0]; |
| 21 | + |
| 22 | +console.log(findAfter(paragraph, 1, 'strong')); |
46 | 23 | ```
|
47 | 24 |
|
48 | 25 | Yields:
|
49 | 26 |
|
50 |
| -```text |
51 |
| -text: ', ' |
52 |
| -text: ', ' |
53 |
| -strong[1] |
54 |
| -└─ text: 'strongness' |
55 |
| -null |
56 |
| -inlineCode: 'code' |
| 27 | +```js |
| 28 | +{ type: 'strong', |
| 29 | + children: [ { type: 'text', value: 'importance' } ] } |
57 | 30 | ```
|
58 | 31 |
|
59 | 32 | ## API
|
60 | 33 |
|
61 |
| -### findAfter(parent, index|node\[, test]) |
| 34 | +### `findAfter(parent, node|index[, test])` |
62 | 35 |
|
63 |
| -Find the first child after `index` (or `node`), that passes `test` (when |
64 |
| -given). |
| 36 | +Find the first child after `index` (or `node`) in `parent`, that passes `test` |
| 37 | +(when given). |
65 | 38 |
|
66 |
| -**Parameters**: |
| 39 | +###### Parameters |
67 | 40 |
|
68 |
| -* `parent` (`Node`) — Parent to search in; |
| 41 | +* `parent` ([`Node`][node]) — Context node; |
| 42 | +* `node` ([`Node`][node]) — Node in `parent`; |
| 43 | +* `index` (`number`, optional) — Position of a `node` in `parent`; |
| 44 | +* `test` (`Function`, `string`, or `Node`, optional) |
| 45 | + — See [`unist-util-is`][is]. |
69 | 46 |
|
70 |
| -* `node` (`Node`) |
71 |
| - — [Node](https://github.com/wooorm/unist#unist-nodes) to search after; |
| 47 | +###### Returns |
72 | 48 |
|
73 |
| -* `index` (`number`) — Position of child to search after; |
| 49 | +[`Node?`][node] — Child node of `parent` passing `test`. |
74 | 50 |
|
75 |
| -* `test` (`Function`, `string`, or `Node`; optional) |
76 |
| - — See [`is()`](https://github.com/wooorm/unist-util-is#istest-node-index-parent-context). |
| 51 | +## License |
77 | 52 |
|
78 |
| -**Returns**: `node?`, when found. Child node of `parent` which passes `test`. |
| 53 | +[MIT][license] © [Titus Wormer][author] |
79 | 54 |
|
80 |
| -## License |
| 55 | +<!-- Definitions --> |
| 56 | + |
| 57 | +[travis-badge]: https://img.shields.io/travis/wooorm/unist-util-find-after.svg |
| 58 | + |
| 59 | +[travis]: https://travis-ci.org/wooorm/unist-util-find-after |
| 60 | + |
| 61 | +[codecov-badge]: https://img.shields.io/codecov/c/github/wooorm/unist-util-find-after.svg |
| 62 | + |
| 63 | +[codecov]: https://codecov.io/github/wooorm/unist-util-find-after |
| 64 | + |
| 65 | +[npm]: https://docs.npmjs.com/cli/install |
| 66 | + |
| 67 | +[license]: LICENSE |
| 68 | + |
| 69 | +[author]: http://wooorm.com |
| 70 | + |
| 71 | +[unist]: https://github.com/wooorm/unist |
| 72 | + |
| 73 | +[node]: https://github.com/wooorm/unist#node |
81 | 74 |
|
82 |
| -[MIT](LICENSE) © [Titus Wormer](http://wooorm.com) |
| 75 | +[is]: https://github.com/wooorm/unist-util-is |
0 commit comments