render_message_backend: Pass realm data into bugdown.

Previously, the way that render_messages was calling bugdown meant
that the preview feature didn't have access to realm data like the
list of users or streams, resulting in previews for those elements
being wrong.

Now render_message_backend uses zerver.lib.render_markdown to render
messages correctly.

[Commit message tweaked and test added by tabbott]
This commit is contained in:
Mohsen Ibrahim 2016-11-07 21:40:40 +02:00 committed by Tim Abbott
parent ff1e97603d
commit e0351948ba
2 changed files with 20 additions and 1 deletions

View File

@ -843,6 +843,19 @@ class BugdownApiTests(ZulipTestCase):
self.assertEqual(data['rendered'],
u'<p>That is a <strong>bold</strong> statement</p>')
def test_render_message_api(self):
# type: () -> None
content = 'This mentions #**Denmark** and @**King Hamlet**.'
result = self.client_get(
'/api/v1/messages/render',
dict(content=content),
**self.api_auth('othello@zulip.com')
)
self.assert_json_success(result)
data = ujson.loads(result.content)
self.assertEqual(data['rendered'],
u'<p>This mentions <a class="stream" data-stream-id="3" href="/#narrow/stream/Denmark">#Denmark</a> and <span class="user-mention" data-user-email="hamlet@zulip.com">@King Hamlet</span>.</p>')
class BugdownErrorTests(ZulipTestCase):
def test_bugdown_error_handling(self):
# type: () -> None

View File

@ -29,6 +29,7 @@ from zerver.lib.message import (
access_message,
MessageDict,
extract_message_dict,
render_markdown,
stringify_message_dict,
)
from zerver.lib.response import json_success, json_error
@ -940,7 +941,12 @@ def json_fetch_raw_message(request, user_profile,
@has_request_variables
def render_message_backend(request, user_profile, content=REQ()):
# type: (HttpRequest, UserProfile, text_type) -> HttpResponse
rendered_content = bugdown.convert(content, user_profile.realm.domain)
message = Message()
message.sender = user_profile
message.content = content
message.sending_client = request.client
rendered_content = render_markdown(message, content, domain=user_profile.realm.domain)
return json_success({"rendered": rendered_content})
@authenticated_json_post_view