Cursor of md tokens to the cursor

Let’s say I have a function to get the cursor position

 public getCursorPosition = () => {
   const {state} = this.view;
   const {from, to, empty} = state.selection;

   if (empty) {
     return from;
   }

   return to;
 }

I would like to receive the number of md tokens per position. That is, if the text is italic, then I will receive 2 md tokens. (* and *) I need help because I have no idea how to do this. You can, of course, blindly follow the value and sort of parse it, but what if there is a better way?

You can write a PM to Md converter, and during the conversion process save the (mdPos, pmPos) pairs at node starts. From there you can calculate back mdPos from a pmPos. An example would be GitHub - emergence-engineering/prosemirror-text-map : this is for pure text, you’ll have to modify it to use markdown.