django-markdownx icon indicating copy to clipboard operation
django-markdownx copied to clipboard

Implement user mentions

Open azaitsev opened this issue 5 years ago • 2 comments

Could you consider implementing user mentions?

azaitsev avatar Mar 10 '19 13:03 azaitsev

I just realize the code for one of my project.

I will try to create PR when the code will be improved. But it's will be something like this (based on @username):

USERNAME_RE = r'(@)(\S+)'
 
class UsernamePattern(InlineProcessor):
    """ Return a link to User page based on '/user/<username> """
    def handleMatch(self, m, data):
        username = m.group(2)

        el = markdown.util.etree.Element("a")
        el.set('href', f'/user/{username}')
        el.text = markdown.util.AtomicString(m.group(0))

        return el, m.start(0), m.end(0)


class UsernameExtension(Extension):
    """
    Wrap '@username' to <a href='/user/username'>@username</a>
    """
    def extendMarkdown(self, md, md_globals):
        md.registerExtension(self)
        md.inlinePatterns['usernamelink'] = UsernamePattern(USERNAME_RE, md)

Pyvonix avatar Apr 13 '20 16:04 Pyvonix

Thank @adi- for this great extension.

I can propose you to solve this issue by creating a repertory in markdownx\extensions where we can put all future features you want to see in your project. I can create a file mdx_username.py inside who can be enabled by setting a variable like MARDKWONX_USERNAME = '/user/' in setrings.py (by default MARDKWONX_USERNAME = None and feature not active).

Tell me what you think of this implementation before I propose it

Pyvonix avatar Apr 14 '20 15:04 Pyvonix