markdown: Only attempt to adjust /wiki/File: paths on Wikipedia.

This commit is contained in:
Alex Vandiver 2023-07-06 22:49:43 +00:00 committed by Tim Abbott
parent 533f929591
commit ff53ee8e28
2 changed files with 10 additions and 4 deletions

View File

@ -698,13 +698,14 @@ class InlineInterestingLinkProcessor(markdown.treeprocessors.Treeprocessor):
# structurally very similar to dropbox_image, and possibly # structurally very similar to dropbox_image, and possibly
# should be rewritten to use open graph, but has some value. # should be rewritten to use open graph, but has some value.
parsed_url = urllib.parse.urlparse(url) 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:" # Redirecting from "/wiki/File:" to "/wiki/Special:FilePath/File:"
# A possible alternative, that avoids the redirect after hitting "Special:" # A possible alternative, that avoids the redirect after hitting "Special:"
# is using the first characters of md5($filename) to generate the URL # is using the first characters of md5($filename) to generate the URL
domain = parsed_url.scheme + "://" + parsed_url.netloc newpath = parsed_url.path.replace("/wiki/File:", "/wiki/Special:FilePath/File:", 1)
correct_url = domain + parsed_url.path[:6] + "Special:FilePath" + parsed_url.path[5:] return parsed_url._replace(path=newpath).geturl()
return correct_url
if parsed_url.netloc == "linx.li": if parsed_url.netloc == "linx.li":
return "https://linx.li/s" + parsed_url.path return "https://linx.li/s" + parsed_url.path
return None return None

View File

@ -757,6 +757,11 @@ class MarkdownTest(ZulipTestCase):
converted = render_markdown(msg, content) converted = render_markdown(msg, content)
self.assertEqual(converted.rendered_content, expected) 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&amp;size=full" src="/thumbnail?url=https%3A%2F%2Fen.wikipedia.org%2Fstatic%2Fimages%2Ficons%2Fwikipedia.png&amp;size=thumbnail"></a></div>'
converted = render_markdown(msg, content)
self.assertEqual(converted.rendered_content, expected)
@override_settings(INLINE_IMAGE_PREVIEW=False) @override_settings(INLINE_IMAGE_PREVIEW=False)
def test_image_preview_enabled(self) -> None: def test_image_preview_enabled(self) -> None:
ret = image_preview_enabled() ret = image_preview_enabled()