Get Marks and their position surrounding cursor

I would like to get all the marks which surrounding the cursor in hierarchical order. So if you take for example:

Vivamus 
<span class="text-red">
rhoncus justo vitae 
<i>sem consequat, ac <b>tristique{{$cursor}}</b> neque maximus. </i>
Etiam et sapien vel ipsum tincidunt iaculis. Nullam sed purus mi.
</span> 
Morbi eu vestibulum augue, at dignissim met

where {{$cursor}} denotes the position of the cursor.

I would like to end up with and array like

[
    { from: 26; to: 35, name: 'b' ''},
    { from: 17; to: 45, name: 'i'}
    { from: 7; to: 56, name: 'span'}
]

Notice how they are in order as you travel up the dom tree. Is there an already established easy way to get hold of this information. If you had to create it from scratch how would you go about it ?

I have found https://prosemirror.net/docs/ref/#model.ResolvedPos.marks. but the marks are returned in no particular order.

Maybe I could use them but I would still need to get their positions. Any thought on this would be great ! Thank you :slight_smile:

state.selection.$head.marks() should work (though the question of which marks are at a given position is a bit subtle in boundary cases).

1 Like

Oh dear I linked to the wrong part of the docs … this is where I meant to link sorry. https://prosemirror.net/docs/ref/#model.ResolvedPos.marks

Anyway …

  1. The order which state.selection.$head. marks () returns the marks is arbitrary or sort of. Not in the order which I need them.

  2. When you get the marks back from https://prosemirror.net/docs/ref/#model.ResolvedPos.marks they don’t contain their position. How do you get their position anyway ?

Sounds like there is no ‘built in’ solution for this … I just wanted to check before I bodge my own :slight_smile:

No it’s not, it’s the normalized order based on the order of the marks in the schema.

Marks do not have a position. They may be attached to nodes, and if those nodes are adjacent you may consider the marked region to belong together, but the way they are stored doesn’t reflect that, so you’ll have to scan the marked node’s sibling nodes.

1 Like

Blockquote No it’s not, it’s the normalized order based on the order of the marks in the schema.

I realised that in the meantime as well. They are in order but their order is not helpful in this case.

Marks do not have a position.

Yup. I now get it. I have to bodge it my way. : |

Thank you for your help !