mirror of
https://github.com/zulip/zulip.git
synced 2026-07-12 21:04:41 +08:00
bugdown: Linkify using a Markdown extension
This prevents trying to linkify inside code blocks. (imported from commit 97dd20ecee19f41650aa98e68aa8e9908ece5b33)
This commit is contained in:
parent
d72aea25f2
commit
649cd15415
@ -12,6 +12,14 @@ class Gravatar(markdown.inlinepatterns.Pattern):
|
||||
% (gravatar_hash(match.group('email')),))
|
||||
return img
|
||||
|
||||
class AutoLink(markdown.inlinepatterns.Pattern):
|
||||
def handleMatch(self, match):
|
||||
url = match.group('url')
|
||||
a = markdown.util.etree.Element('a')
|
||||
a.set('href', url)
|
||||
a.text = url
|
||||
return a
|
||||
|
||||
class Bugdown(markdown.Extension):
|
||||
def extendMarkdown(self, md, md_globals):
|
||||
del md.inlinePatterns['image_link']
|
||||
@ -21,6 +29,10 @@ class Bugdown(markdown.Extension):
|
||||
|
||||
md.inlinePatterns.add('gravatar', Gravatar(r'!gravatar\((?P<email>[^)]*)\)'), '_begin')
|
||||
|
||||
# A link starts after whitespace and continues to the next whitespace.
|
||||
link_regex = r'(\s|\A)(?P<url>https?://[^\s]+)'
|
||||
md.inlinePatterns.add('autolink', AutoLink(link_regex), '_begin')
|
||||
|
||||
# We need to re-initialize the markdown engine every 30 messages
|
||||
# due to some sort of performance leak in the markdown library.
|
||||
MAX_MD_ENGINE_USES = 30
|
||||
@ -28,11 +40,6 @@ MAX_MD_ENGINE_USES = 30
|
||||
_md_engine = None
|
||||
_use_count = 0
|
||||
|
||||
# A link starts after whitespace, and cannot contain spaces,
|
||||
# end parentheses, or end brackets (which would confuse Markdown).
|
||||
# FIXME: Use one of the actual linkification extensions.
|
||||
_link_regex = re.compile(r'(\s|\A)(?P<url>https?://[^\s\])]+)')
|
||||
|
||||
def _linkify(match):
|
||||
url = match.group('url')
|
||||
return ' [%s](%s) ' % (url, url)
|
||||
@ -49,8 +56,6 @@ def convert(md):
|
||||
codehilite.makeExtension(configs=[('force_linenos', False)]),
|
||||
Bugdown()])
|
||||
|
||||
md = _link_regex.sub(_linkify, md)
|
||||
|
||||
try:
|
||||
html = _md_engine.convert(md)
|
||||
except:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user