Hi, I’m trying to extend markdown syntax in Lezer/CodeMirror for an app I’m working on.
This new syntax should be inline and of the form ![[text]]
, i.e. text between the opening delimiters ![[
and ]]
.
The two best pieces of relevant information I found are DelimiterType's
section in the GitHub docs for lezer-markdown
, and this discussion here in this forum.
My strategy is to define two new inline parsers, one for each delimiter, and use cx.addDelimiter()
to register them when found (with the corresponding values for open
and close
, and using DelimiterType.resolve
with the same value for both). The full (relevant) code:
export const embeddedRefTags = {
embeddedRef: Tag.define(tags.link),
embeddedRefStart: Tag.define(tags.processingInstruction),
embeddedRefEnd: Tag.define(tags.processingInstruction),
}
const embeddedRef = "EmbeddedRef";
const embeddedRefStart = "EmbeddedRefStart";
const embeddedRefEnd = "EmbeddedRefEnd";
const embeddedRefStartDelim = { resolve: embeddedRef, mark: embeddedRef };
const embeddedRefEndDelim = { resolve: embeddedRef, mark: embeddedRef };
export const markdownEmbeddedRefs: MarkdownConfig = {
defineNodes: [embeddedRef, embeddedRefStart, embeddedRefEnd],
parseInline: [{
before: "Image",
name: embeddedRefStart,
parse(cx, _, pos) {
const openMark = '![[';
if (cx.slice(pos, pos + openMark.length) !== openMark) {
return -1;
}
return cx.addDelimiter(embeddedRefStartDelim, pos, pos + openMark.length, true, false);
}
}, {
before: "LinkEnd",
name: embeddedRefEnd,
parse(cx, _, pos) {
const closeMark = ']]';
if (cx.slice(pos, pos + closeMark.length) !== closeMark) {
return -1;
}
return cx.addDelimiter(embeddedRefEndDelim, pos, pos + closeMark.length, false, true);
}
}],
props: [
styleTags({
[embeddedRefStart]: embeddedRefTags.embeddedRefStart,
[embeddedRefEnd]: embeddedRefTags.embeddedRefEnd,
[`${embeddedRef}/...`]: embeddedRefTags.embeddedRef,
}),
],
}
this doesn’t seem to work; this extension above does seem to match the correct text, however iterating the syntax tree (with this extension registered) shows no signs of it. Here’s also a minimal reproduction.
What am I doing wrong?
If it’s relevant, changing both the open
and close
parameters of cx.addDelimiter()
to true
produces the expected behavior (they show up on the syntax tree, matching pairs of ![[
tokens and pairs of ]]
tokens).
Originally I had a solution which worked but used dirty tricks (most notably using cx.parts
heavily, in a similar fashion to some of the snippets in the thread mentioned above) and I’m now trying to do it properly.
Thanks!