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.

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