How to update multiple inline decorations on node change


#1

Hey, currently I try to implement automatic code highlighting with highlight.js for code blocks. It’s already working but it’s not performant for large documents because it’s getting re-rendered on every change of the editor.

highlighting

I can’t find any example for decorations in a similar use case. So basically here is what I’ve done already:

  • I create a Plugin() for adding my decorations
  • I search in the doc for all code_block nodes
  • I create an array of inline decorations for all code highlights of all code blocks and return them with a DecorationSet

At the moment I found only two ways to get this working:

  1. Re-render all decorations on every change
  2. Render all decorations only on init

But what I want is to render all decorations for code_block nodes on init and re-render them individually whenever a code_block node changes.

I have no clue how to solve this because nodes do not have any identifiers so if I try to cache all the decorations I do not know which decorations belongs to which nodes. :weary:Any idea?


#2

What you do is track the position of the element you are interested in, and on each transaction, map to its new position (possibly tracking both the start and end so that it’s easier to determine whether any of the step maps in the transaction touch it).