Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
markdown: Only attempt to adjust /wiki/File: paths on Wikipedia.
Alex Vandiverff53ee8e28ff
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.pyindex d10a0a006d9..ea59d57f30c 100644--- a/zerver/lib/markdown/__init__.py+++ b/zerver/lib/markdown/__init__.py@@ -698,13 +698,14 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):# structurally very similar to dropbox_image, and possibly# should be rewritten to use open graph, but has some value.parsed_url = urllib.parse.urlparse(url)- if parsed_url.netloc.lower().endswith(".wikipedia.org"):+ if parsed_url.netloc.lower().endswith(".wikipedia.org") and parsed_url.path.startswith(+ "/wiki/File:"+ ):# Redirecting from "/wiki/File:" to "/wiki/Special:FilePath/File:"# A possible alternative, that avoids the redirect after hitting "Special:"# is using the first characters of md5($filename) to generate the URL- domain = parsed_url.scheme + "://" + parsed_url.netloc- correct_url = domain + parsed_url.path[:6] + "Special:FilePath" + parsed_url.path[5:]- return correct_url+ newpath = parsed_url.path.replace("/wiki/File:", "/wiki/Special:FilePath/File:", 1)+ return parsed_url._replace(path=newpath).geturl()if parsed_url.netloc == "linx.li":return "https://linx.li/s" + parsed_url.pathreturn Nonediff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.pyindex 5759e9bae26..f4f1a6214f6 100644--- a/zerver/tests/test_markdown.py+++ b/zerver/tests/test_markdown.py@@ -757,6 +757,11 @@ class MarkdownTest(ZulipTestCase):converted = render_markdown(msg, content)self.assertEqual(converted.rendered_content, expected)+ content = "https://en.wikipedia.org/static/images/icons/wikipedia.png"+ expected = '<div class="message_inline_image"><a href="https://en.wikipedia.org/static/images/icons/wikipedia.png"><img data-src-fullsize="/thumbnail?url=https%3A%2F%2Fen.wikipedia.org%2Fstatic%2Fimages%2Ficons%2Fwikipedia.png&size=full" src="/thumbnail?url=https%3A%2F%2Fen.wikipedia.org%2Fstatic%2Fimages%2Ficons%2Fwikipedia.png&size=thumbnail"></a></div>'+ converted = render_markdown(msg, content)+ self.assertEqual(converted.rendered_content, expected)+@override_settings(INLINE_IMAGE_PREVIEW=False)def test_image_preview_enabled(self) -> None:ret = image_preview_enabled()