posthtml-parser icon indicating copy to clipboard operation
posthtml-parser copied to clipboard

Class Node

Open Scrum opened this issue 3 years ago • 0 comments

Have the same issue, I want to add a new node. Maybe add some method for creating a node to the core library? More "legal" way.

By now, I use a class. like this

// Or better use a plain object?
class Node {
  constructor(tag, attrs, content) {
    this.tag = tag;
    this.attrs = attrs;
    this.content = content;
  }
}

posthtml().use((tree) => {
  tree.match({ tag: 'p' }, (node) => {
    node.content ??= [
        new Node('span', { class: 'some' }, ['text']),
    ];

    return node;
  })
});

Originally posted by @shoonia in https://github.com/posthtml/posthtml/discussions/374#discussioncomment-1985737

Scrum avatar Jan 18 '22 06:01 Scrum