Kind: global namespace Author: Ivan Voischev (@voischev), Anton Winogradov (@awinogradov), Alexej Yaroshevich (@zxqfox), Vasiliy (@Yeti-or)
- tree :
object
- .walk(cb) ⇒
function
- .match(expression, cb) ⇒
function
- .walk(cb) ⇒
Walks the tree and passes all nodes via a callback
Kind: static method of tree
Returns: function
- Callback(node)
Param | Type | Description |
---|---|---|
cb | function |
Callback |
Example
export const walk = tree => {
tree.walk(node => {
let classes = (node.attrs && node.attrs.class.split(" ")) || [];
if (classes.includes(className)) return cb(node);
return node;
});
};
Matches an expression to search for nodes in the tree
Kind: static method of tree
Returns: function
- Callback(node)
Param | Type | Description |
---|---|---|
expression | String | RegExp | Object | Array |
Matcher(s) to search |
cb | function |
Callback |
Example
export const match = tree => {
// Single matcher
tree.match({ tag: "custom-tag" }, node => {
let tag = node.tag;
Object.assign(node, { tag: "div", attrs: { class: tag } });
return node;
});
// Multiple matchers
tree.match([{ tag: "b" }, { tag: "strong" }], node => {
let style = "font-weight: bold;";
node.tag = "span";
node.attrs
? node.attrs.style
? (node.attrs.style += style)
: (node.attrs.style = style)
: (node.attrs = { style: style });
return node;
});
};