Inconsistent precedence in Markdown parser #42

Open
opened 2024-06-14 14:25:26 +02:00 by danieldongit · 1 comment
danieldongit commented 2024-06-14 14:25:26 +02:00 (Migrated from github.com)

There seems to be a conflict between my custom inline parser and image tags.
This is my code:

const copyOpen = '[copy]';
const copyClose = '[/copy]';
export const CopyExtension: MarkdownConfig = {
    defineNodes: [NodeTypes.copy, NodeTypes.copyTag],
    parseInline: [{
        name: NodeTypes.copy,
        parse(cx: InlineContext, next: number, pos: number) {
            const isOpenTag = cx.slice(pos, pos + copyOpen.length) === copyOpen;
            const isCloseTag = cx.slice(pos, pos + copyClose.length) === copyClose;

            if (isOpenTag) {
                return cx.addDelimiter(CopyDelim, pos, pos + copyOpen.length, true, false);
            } else if (isCloseTag) {
                return cx.addDelimiter(CopyDelim, pos, pos + copyClose.length, false, true);
            }
            return -1;
        },
        after: NodeTypes.italic,
    }],
    props: [
        styleTags({
            [NodeTypes.copyTag]: tags.special(CustomTags.CopyTag),
            [NodeTypes.copy]: tags.special(CustomTags.Copy),
        }),
    ],
};

This issue is that my copy tag gets mistaken for on image when placing a ! character before the closing tag.

Since it is parsed after Italic it should be parsed before images. If I set it to be before image it gets interpreted as a link.
I don't see what else can be done on my end to fix this.

Here are some screenshots of the behaviour:
image
image

There seems to be a conflict between my custom inline parser and image tags. This is my code: ``` const copyOpen = '[copy]'; const copyClose = '[/copy]'; export const CopyExtension: MarkdownConfig = { defineNodes: [NodeTypes.copy, NodeTypes.copyTag], parseInline: [{ name: NodeTypes.copy, parse(cx: InlineContext, next: number, pos: number) { const isOpenTag = cx.slice(pos, pos + copyOpen.length) === copyOpen; const isCloseTag = cx.slice(pos, pos + copyClose.length) === copyClose; if (isOpenTag) { return cx.addDelimiter(CopyDelim, pos, pos + copyOpen.length, true, false); } else if (isCloseTag) { return cx.addDelimiter(CopyDelim, pos, pos + copyClose.length, false, true); } return -1; }, after: NodeTypes.italic, }], props: [ styleTags({ [NodeTypes.copyTag]: tags.special(CustomTags.CopyTag), [NodeTypes.copy]: tags.special(CustomTags.Copy), }), ], }; ``` This issue is that my copy tag gets mistaken for on image when placing a `!` character before the closing tag. Since it is parsed after Italic it should be parsed before images. If I set it to be before image it gets interpreted as a link. I don't see what else can be done on my end to fix this. Here are some screenshots of the behaviour: ![image](https://github.com/lezer-parser/markdown/assets/150142406/cb7a886c-72ff-407e-93bb-663e59b12884) ![image](https://github.com/lezer-parser/markdown/assets/150142406/3524bd11-3899-4bd8-aaf7-170dd081ea01)
marijnh commented 2024-06-14 17:02:49 +02:00 (Migrated from github.com)

The order in which inline parsers get applied applies to the first character they match. So at the !, all inline parsers would be tried, in order, and since your custom syntax doesn't match there yet, even if it has a higher precedence than images, it won't match yet, but then the image parser will match, and consume the bracket syntax. As such, you may not be able to parse this notation in combination with images using @lezer/markdown, at the moment.

The order in which inline parsers get applied applies to the first character they match. So at the `!`, all inline parsers would be tried, in order, and since your custom syntax doesn't match there yet, even if it has a higher precedence than images, it won't match yet, but then the image parser will match, and consume the bracket syntax. As such, you may not be able to parse this notation in combination with images using @lezer/markdown, at the moment.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
lezer/markdown#42
No description provided.