django-markdownx
django-markdownx copied to clipboard
Implement user mentions
Could you consider implementing user mentions?
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)
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