Skip to content

Commit

Permalink
Merge pull request #809 from Natay/master
Browse files Browse the repository at this point in the history
fix in bleach linkifiy
  • Loading branch information
ialbert committed Apr 13, 2021
2 parents da5852d + 66e638b commit 6908d55
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
9 changes: 3 additions & 6 deletions biostar/forum/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'<a href="{source}">{source}</a>'
emb = f'<a href="{source}" rel="nofollow">{source}</a>'
html = html.replace(emb, target)

# Add nofollow to each link.
html = bleach.linkify(text=html, callbacks=[nofollow], skip_tags=['pre', 'code'])

return html


Expand Down
4 changes: 2 additions & 2 deletions biostar/forum/tests/test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
(f"{settings.PROTOCOL}://{SITE_URL}/u/5 ", f'<p><a href="{settings.PROTOCOL}://{SITE_URL}/u/5" rel="nofollow">tested2</a></p>'),

# Twitter link
("https://twitter.com/Linux/status/2311234267", '<p></p><blockquote class="twitter-tweet"><p lang="en" dir="ltr">w00t! 10,000 followers!</p> Linux (@Linux) <a href="https://twitter.com/Linux/status/2311234267?ref_src=twsrc%5Etfw" rel="nofollow">June 24, 2009</a></blockquote><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script><p></p>'),
("https://twitter.com/Linux/status/2311234267", '<p><blockquote class="twitter-tweet"><p lang="en" dir="ltr">w00t! 10,000 followers!</p>&mdash; Linux (@Linux) <a href="https://twitter.com/Linux/status/2311234267?ref_src=twsrc%5Etfw">June 24, 2009</a></blockquote><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script></p>'),

# Youtube link
("https://www.youtube.com/watch?v=dQw4w9WgXcQ", '<p><iframe width="420" height="315" src="//www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen=""></iframe></p>'),
("https://www.youtube.com/watch?v=dQw4w9WgXcQ", '<p><iframe width="420" height="315" src="//www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe></p>'),

# Gist link
("https://gist.github.com/afrendeiro/6732a46b949e864d6803", '<p><script src="https://gist.github.com/afrendeiro/6732a46b949e864d6803.js"></script></p>'),
Expand Down

0 comments on commit 6908d55

Please sign in to comment.