Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent precedence in Markdown parser #42

Open
danieldongit opened this issue Jun 14, 2024 · 1 comment
Open

Inconsistent precedence in Markdown parser #42

danieldongit opened this issue Jun 14, 2024 · 1 comment

Comments

@danieldongit
Copy link

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

@marijnh
Copy link
Contributor

marijnh commented Jun 14, 2024

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants