bugdown: Rewrite links to local server to be relative links.

Fixes #7247
This commit is contained in:
Tommy Ip
2017-10-31 21:03:39 +00:00
committed by Tim Abbott
parent 982bd9c38d
commit bcd575d8de
2 changed files with 37 additions and 1 deletions

View File

@@ -982,15 +982,26 @@ def url_to_a(url, text = None):
a = markdown.util.etree.Element('a')
href = sanitize_url(url)
target_blank = True
if href is None:
# Rejected by sanitize_url; render it as plain text.
return url
if text is None:
text = markdown.util.AtomicString(url)
target_blank = 'mailto:' not in href[:7]
if db_data:
# If the link points to a local destination we can just switch to that
# instead of opening a new tab.
local_link = re.match("^{}\/(#.+)$".format(re.escape(db_data['realm_uri'])), url)
if local_link:
href = local_link.group(1)
target_blank = False
a.set('href', href)
a.text = text
fixup_link(a, 'mailto:' not in href[:7])
fixup_link(a, target_blank)
return a
class VerbosePattern(markdown.inlinepatterns.Pattern):
@@ -1719,6 +1730,7 @@ def do_convert(content, message=None, message_realm=None, possible_words=None, s
'email_info': email_info,
'mention_data': mention_data,
'realm_emoji': realm_emoji,
'realm_uri': message_realm.uri,
'sent_by_bot': sent_by_bot,
'stream_names': stream_name_info,
}

View File

@@ -1168,6 +1168,30 @@ class BugdownTest(ZulipTestCase):
'<p>I am writing this message to test something. I am writing this message to test something.</p>'
self.assertEqual(converted, expected_output)
def test_normal_link(self):
# type: () -> None
realm = get_realm("zulip")
sender_user_profile = self.example_user('othello')
message = Message(sender=sender_user_profile, sending_client=get_client("test"))
msg = "http://example.com/#settings/"
self.assertEqual(
bugdown.convert(msg, message_realm=realm, message=message),
'<p><a href="http://example.com/#settings/" target="_blank" title="http://example.com/#settings/">http://example.com/#settings/</a></p>'
)
def test_relative_link(self):
# type: () -> None
realm = get_realm("zulip")
sender_user_profile = self.example_user('othello')
message = Message(sender=sender_user_profile, sending_client=get_client("test"))
msg = "http://zulip.testserver/#narrow/stream/hello"
self.assertEqual(
bugdown.convert(msg, message_realm=realm, message=message),
'<p><a href="#narrow/stream/hello" title="#narrow/stream/hello">http://zulip.testserver/#narrow/stream/hello</a></p>'
)
class BugdownApiTests(ZulipTestCase):
def test_render_message_api(self):
# type: () -> None