Skip to content

inlinePatterns should have access to their parent/ancestors. #596

@waylan

Description

@waylan

It is reasonable to expect that some inlinePatterns should not apply if an ancestor is of a specific set of elements. For example. an @mention should not be converted to a "mention link" if the text is in a link label (like this: [@waylan](https://example.com/waylan)). Also consider that ancestors are potentially relevant ([**@waylan**](https://example.com/waylan)). In fact, see Python-Markdown/github-links#5 for that very example.

Perhaps markdown.inlinepatterns.Pattern.handleMatch should have an ancestors keyword passed to it which would include a list of ancestor elements. I doubt we need to actually pass the elements themselves. Presumably, strings of their tag names would be sufficient. Do we only pass the inline elements, or include the block-level elements all the way up to the root? Should we pass a set (with no repetitions) or a list (with all elements in order)?

I'm thinking perhaps it could be used something like this:

class MentionPattern(Pattern):

    def handleMatch(self, m, ancestors):
        if 'a' in ancestors:
            # A false match. Return None to indicate no change.
            return None
        # build and return element here...

As a reminder, it would not be a good idea to pass the parent element as the element could include text before and after the match that would need to wrap the created element. While the user (extension dev) could access and rebuilt the content correctly, that just opens up more ways for them to break things. Therefore, I'm inclined to limit this to a list of tags (as string names).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions