Trigger InputRule on enter?


#1

I’d like to trigger my markdown code block shortcut on whitespace OR enter:

```js(space|enter)

InputRules were definitely not designed for ENTER, thoughts on extending it to make it work? Perhaps there’s another way for me to go about triggering this on enter?

I could also see people wanting to trigger this via a multiline rule that awaits the bottom ```:

```js
function() { return true;}
```

#2

Did you ever find a solution to this? I’m facing the same problem where I want to allow URLs to be linkified when they occur at the end of a paragraph.


#3

I am also looking to do exactly this code-block example.

I might go with /^```([a-zA-Z]*)? $/, but the SPACE feels kinda weird compared to ENTER


#4

You can do this, but you’ll need to handle the ‘Enter’ keypress yourself. i.e. inside an ‘Enter’ keymap handler you’ll need to do something like

if (!state.selection.$cursor) {
  return false;
}
const { nodeBefore } = state.selection.$from;
if (!nodeBefore || !nodeBefore.isText) {
  return false;
}
const regex = /^```([a-zA-Z]*)?$/;
const matches = nodeBefore.text.match(regex);
if (matches) {
  const [, language] = matches;
  // create the node with the language, etc. & set the selection inside it
  // you may also want to assert a depth in the document, etc. if you have blockquotes, etc as otherwise this would get triggered in there too
  return true;
}
return false;

The reason why this doesn’t work is because the inputrules pluginonly looks at the text between the parent of the node you’re typing in and your selection head - which means these input rules can never span across paragraphs.