Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add noopener to external links #371

Merged
merged 2 commits into from Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/markdown2.py
Expand Up @@ -389,12 +389,17 @@ def convert(self, text):
# return the removed text warning to its markdown.py compatible form
text = text.replace(self.html_removed_text, self.html_removed_text_compat)

if "nofollow" in self.extras:
text = self._a_nofollow.sub(r'<\1 rel="nofollow"\2', text)

if "target-blank-links" in self.extras:
text = self._a_blank.sub(r'<\1 target="_blank"\2', text)

if "nofollow" in self.extras:
if "target-blank-links" in self.extras:
text = self._a_nofollow.sub(r'<\1 rel="nofollow noopener"\2', text)
else:
text = self._a_nofollow.sub(r'<\1 rel="nofollow"\2', text)
elif "target-blank-links" in self.extras:
text = self._a_nofollow.sub(r'<\1 rel="noopener"\2', text)

if "toc" in self.extras and self._toc:
self._toc_html = calculate_toc_html(self._toc)

Expand Down Expand Up @@ -2234,7 +2239,7 @@ def _encode_amps_and_angles(self, text):
def _encode_incomplete_tags(self, text):
if self.safe_mode not in ("replace", "escape"):
return text

if text.endswith(">"):
return text # this is not an incomplete tag, this is a link in the form <http://x.y.z>

Expand Down
4 changes: 2 additions & 2 deletions test/tm-cases/link_with_blank.html
@@ -1,5 +1,5 @@
<p><a target="_blank" href="http://www.example.com">Ref</a></p>
<p><a rel="noopener" target="_blank" href="http://www.example.com">Ref</a></p>

<p><a href="#bar">Foo</a></p>

<p><a target="_blank" href="http://www.example.com/two#three">One</a></p>
<p><a rel="noopener" target="_blank" href="http://www.example.com/two#three">One</a></p>