Best way to get token positions for textMarker creation


I like to replace any Markdown-Mode link/url token that appears in the document at any time with a TextMarker.
And I know that there are a lot of ways, but you maybe know which is the best one?
(Performance is important, it is for displaying your files like a filemanager… up to 1000 links will happen sometimes)

Which Event to use?

Right know i use “changes”.
I increase the by the change.text.length if necessary and call eachLine() to get the lineHandle
Works well, but the stuff is bit ugly.

Is simpler and I would get the lineHandle directly.
But I think the documentation wan’t to say, I shouldn’t do that?^^ I’m not sure about what state means:

The handler may mess with the style of the resulting element, or add event handlers, but should not try to change the state of the editor.

Get Token Position

lineHandle.styles contains the token positions and names i need.
But is undefined if the line is not already rendered.

getLineTokens( ) the way I use right know.
But every token is only a single character and I have to loop token.type.indexOf("link") for any of them to find the start and end Chars on my own. That works, but I have the feeling it do something what codemirror already knows?

getTokenAt() according to the documentation not with good performance

getTokenAt, which re-parses the part of the line before the token for every call.

That is everything I have in mind right know.

Thank you! CodeMirror is so awesome!

I think you should not look at the code right now, its ugly ^^

Determining which lines need to be re-parsed based on change events, and then inspecting them with getLineTokens() is a solid approach. Don’t directly access lineHandle.styles, it’s not part of the public API (if it’s not documented, it’s not stable).

1 Like