diff --git a/biostar/forum/markdown.py b/biostar/forum/markdown.py index e91dca6c8..ba4b7d756 100644 --- a/biostar/forum/markdown.py +++ b/biostar/forum/markdown.py @@ -346,7 +346,7 @@ def embedder(attrs, new, embed=None): return attrs href = attrs['_text'] - linkable = href[:4] in ('http', 'ftp:', 'https') + linkable = href.startswith(('http', 'ftp:', 'https')) # Don't linkify non http links if not linkable: @@ -377,17 +377,14 @@ def embedder(attrs, new, embed=None): def linkify(text): # List of links to embed embed = [] - html = bleach.linkify(text=text, callbacks=[partial(embedder, embed=embed)], skip_tags=['pre', 'code']) + html = bleach.linkify(text=text, callbacks=[partial(embedder, embed=embed), nofollow], skip_tags=['pre', 'code']) # Embed links into html. for em in embed: source, target = em - emb = f'{source}' + emb = f'{source}' html = html.replace(emb, target) - # Add nofollow to each link. - html = bleach.linkify(text=html, callbacks=[nofollow], skip_tags=['pre', 'code']) - return html diff --git a/biostar/forum/tests/test_markdown.py b/biostar/forum/tests/test_markdown.py index 1e301f621..17d6dbc93 100644 --- a/biostar/forum/tests/test_markdown.py +++ b/biostar/forum/tests/test_markdown.py @@ -24,10 +24,10 @@ (f"{settings.PROTOCOL}://{SITE_URL}/u/5 ", f'
'), # Twitter link - ("https://twitter.com/Linux/status/2311234267", ''), + ("https://twitter.com/Linux/status/2311234267", 'w00t! 10,000 followers!
— Linux (@Linux) June 24, 2009
'), # Youtube link - ("https://www.youtube.com/watch?v=dQw4w9WgXcQ", ''), + ("https://www.youtube.com/watch?v=dQw4w9WgXcQ", ''), # Gist link ("https://gist.github.com/afrendeiro/6732a46b949e864d6803", ''),w00t! 10,000 followers!
— Linux (@Linux) June 24, 2009