Opening square bracket is ignored as OpeningCharacter #729

Closed
opened 2026-01-29 14:44:01 +00:00 by claunia · 4 comments
Owner

Originally created by @ridercz on GitHub (Mar 12, 2025).

I'm trying to write extension, which will convert markup [#something] to a link to an item with name something, ie. <a href="#something">name-of-something-got-from-lookup</a>.

My trouble is that when I define OpeningCharacters = ['[']; in my class derived from InlineParser, the parser is not invoked. If I use any other character (like #), it work just fine.

The minimal example is:

public class StepLinkInlineParser : InlineParser {

    public StepLinkInlineParser() {
        this.OpeningCharacters = ['['];
    }

    public override bool Match(InlineProcessor processor, ref StringSlice slice) {
        throw new NotImplementedException();
    }

}

The Match method is never called, the exception is never thrown, even when the source document contains [#something].

For now I solved the problem by using # as my opening character, but then i have extra [ in front of the link, ie. [<a href="#something">name-of-something-got-from-lookup</a>

Originally created by @ridercz on GitHub (Mar 12, 2025). I'm trying to write extension, which will convert markup `[#something]` to a link to an item with name `something`, ie. `<a href="#something">name-of-something-got-from-lookup</a>`. My trouble is that when I define `OpeningCharacters = ['['];` in my class derived from `InlineParser`, the parser is not invoked. If I use any other character (like `#`), it work just fine. The minimal example is: ```csharp public class StepLinkInlineParser : InlineParser { public StepLinkInlineParser() { this.OpeningCharacters = ['[']; } public override bool Match(InlineProcessor processor, ref StringSlice slice) { throw new NotImplementedException(); } } ``` The `Match` method is never called, the exception is never thrown, even when the source document contains `[#something]`. For now I solved the problem by using `#` as my opening character, but then i have extra `[` in front of the link, ie. `[<a href="#something">name-of-something-got-from-lookup</a>`
Author
Owner

@MihaZupan commented on GitHub (Mar 12, 2025):

Are you inserting your parser before the LinkInlineParser in the inline parser list?

@MihaZupan commented on GitHub (Mar 12, 2025): Are you inserting your parser before the `LinkInlineParser` in the inline parser list?
Author
Owner

@ridercz commented on GitHub (Mar 12, 2025):

@MihaZupan no, but from other documentation I thought that multiple parsers can share the same trigger character?

@ridercz commented on GitHub (Mar 12, 2025): @MihaZupan no, but from other documentation I thought that multiple parsers can share the same trigger character?
Author
Owner

@ridercz commented on GitHub (Mar 12, 2025):

@MihaZupan when I positioned it before the LinkInlineParser, it solved my problem, thanks.

@ridercz commented on GitHub (Mar 12, 2025): @MihaZupan when I positioned it before the `LinkInlineParser`, it solved my problem, thanks.
Author
Owner

@MihaZupan commented on GitHub (Mar 12, 2025):

They can share it, but if one parser says it matched that location then others won't be attempted. So order matters there.

Alternatiely if you're just trying to replace the URLs based on some lookup, consider post-processing the MarkdownDocument instead of implementing a parser, it might be simpler.

@MihaZupan commented on GitHub (Mar 12, 2025): They can share it, but if one parser says it matched that location then others won't be attempted. So order matters there. Alternatiely if you're just trying to replace the URLs based on some lookup, consider post-processing the `MarkdownDocument` instead of implementing a parser, it might be simpler.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#729