Skip to content

Commit 1ed409a

Browse files
committed
Refactor docs
1 parent 64ae5d6 commit 1ed409a

File tree

1 file changed

+52
-72
lines changed

1 file changed

+52
-72
lines changed

readme.md

Lines changed: 52 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,89 @@
1-
# unist-util-modify-children [![Build Status](https://img.shields.io/travis/wooorm/unist-util-modify-children.svg)](https://travis-ci.org/wooorm/unist-util-modify-children) [![Coverage Status](https://img.shields.io/codecov/c/github/wooorm/unist-util-modify-children.svg)](https://codecov.io/github/wooorm/unist-util-modify-children)
1+
# unist-util-modify-children [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov]
22

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.
74

85
## Installation
96

10-
[npm](https://docs.npmjs.com/cli/install):
7+
[npm][]:
118

129
```bash
1310
npm install unist-util-modify-children
1411
```
1512

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-
2113
## Usage
2214

23-
```js
15+
```javascript
16+
var remark = require('remark');
2417
var modifyChildren = require('unist-util-modify-children');
2518

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
6142
```
6243

6344
## API
6445

65-
### modifyChildren(fn)
46+
### `modify = modifyChildren(modifier)`
6647

67-
**Parameters**
48+
Wrap [`modifier`][modifier] to be invoked for each child in the node given to
49+
[`modify`][modify].
6850

69-
* `fn` ([`Function`](#function-fnchild-index-parent))
70-
— Function to wrap.
51+
#### `next? = modifier(child, index, parent)`
7152

72-
**Return**
53+
Invoked if [`modify`][modify] is called on a parent node for each `child`
54+
in `parent`.
7355

74-
[`Function`](#function-pluginparent) — Wrapped `fn`.
56+
###### Returns
7557

76-
#### function fn(child, index, parent)
58+
`number` (optional) — Next position to iterate.
7759

78-
Modifier for children of `parent`.
60+
#### `function modify(parent)`
7961

80-
**Parameters**
62+
Invoke the bound [`modifier`][modifier] for each child in `parent`
63+
([`Node`][node]).
8164

82-
* `child` ([`Node`](https://github.com/wooorm/unist##unist-nodes))
83-
— Current iteration;
65+
## License
8466

85-
* `index` (`number`) — Position of `child` in `parent`;
67+
[MIT][license] © [Titus Wormer][author]
8668

87-
* `parent` ([`Node`](https://github.com/wooorm/unist##unist-nodes))
88-
— Parent node of `child`.
69+
<!-- Definitions -->
8970

90-
**Returns**
71+
[travis-badge]: https://img.shields.io/travis/wooorm/unist-util-modify-children.svg
9172

92-
`number` (optional) — Next position to iterate.
73+
[travis]: https://travis-ci.org/wooorm/unist-util-modify-children
9374

94-
#### function plugin(parent)
75+
[codecov-badge]: https://img.shields.io/codecov/c/github/wooorm/unist-util-modify-children.svg
9576

96-
Function invoking `fn` for each child of `parent`.
77+
[codecov]: https://codecov.io/github/wooorm/unist-util-modify-children
9778

98-
**Parameters**
79+
[npm]: https://docs.npmjs.com/cli/install
9980

100-
* `parent` ([`Node`](https://github.com/wooorm/unist##unist-nodes))
101-
— Node with children.
81+
[license]: LICENSE
10282

103-
**Throws**
83+
[author]: http://wooorm.com
10484

105-
* `Error` — When not given a parent node.
85+
[node]: https://github.com/wooorm/unist#node
10686

107-
## License
87+
[modifier]: #next--modifierchild-index-parent
10888

109-
[MIT](LICENSE) © [Titus Wormer](http://wooorm.com)
89+
[modify]: #function-modifyparent

0 commit comments

Comments
 (0)