Alternative inputrules implementation: @handlewithcare/prosemirror-inputrules

Howdy! Recently I had the need for an inputrules plugin with slightly different behavior from the default, so I lightly forked prosemirror-inputrules. Maybe some other folks will also want this approach!

@handlewithcare/prosemirror-inputrules

From the README:

Differences from prosemirror-inputrules

The primary behavior difference from prosemirror-inputrules is that this library actually applies the text that triggers its rules before executing the rules. In the first example, we had the following rule:

new InputRule(/->\s/, "β†’ ");

Using prosemirror-inputrules, if a user typed - and then >, the editor history would look like:

[
  {
    "from": 1,
    "to": 1,
    "text": "-"
  },
  {
    "from": 1,
    "to": 2,
    "text": "β†’"
  }
]

The plugin identifies that a user is going to type ->, and executes the input rule instead of applying the >. This means that an undo command, rather than undoing just the input rule, undoes the insertion of the > character, resulting in just - again.

By contrast, here’s the editor history after the same inputs using this library:

[
  {
    "from": 1,
    "to": 1,
    "text": "-"
  },
  {
    "from": 2,
    "to": 2,
    "text": ">"
  },
  {
    "from": 1,
    "to": 3,
    "text": "β†’"
  }
]

Since this library actually applies the > character to the editor before executing the input rule, an undo command only undoes the input rule, resulting in what the user initially typed (->).

Also, this library exports a markTypeInputRule builder that can be used for Markdown-style formatting (e.g. **text** becomes text).

2 Likes