Extending lezer-markdown?

Hi there! With CM6 + lezer-markdown, I was hoping to be able to extend the commonmarkLanguage so that it could parse content enclosed in ${} as JavaScript, marking it as a special “TemplateSubstitution” node, or something like that, if you will. After looking into the markdown parser a bit more, I see that MarkdownExtensions can provide parseInline and/or parseBlock. This theoretical “TemplateSubstitution” would itself be inline (as it could start anywhere in a paragraph), but it also could include multiple newlines within it, which I wouldn’t want to be considered separate markdown blocks. Is it possible to do something like this with the markdown parser such that it ignores new lines until it finds this particular inline node’s closing (I see it’s possible to skipSpace but I think this is only within a given line…) or is this not really feasible with the markdown parser? Hope this question makes sense — thanks so much in advance for the help!!

1 Like

No, that’s not currently possible—parsing happens in two phases: first the block structure is recognized, and then within that inline content is parsed. If your ${} escape contains, say, a blank line, that’ll end the enclosing paragraph. Of course, something like ${foo + \nbar} wouldn’t create empty lines or other markup that could end the paragraph, so that’d still work. But allowing any content in such a construct is hard to do with this parser.

1 Like

Hello, has there been any change to allowing these types of extensions? I have a similar scenario where I’m using Markdoc and would like to provide all of the necessary syntax highlighting without losing the existing markdown highlighting and autocompletion capabilities provided by GFM.

Markdoc has its own language syntax that augments what you can do in traditional markdown, and enables you to write custom component logic. I would like to provided supplemental autocompletion as well, hinting what custom components are available to the author.

In this sandbox I linked, this is actually a codemirror 5 editor that is styled using streams. I could go with this approach, but doesn’t offer anywhere near the capabilities of version 6.

See the docs for @lezer/markdown.