What does index and indexAfter on ResolvedPos exactly means?

I have been trying to get my head around index() and indexAfter() functions in class ResolvedPos.

Let’s say an example, where my schema is paragraph, which has a span node, which further has text inline node.

So in this example, if i have $from.pos = 19, then the index return $from.index() = 1 and indexAfter is $from.indexAfter() = 1.

And if i have $from.pos = 9 then the index return $from.index() = 0 and indexAfter is $from.indexAfter() = 1

So from above examples, i get confused what the index() and indexAfter() really is.

Can someone please explain what they actually do in context of the above example.

When called without argument, they return child indices in the position’s parent node. So for positions that fall between nodes, they’ll return the same value. For positions inside a text node, indexAfter will be one more than index().

If you pass an argument to select an ancestor node further up the tree, indexAfter is always one more than index, because the position points into a child of that node.

1 Like

Thanks Marijn. Thats useful information. I also did some more inspection, and found out more about it.

I have been scratching my head to think about this until I found this explanation. I suggest we should add this to the doc of the method, because IMHO the source code is not that self-describing …