syntax-tree / unist

Universal Syntax Tree used by @unifiedjs
https://unifiedjs.com
875 stars 19 forks source link

is it possible to insert a node or remove a node in ast tree? #28

Closed weituotian closed 4 years ago

weituotian commented 4 years ago

Subject of the feature

insert a node or remove a node in ast tree

Problem

could not find some method for Node to modify its text, its position.

Expected behaviour

add some api for Node

Alternatives

jquery can modify the dom ele positions and its attrs

wooorm commented 4 years ago

Hi! 👋

Trees are a JSON structure. That means that it’s arrays and objects, and you can use standard javascript functions such as .push to add something or children[index] = newNode.

More info on traversing the tree is described here: https://github.com/unifiedjs/unifiedjs.github.io/blob/src/doc/learn/tree-traversal.md.

It’s a good idea to look at the hundreds of utilities and plugins that exist for inspiration!

wooorm commented 4 years ago

I believe this question is now answered, so I’ll close it. Feel free to ask more questions, or continue here. However, Spectrum is the best place for questions!

weituotian commented 4 years ago

@wooorm thanks!! but i worry about how to modify the position attr of a node structure image

wooorm commented 4 years ago

If you are adding a new node, or replacing an existing node with a new node, it is suggested to not add positional info to them. Positional information references a place in an original source document, so if the things wasn’t in that document, it shouldn’t have positional information!