How to get ascendants of a node?

I would like to know if a node is inside a paragraph. How could I do?

I saw this page saying there is no .parent attribute. But, how could I traverse the ascendants of a node?

From a Node object, you can’t. But if you have a resolved position for the node’s position, you can inspect that (with the node method).

Thank you for the answer.

More precisely, I would like to get the common ascendants of the document selection. How could I get them? How could I get a resolved position from the common parent of the selection?

The sharedDepth method can give you the depth of shared ancestors (via something like sel.$from.sharedDepth(sel.to)), which you can then iterate over.

Okay, thank you!

Suggestion

It would be also nice to have a .$before(depth) method that returns the resolved position of .before(depth) (the same for .$after, .$begin, .$end). Hence, everything could be done with resolved position.

Motivation of this suggestion

Currently, I find it very difficult to use Prosemirror. The difficulty is because PM has 3 main concepts (nodes, positions and resolved positions) and no easy way to do everything in one of them. PM functions sometimes use pos, sometimes node, sometimes resolved pos, which forces either (a) to convert one in the other, or (b) to keep a synchronized version of each.

(a) is hard:

  • If I want to go from pos to node, I need to resolve the pos which requires to have the document node!
  • If want to go from node to pos, I can’t! So, I have to always keep a resolved position. But resolved position methods always return position. So, I need to also keep the document node, i.e. I need to keep two objects along side, i.e. I need to do (b), which is painful.

Edit:

I have just seen that a resolved position has a link to the doc!

So, $pos.doc.resolve($pos.before(1)) would give a resolve position! But, this is not convenient to write… Having something like $pos.$before(1) that returns a resolved position would make it much easier, and now, everything could be done with resolved positions.