Skip to content

Latest commit

 

History

History
86 lines (61 loc) · 2.38 KB

api.md

File metadata and controls

86 lines (61 loc) · 2.38 KB

tree : object

API

Kind: global namespace Author: Ivan Voischev (@voischev), Anton Winogradov (@awinogradov), Alexej Yaroshevich (@zxqfox), Vasiliy (@Yeti-or)

tree.walk(cb) ⇒ function

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;
  });
};

tree.match(expression, cb) ⇒ function

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;
  });
};