10
10
11
11
Modify direct children of a parent.
12
12
13
- ## Installation
13
+ ## Install
14
14
15
15
[ npm] [ ] :
16
16
17
- ``` bash
17
+ ``` sh
18
18
npm install unist-util-modify-children
19
19
```
20
20
21
21
## Usage
22
22
23
- ``` javascript
24
- var remark = require (' remark ' )
23
+ ``` js
24
+ var u = require (' unist-builder ' )
25
25
var modifyChildren = require (' unist-util-modify-children' )
26
26
27
- var doc = remark ()
28
- .use (plugin)
29
- .process (' This _and_ that' )
27
+ var modify = modifyChildren (modifier)
30
28
31
- console .log (String (doc))
29
+ var tree = u (' root' , [
30
+ u (' leaf' , ' 1' ),
31
+ u (' node' , [u (' leaf' , ' 2' )]),
32
+ u (' leaf' , ' 3' )
33
+ ])
32
34
33
- function plugin () {
34
- return transformer
35
- function transformer (tree ) {
36
- modifyChildren (modifier)(tree .children [0 ])
37
- }
38
- }
35
+ modify (tree)
36
+
37
+ console .dir (tree, {depth: null })
39
38
40
39
function modifier (node , index , parent ) {
41
- if (node .type === ' emphasis ' ) {
42
- parent .children .splice (index, 1 , {type: ' strong ' , children: node .children })
40
+ if (node .type === ' node ' ) {
41
+ parent .children .splice (index, 1 , {type: ' subtree ' , children: node .children })
43
42
return index + 1
44
43
}
45
44
}
@@ -48,7 +47,14 @@ function modifier(node, index, parent) {
48
47
Yields:
49
48
50
49
``` js
51
- This ** and** that
50
+ {
51
+ type: ' root' ,
52
+ children: [
53
+ { type: ' leaf' , value: ' 1' },
54
+ { type: ' subtree' , children: [ { type: ' leaf' , value: ' 2' } ] },
55
+ { type: ' leaf' , value: ' 3' }
56
+ ]
57
+ }
52
58
```
53
59
54
60
## API
@@ -72,13 +78,30 @@ in `parent`.
72
78
Invoke the bound [ ` modifier ` ] [ modifier ] for each child in ` parent `
73
79
([ ` Node ` ] [ node ] ).
74
80
81
+ ## Related
82
+
83
+ * [ ` unist-util-visit ` ] ( https://github.com/syntax-tree/unist-util-visit )
84
+ — Visit nodes
85
+ * [ ` unist-util-visit-parents ` ] ( https://github.com/syntax-tree/unist-util-visit-parents )
86
+ — Visit nodes with ancestral information
87
+ * [ ` unist-util-filter ` ] ( https://github.com/eush77/unist-util-filter )
88
+ — Create a new tree with all nodes that pass a test
89
+ * [ ` unist-util-map ` ] ( https://github.com/syntax-tree/unist-util-map )
90
+ — Create a new tree with all nodes mapped by a given function
91
+ * [ ` unist-util-remove ` ] ( https://github.com/eush77/unist-util-remove )
92
+ — Remove nodes from a tree that pass a test
93
+ * [ ` unist-util-select ` ] ( https://github.com/eush77/unist-util-select )
94
+ — Select nodes with CSS-like selectors
95
+
75
96
## Contribute
76
97
77
- See [ ` contributing.md ` in ` syntax-tree/unist ` ] [ contributing ] for ways to get
98
+ See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
78
99
started.
100
+ See [ ` support.md ` ] [ support ] for ways to get help.
79
101
80
- This organisation has a [ Code of Conduct] [ coc ] . By interacting with this
81
- repository, organisation, or community you agree to abide by its terms.
102
+ This project has a [ Code of Conduct] [ coc ] .
103
+ By interacting with this repository, organisation, or community you agree to
104
+ abide by its terms.
82
105
83
106
## License
84
107
@@ -118,12 +141,14 @@ repository, organisation, or community you agree to abide by its terms.
118
141
119
142
[ author ] : https://wooorm.com
120
143
144
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/master/contributing.md
145
+
146
+ [ support ] : https://github.com/syntax-tree/.github/blob/master/support.md
147
+
148
+ [ coc ] : https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md
149
+
121
150
[ node ] : https://github.com/syntax-tree/unist#node
122
151
123
152
[ modifier ] : #next--modifierchild-index-parent
124
153
125
154
[ modify ] : #function-modifyparent
126
-
127
- [ contributing ] : https://github.com/syntax-tree/unist/blob/master/contributing.md
128
-
129
- [ coc ] : https://github.com/syntax-tree/unist/blob/master/code-of-conduct.md
0 commit comments