From f929453ebcfd4d68bcae2fbc6fe0f619b2e870a1 Mon Sep 17 00:00:00 2001 From: Mathias Rav Date: Wed, 8 Aug 2018 17:48:01 +0200 Subject: [PATCH] redlinks: Don't mark mailto links as broken --- src/wiki/plugins/redlinks/mdx/redlinks.py | 16 ++++++++++++++-- tests/plugins/redlinks/test_redlinks.py | 9 +++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/wiki/plugins/redlinks/mdx/redlinks.py b/src/wiki/plugins/redlinks/mdx/redlinks.py index 807fbc77..8ce2dffc 100644 --- a/src/wiki/plugins/redlinks/mdx/redlinks.py +++ b/src/wiki/plugins/redlinks/mdx/redlinks.py @@ -1,6 +1,8 @@ +import html from urllib.parse import urljoin, urlparse from markdown.extensions import Extension +from markdown.postprocessors import AndSubstitutePostprocessor from markdown.treeprocessors import Treeprocessor from wiki.models import URLPath @@ -48,11 +50,21 @@ class LinkTreeprocessor(Treeprocessor): return self._my_urlpath def get_class(self, el): + href = el.get("href") + if not href: + return + # The autolinker turns email links into links with many HTML entities. + # These entities are further escaped using markdown-specific codes. + # First unescape the markdown-specific, then use html.unescape. + href = AndSubstitutePostprocessor().run(href) + href = html.unescape(href) try: - url = urlparse(el.get("href")) + url = urlparse(href) except ValueError: return - if url.netloc or url.path.startswith("/"): + if url.scheme == "mailto": + return + if url.scheme or url.netloc or url.path.startswith("/"): # Contains a hostname or is an absolute link => external return self.external_class # Ensure that path ends with a slash diff --git a/tests/plugins/redlinks/test_redlinks.py b/tests/plugins/redlinks/test_redlinks.py index cf37d612..53a90efc 100644 --- a/tests/plugins/redlinks/test_redlinks.py +++ b/tests/plugins/redlinks/test_redlinks.py @@ -52,3 +52,12 @@ class RedlinksTests(RequireRootArticleMixin, TestBase): self.assertNotIn("wiki-internal", html) self.assertNotIn("wiki-external", html) self.assertIn("wiki-broken", html) + + def test_mailto(self): + md = markdown.ArticleMarkdown(article=self.root.article) + md_text = "" + html = md.convert(md_text) + self.assertNotIn("wiki-internal", html) + self.assertNotIn("wiki-external", html) + self.assertNotIn("wiki-broken", html) + self.assertIn("