|
1 |
| -# unist-util-modify-children [](https://travis-ci.org/wooorm/unist-util-modify-children) [](https://codecov.io/github/wooorm/unist-util-modify-children) |
| 1 | +# unist-util-modify-children [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] |
2 | 2 |
|
3 |
| -[Unist](https://github.com/wooorm/unist) ([mdast](https://github.com/wooorm/mdast/blob/master/doc/mdastnode.7.md), |
4 |
| -[retext](https://github.com/wooorm/retext)) utility to modify direct children of |
5 |
| -a parent. As in, wrap `fn` so it accepts a parent and invoke `fn` for each of |
6 |
| -its children. When `fn` returns a number, goes to that child next. |
| 3 | +Modify direct children of a parent. |
7 | 4 |
|
8 | 5 | ## Installation
|
9 | 6 |
|
10 |
| -[npm](https://docs.npmjs.com/cli/install): |
| 7 | +[npm][]: |
11 | 8 |
|
12 | 9 | ```bash
|
13 | 10 | npm install unist-util-modify-children
|
14 | 11 | ```
|
15 | 12 |
|
16 |
| -**unist-util-modify-children** is also available for [bower](http://bower.io/#install-packages), |
17 |
| -[component](https://github.com/componentjs/component), and |
18 |
| -[duo](http://duojs.org/#getting-started), and as an AMD, CommonJS, and globals |
19 |
| -module, [uncompressed](unist-util-modify-children.js) and [compressed](unist-util-modify-children.min.js). |
20 |
| - |
21 | 13 | ## Usage
|
22 | 14 |
|
23 |
| -```js |
| 15 | +```javascript |
| 16 | +var remark = require('remark'); |
24 | 17 | var modifyChildren = require('unist-util-modify-children');
|
25 | 18 |
|
26 |
| -var modifier = modifyChildren(function (child, index, parent) { |
27 |
| - console.log(child, index); |
28 |
| - |
29 |
| - if (child.value === 'bravo') { |
30 |
| - parent.children.splice(index + 1, 0, { 'type': 'bar', 'value': 'delta' }); |
31 |
| - return index + 1; |
32 |
| - } |
33 |
| -}); |
34 |
| - |
35 |
| -var parent = { |
36 |
| - 'type': 'foo', |
37 |
| - 'children': [ |
38 |
| - { 'type': 'bar', 'value': 'alpha' }, |
39 |
| - { 'type': 'bar', 'value': 'bravo' }, |
40 |
| - { 'type': 'bar', 'value': 'charlie' } |
41 |
| - ] |
42 |
| -}; |
43 |
| - |
44 |
| -modifier(parent); |
45 |
| -/* |
46 |
| - * { type: 'bar', value: 'alpha' } 0 |
47 |
| - * { type: 'bar', value: 'bravo' } 1 |
48 |
| - * { type: 'bar', value: 'delta' } 2 |
49 |
| - * { type: 'bar', value: 'charlie' } 3 |
50 |
| - */ |
51 |
| - |
52 |
| -console.log(parent); |
53 |
| -/* |
54 |
| - * { type: 'foo', |
55 |
| - * children: |
56 |
| - * [ { type: 'bar', value: 'alpha' }, |
57 |
| - * { type: 'bar', value: 'bravo' }, |
58 |
| - * { type: 'bar', value: 'delta' }, |
59 |
| - * { type: 'bar', value: 'charlie' } ] } |
60 |
| - */ |
| 19 | +var doc = remark().use(plugin).process('This _and_ that'); |
| 20 | + |
| 21 | +console.log(String(doc)); |
| 22 | + |
| 23 | +function plugin() { |
| 24 | + return transformer; |
| 25 | + function transformer(tree) { |
| 26 | + modifyChildren(modifier)(tree.children[0]); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function modifier(node, index, parent) { |
| 31 | + if (node.type === 'emphasis') { |
| 32 | + parent.children.splice(index, 1, {type: 'strong', children: node.children}); |
| 33 | + return index + 1; |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Yields: |
| 39 | + |
| 40 | +```js |
| 41 | +This **and** that |
61 | 42 | ```
|
62 | 43 |
|
63 | 44 | ## API
|
64 | 45 |
|
65 |
| -### modifyChildren(fn) |
| 46 | +### `modify = modifyChildren(modifier)` |
66 | 47 |
|
67 |
| -**Parameters** |
| 48 | +Wrap [`modifier`][modifier] to be invoked for each child in the node given to |
| 49 | +[`modify`][modify]. |
68 | 50 |
|
69 |
| -* `fn` ([`Function`](#function-fnchild-index-parent)) |
70 |
| - — Function to wrap. |
| 51 | +#### `next? = modifier(child, index, parent)` |
71 | 52 |
|
72 |
| -**Return** |
| 53 | +Invoked if [`modify`][modify] is called on a parent node for each `child` |
| 54 | +in `parent`. |
73 | 55 |
|
74 |
| -[`Function`](#function-pluginparent) — Wrapped `fn`. |
| 56 | +###### Returns |
75 | 57 |
|
76 |
| -#### function fn(child, index, parent) |
| 58 | +`number` (optional) — Next position to iterate. |
77 | 59 |
|
78 |
| -Modifier for children of `parent`. |
| 60 | +#### `function modify(parent)` |
79 | 61 |
|
80 |
| -**Parameters** |
| 62 | +Invoke the bound [`modifier`][modifier] for each child in `parent` |
| 63 | +([`Node`][node]). |
81 | 64 |
|
82 |
| -* `child` ([`Node`](https://github.com/wooorm/unist##unist-nodes)) |
83 |
| - — Current iteration; |
| 65 | +## License |
84 | 66 |
|
85 |
| -* `index` (`number`) — Position of `child` in `parent`; |
| 67 | +[MIT][license] © [Titus Wormer][author] |
86 | 68 |
|
87 |
| -* `parent` ([`Node`](https://github.com/wooorm/unist##unist-nodes)) |
88 |
| - — Parent node of `child`. |
| 69 | +<!-- Definitions --> |
89 | 70 |
|
90 |
| -**Returns** |
| 71 | +[travis-badge]: https://img.shields.io/travis/wooorm/unist-util-modify-children.svg |
91 | 72 |
|
92 |
| -`number` (optional) — Next position to iterate. |
| 73 | +[travis]: https://travis-ci.org/wooorm/unist-util-modify-children |
93 | 74 |
|
94 |
| -#### function plugin(parent) |
| 75 | +[codecov-badge]: https://img.shields.io/codecov/c/github/wooorm/unist-util-modify-children.svg |
95 | 76 |
|
96 |
| -Function invoking `fn` for each child of `parent`. |
| 77 | +[codecov]: https://codecov.io/github/wooorm/unist-util-modify-children |
97 | 78 |
|
98 |
| -**Parameters** |
| 79 | +[npm]: https://docs.npmjs.com/cli/install |
99 | 80 |
|
100 |
| -* `parent` ([`Node`](https://github.com/wooorm/unist##unist-nodes)) |
101 |
| - — Node with children. |
| 81 | +[license]: LICENSE |
102 | 82 |
|
103 |
| -**Throws** |
| 83 | +[author]: http://wooorm.com |
104 | 84 |
|
105 |
| -* `Error` — When not given a parent node. |
| 85 | +[node]: https://github.com/wooorm/unist#node |
106 | 86 |
|
107 |
| -## License |
| 87 | +[modifier]: #next--modifierchild-index-parent |
108 | 88 |
|
109 |
| -[MIT](LICENSE) © [Titus Wormer](http://wooorm.com) |
| 89 | +[modify]: #function-modifyparent |
0 commit comments