How to storage tokens and their line in object?

My file format like this:
[header1]

[header2]

I’ve made a mode to highlight headers, and give them class “cm-header”. I want to storage headers and their lines to object like this: {'header1": 10, “header2”: 20}, and I hope this object update when the text changed, the text file may very big. Is there an efficient way to deal with this problem?

You could write code that listens to "change" events and updates your data structure by re-scanning only the changed lines. CodeMirror itself doesn’t provide a framework for doing this (though it does expose the events and methods you need for it).

Thanks for your help, which is very useful. I did not notice this event parameters in the document, just though it like the normal “change” event of DOM.