unist-util-modify-children
Unist (mdast,
retext) utility to modify direct children of
a parent. As in, wrap fn
so it accepts a parent and invoke fn
for each of
its children. When fn
returns a number, goes to that child next.
Installation
npm:
npm install unist-util-modify-children
unist-util-modify-children is also available for bower,
component, and
duo, and as an AMD, CommonJS, and globals
module, uncompressed and compressed.
Usage
var modifyChildren = require('unist-util-modify-children');
var modifier = modifyChildren(function (child, index, parent) {
console.log(child, index);
if (child.value === 'bravo') {
parent.children.splice(index + 1, 0, { 'type': 'bar', 'value': 'delta' });
return index + 1;
}
});
var parent = {
'type': 'foo',
'children': [
{ 'type': 'bar', 'value': 'alpha' },
{ 'type': 'bar', 'value': 'bravo' },
{ 'type': 'bar', 'value': 'charlie' }
]
};
modifier(parent);
console.log(parent);
API
modifyChildren(fn)
Parameters
Return
Function
— Wrapped fn
.
function fn(child, index, parent)
Modifier for children of parent
.
Parameters
-
child
(Node
)
— Current iteration;
-
index
(number
) — Position of child
in parent
;
-
parent
(Node
)
— Parent node of child
.
Returns
number
(optional) — Next position to iterate.
function plugin(parent)
Function invoking fn
for each child of parent
.
Parameters
parent
(Node
)
— Node with children.
Throws
Error
— When not given a parent node.
License
MIT © Titus Wormer