# Can't get inputrule to apply mark to match only

**URL:** https://discuss.prosemirror.net/t/cant-get-inputrule-to-apply-mark-to-match-only/3614
**Category:** Uncategorized
**Created:** [March 19, 2021, 6:30pm UTC](https://discuss.prosemirror.net/t/cant-get-inputrule-to-apply-mark-to-match-only/3614 "2021-03-19T18:30:23Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![phikes](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/phikes/32/1709_2.png) [@phikes](https://discuss.prosemirror.net/u/phikes)
#### Post date: [March 19, 2021, 6:30pm UTC](https://discuss.prosemirror.net/t/cant-get-inputrule-to-apply-mark-to-match-only/3614/1 "2021-03-19T18:30:23Z")

</div>

Hi there,

I use the input rule plugin likes this to allow to add inline code blocks by typing backticks.

```auto
  new InputRule(/`(.*)`$/, (state, match, start, end) => state.tr
    .insertText(match[1], start, end)
    .addMark(start, end, schema.marks.code.create())
    .removeStoredMark(schema.marks.code)
  ),

```

I know that the cursor position in inline elements in browsers can be tricky, however it doesn’t remove the code mark at all if I keep typing. If I type my backticks and keep on typing after the text is wrapped in the code mark the text I type gets appended inside the mark’s wrapping text node and not afterwards.

It seems to be similar (but less generic) than the solution posted here: [Input rules for wrapping marks - #11 by ncphillips](https://discuss.prosemirror.net/t/input-rules-for-wrapping-marks/537/11)

What could I be doing wrong?

Thanks for the help!

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [March 19, 2021, 9:01pm UTC](https://discuss.prosemirror.net/t/cant-get-inputrule-to-apply-mark-to-match-only/3614/2 "2021-03-19T21:01:15Z")

</div>

I’m not sure if that’s the problem you’re running into, but you can’t use `end` like that in `addMark`, since it points at the end of the matched range in the document _before_ you ran `insertText`, and that won’t be a meaningful position anymore after that change.

You should be able to just run `replace` with an already-marked text node instead of `insertText`/`addMark`. That should simplify the whole thing.

---

<div class="post-metadata">

### Author: ![phikes](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/phikes/32/1709_2.png) [@phikes](https://discuss.prosemirror.net/u/phikes)
#### Post date: [March 20, 2021, 9:35am UTC](https://discuss.prosemirror.net/t/cant-get-inputrule-to-apply-mark-to-match-only/3614/3 "2021-03-20T09:35:22Z")

</div>

Since in my case I have a fixed delimiter (backticks) in a fixed regex, I think just taking `end - 1` should have done the trick. I tried variations of that because I wasn’t sure if the implementation of `addMark` uses the limits exclusively or inclusively. However no dice.

I tried other variations of your suggestion before (e.g. deleting from start to end, then inserting, then adding mark, etc.). I even tried to set the selection explicitly again and inserting (and then deleting) a space after my insertion. Re your suggestion:

```auto
  new InputRule(/`(.*)`$/, (state, match, start, end) => state.tr
    .replaceRangeWith(start, end, schema.text(match[1], [schema.marks.code.create()]))
    .removeStoredMark(schema.marks.code) // with or without doesn't change anything
  ),

```

This doesn’t work either (not at the end of the doc, nor with content coming after it). I checked what exactly happens with the DOM document’s selection to see if I can replicate pressing right arrow after my substitution (which removes the stored mark). I couldn’t even see any difference between a cursor position which ends the mark on typing or which keeps it when typing. Both are inside my text node at the last position.

I am not 100% sure, but I know you mentioned before that browsers handle this case weirdly and I am wondering if this just stopped working in the meantime in Chrome.

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [March 20, 2021, 11:03am UTC](https://discuss.prosemirror.net/t/cant-get-inputrule-to-apply-mark-to-match-only/3614/4 "2021-03-20T11:03:21Z")

</div>

Could it be that `schema` isn’t the same as `state.schema`?

---

<div class="post-metadata">

### Author: ![phikes](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/phikes/32/1709_2.png) [@phikes](https://discuss.prosemirror.net/u/phikes)
#### Post date: [March 20, 2021, 11:49am UTC](https://discuss.prosemirror.net/t/cant-get-inputrule-to-apply-mark-to-match-only/3614/5 "2021-03-20T11:49:56Z")

</div>

Thanks for the replies btw.

I checked and they are the exact same object (compared with ===).

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [March 20, 2021, 4:45pm UTC](https://discuss.prosemirror.net/t/cant-get-inputrule-to-apply-mark-to-match-only/3614/6 "2021-03-20T16:45:19Z")

</div>

Strange. The input rule you showed above works for me.

---

<div class="post-metadata">

### Author: ![phikes](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/phikes/32/1709_2.png) [@phikes](https://discuss.prosemirror.net/u/phikes)
#### Post date: [March 22, 2021, 1:33pm UTC](https://discuss.prosemirror.net/t/cant-get-inputrule-to-apply-mark-to-match-only/3614/7 "2021-03-22T13:33:39Z")

</div>

Could you share how you set that up? I remixed your example editor from the website and there it doesn’t work for me in (close to latest) Chrome.

> **[Glitch](https://glitch.com/edit/#!/wirehaired-gorgeous-belief)**
>
> Combining automated deployment, instant hosting & collaborative editing, Glitch gets you straight to coding so you can build full-stack web apps, fast

I tried using `replaceRangeWith` in the remix as well, however that just removed the whole input and didn’t insert anything.

---

<div class="post-metadata">

### Author: ![phikes](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/phikes/32/1709_2.png) [@phikes](https://discuss.prosemirror.net/u/phikes)
#### Post date: [March 22, 2021, 1:34pm UTC](https://discuss.prosemirror.net/t/cant-get-inputrule-to-apply-mark-to-match-only/3614/8 "2021-03-22T13:34:46Z")

</div>

For the record in the glitch project I shared it applies the code mark correctly, but when I start typing again it removes it from the text again.

---

<div class="post-metadata">

### Author: ![phikes](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/phikes/32/1709_2.png) [@phikes](https://discuss.prosemirror.net/u/phikes)
#### Post date: [March 23, 2021, 5:56pm UTC](https://discuss.prosemirror.net/t/cant-get-inputrule-to-apply-mark-to-match-only/3614/9 "2021-03-23T17:56:38Z")

</div>

I couldn’t figure out the root cause, so in the meantime I solved it by inserting a zero-width space:

```auto
  new InputRule(/`(.*)`$/, (state, match, start, end) =>
    state.tr
      .replaceRangeWith(start, end, schema.text(match[1], [schema.marks.code.create()]))
      .removeStoredMark(schema.marks.code)
      .insertText('\u200B', end - 1),
  ),

```

That works well and in our use case we are fine with these.

---

<div class="post-metadata">

### Author: ![rowanc1](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/rowanc1/32/1341_2.png) [@rowanc1](https://discuss.prosemirror.net/u/rowanc1)
#### Post date: [October 31, 2021, 10:37pm UTC](https://discuss.prosemirror.net/t/cant-get-inputrule-to-apply-mark-to-match-only/3614/10 "2021-10-31T22:37:33Z")

</div>

I was taking some of this discussion as inspiration for [prosemirror-codemark](https://discuss.prosemirror.net/t/new-plugin-prosemirror-codemark-to-handle-inline-code-mark-creation-and-manipulation/4159), which solves some of these issues for inline code marks. Might be helpful?! Let me know!
