From ff53ee8e28ff84e335c42585b1d165dc48323619 Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Thu, 6 Jul 2023 22:49:43 +0000 Subject: [PATCH] markdown: Only attempt to adjust /wiki/File: paths on Wikipedia. --- zerver/lib/markdown/__init__.py | 9 +++++---- zerver/tests/test_markdown.py | 5 +++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py index d10a0a006d..ea59d57f30 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.path return None diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.py index 5759e9bae2..f4f1a6214f 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 = '
' + 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()