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