Keep Matching Characters in textblockTypeInputRule

I’m trying to modify the textblockTypeInputRule to keep the matching characters on an input rule trigger. Specifically, wrap ``` in a codeblock without deleting the backticks in the same transaction.

My first thought is to remove the delete, replacing

    return state.tr
      .delete(start, end)
      .setBlockType(start, start, nodeType, attrs)

with

    return state.tr
      .setBlockType(start, end, nodeType, attrs)

However, this just results in a codeblock with 2 backticks instead of 3.

``

An approach that’s sort of hacky is to delete the backticks and re-insert them after after the setBlockType.

    return state.tr
      .delete(start, end)
      .setBlockType(start, start, nodeType, attrs)
      .insertText('```', start, start)

Is there a less hacky way to keep the 3rd backtick that triggers the inputrule?

The input-rules plugin handles the text input, so indeed, the action is produces replaces the actual insertion, and if you want to keep it, you’ll have to insert it. You should be able to leave the initial two ticks alone and just insert the last one, I guess.

Just out of curious, why would you want to keep these backticks in the code block?

Instead of having a button that changes codeblock language, I would prefer to use the markdown syntax of three backticks. I really enjoyed how syntax highlighting for codeblocks worked in codemirror (https://codemirror.net/mode/markdown/) so I’m trying to reimplement the same thing in ProseMirror.

Thx for sharing the story. I can see what you mean, but I can’t imagine how that looks like. In my experience supporting for code blocks is either plain markup (like this editor I use to write this reply), or WYSIWYG.

Do you think the codemirror markdown mode is plain text or WYSIWYG? Here’s an example of something similar: https://tiptap.scrumpy.io/code-highlighting.