Text inserted at edge of a node gets wrapped within it

I have a markdown text

**hello `world`** 123

Rendered as: image

With their positions as

Now I insert some text right on the right-side of world at position ‘12’ as indicated above:

tr.insertText("foo", 12);

The ‘foo’ ends up inside the <code /> tag: image image

But I wanted the ‘foo’ to be adjacent to world but not inside its code tag. I can get that result if I insert like this:

tr.insertText("foo", 12, 13);

But that removes the existing whitespace between world and ‘123’: image

Which led me to insert like this…

tr.insertText("foo ", 12, 13);  // foo with a whitespace, duh

There is no such issue when I insert like this tr.insertText("foo", 12) and the last child only has one mark. For example, ‘nice’ is the last child and it only has one mark which is ‘strong’:

**hello `world`nice** 123

, and the result will be

hello worldnicefoo 123

Which is what I wanted.

Both the code mark and the strong mark are (supposedly) inclusive in your schema, which means that by default, text inserted directly after marked text will keep the mark. If you want to specifically set the marks for inserted content, use insert instead and add the correct marks to the inserted text node ahead of time.

1 Like