2016-04-01 06:58:14 +02:00
|
|
|
// This contains zulip's frontend markdown implementation; see
|
|
|
|
// docs/markdown.md for docs on our Markdown syntax.
|
|
|
|
|
2013-12-04 17:16:08 +01:00
|
|
|
var echo = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
var waiting_for_id = {};
|
|
|
|
var waiting_for_ack = {};
|
2014-01-06 23:42:52 +01:00
|
|
|
var realm_filter_map = {};
|
2014-01-24 21:48:56 +01:00
|
|
|
var realm_filter_list = [];
|
2014-01-16 20:18:55 +01:00
|
|
|
var home_view_loaded = false;
|
2013-12-19 17:03:08 +01:00
|
|
|
|
2013-12-04 17:16:08 +01:00
|
|
|
// Regexes that match some of our common bugdown markup
|
|
|
|
var bugdown_re = [
|
|
|
|
// Inline image previews, check for contiguous chars ending in image suffix
|
|
|
|
// To keep the below regexes simple, split them out for the end-of-message case
|
|
|
|
/[^\s]*(?:\.bmp|\.gif|\.jpg|\.jpeg|\.png|\.webp)\s+/m,
|
|
|
|
/[^\s]*(?:\.bmp|\.gif|\.jpg|\.jpeg|\.png|\.webp)$/m,
|
|
|
|
// Twitter and youtube links are given previews
|
2017-01-12 00:17:43 +01:00
|
|
|
/[^\s]*(?:twitter|youtube).com\/[^\s]*/,
|
2013-12-04 17:16:08 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
exports.contains_bugdown = function contains_bugdown(content) {
|
|
|
|
// Try to guess whether or not a message has bugdown in it
|
|
|
|
// If it doesn't, we can immediately render it client-side
|
|
|
|
var markedup = _.find(bugdown_re, function (re) {
|
|
|
|
return re.test(content);
|
|
|
|
});
|
|
|
|
return markedup !== undefined;
|
|
|
|
};
|
|
|
|
|
2017-01-20 16:40:45 +01:00
|
|
|
function push_uniquely(lst, elem) {
|
|
|
|
if (!_.contains(lst, elem)) {
|
|
|
|
lst.push(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 15:21:28 +01:00
|
|
|
exports.apply_markdown = function apply_markdown(message) {
|
2017-01-20 16:40:45 +01:00
|
|
|
if (message.flags === undefined) {
|
|
|
|
message.flags = [];
|
|
|
|
}
|
|
|
|
|
2014-01-27 23:21:48 +01:00
|
|
|
// Our python-markdown processor appends two \n\n to input
|
2017-01-20 15:00:07 +01:00
|
|
|
var options = {
|
|
|
|
userMentionHandler: function (name) {
|
|
|
|
var person = people.get_by_name(name);
|
|
|
|
if (person !== undefined) {
|
2017-01-24 00:44:08 +01:00
|
|
|
if (people.is_my_user_id(person.user_id)) {
|
|
|
|
push_uniquely(message.flags, 'mentioned');
|
|
|
|
}
|
2017-01-20 17:43:45 +01:00
|
|
|
return '<span class="user-mention" data-user-id="' + person.user_id + '">' +
|
2017-01-20 15:00:07 +01:00
|
|
|
'@' + person.full_name +
|
|
|
|
'</span>';
|
|
|
|
} else if (name === 'all' || name === 'everyone') {
|
2017-01-20 16:40:45 +01:00
|
|
|
push_uniquely(message.flags, 'mentioned');
|
2017-01-20 17:43:45 +01:00
|
|
|
return '<span class="user-mention" data-user-id="*">' +
|
2017-01-20 15:00:07 +01:00
|
|
|
'@' + name +
|
|
|
|
'</span>';
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
message.content = marked(message.raw_content + '\n\n', options).trim();
|
2013-12-19 17:03:08 +01:00
|
|
|
};
|
|
|
|
|
2014-01-03 20:39:12 +01:00
|
|
|
function resend_message(message, row) {
|
|
|
|
message.content = message.raw_content;
|
|
|
|
var retry_spinner = row.find('.refresh-failed-message');
|
|
|
|
retry_spinner.toggleClass('rotating', true);
|
|
|
|
// Always re-set queue_id if we've gotten a new one
|
|
|
|
// since the time when the message object was initially created
|
|
|
|
message.queue_id = page_params.event_queue_id;
|
|
|
|
var start_time = new Date();
|
|
|
|
compose.transmit_message(message, function success(data) {
|
|
|
|
retry_spinner.toggleClass('rotating', false);
|
|
|
|
|
|
|
|
var message_id = data.id;
|
|
|
|
|
|
|
|
retry_spinner.toggleClass('rotating', false);
|
2014-01-16 21:38:30 +01:00
|
|
|
compose.send_message_success(message.local_id, message_id, start_time, true);
|
2014-01-03 20:39:12 +01:00
|
|
|
|
|
|
|
// Resend succeeded, so mark as no longer failed
|
2014-01-31 22:14:57 +01:00
|
|
|
message_store.get(message_id).failed_request = false;
|
2014-01-03 20:39:12 +01:00
|
|
|
ui.show_failed_message_success(message_id);
|
|
|
|
}, function error(response) {
|
|
|
|
exports.message_send_error(message.local_id, response);
|
|
|
|
retry_spinner.toggleClass('rotating', false);
|
|
|
|
blueslip.log("Manual resend of message failed");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
function truncate_precision(float) {
|
|
|
|
return parseFloat(float.toFixed(3));
|
|
|
|
}
|
|
|
|
|
2014-01-08 20:39:56 +01:00
|
|
|
function add_message_flags(message) {
|
2017-01-20 16:40:45 +01:00
|
|
|
// Note: mention flags are set in apply_markdown()
|
2014-01-08 20:39:56 +01:00
|
|
|
|
2014-01-27 17:06:59 +01:00
|
|
|
if (message.raw_content.indexOf('/me ') === 0 &&
|
|
|
|
message.content.indexOf('<p>') === 0 &&
|
|
|
|
message.content.lastIndexOf('</p>') === message.content.length - 4) {
|
2017-01-20 16:40:45 +01:00
|
|
|
message.flags.push('is_me_message');
|
2014-01-27 17:06:59 +01:00
|
|
|
}
|
2014-01-08 20:39:56 +01:00
|
|
|
}
|
|
|
|
|
2014-01-24 21:48:56 +01:00
|
|
|
function add_subject_links(message) {
|
|
|
|
var subject = message.subject;
|
|
|
|
var links = [];
|
|
|
|
_.each(realm_filter_list, function (realm_filter) {
|
|
|
|
var pattern = realm_filter[0];
|
|
|
|
var url = realm_filter[1];
|
|
|
|
var match;
|
|
|
|
while ((match = pattern.exec(subject)) !== null) {
|
|
|
|
var link_url = url;
|
2016-11-24 08:28:16 +01:00
|
|
|
var matched_groups = match.slice(1);
|
|
|
|
var i = 0;
|
|
|
|
while (i < matched_groups.length) {
|
|
|
|
var matched_group = matched_groups[i];
|
|
|
|
var current_group = i + 1;
|
2014-01-24 21:48:56 +01:00
|
|
|
var back_ref = "\\" + current_group;
|
|
|
|
link_url = link_url.replace(back_ref, matched_group);
|
2016-11-30 19:05:04 +01:00
|
|
|
i += 1;
|
2016-11-24 08:28:16 +01:00
|
|
|
}
|
2014-01-24 21:48:56 +01:00
|
|
|
links.push(link_url);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
message.subject_links = links;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For unit testing
|
|
|
|
exports._add_subject_links = add_subject_links;
|
2014-01-27 17:06:59 +01:00
|
|
|
exports._add_message_flags = add_message_flags;
|
2014-01-24 21:48:56 +01:00
|
|
|
|
2014-01-16 20:18:55 +01:00
|
|
|
function get_next_local_id() {
|
2013-12-19 17:03:08 +01:00
|
|
|
var local_id_increment = 0.01;
|
2014-01-16 20:18:55 +01:00
|
|
|
var latest = page_params.max_message_id;
|
2016-04-21 22:49:23 +02:00
|
|
|
if (typeof message_list.all !== 'undefined' && message_list.all.last() !== undefined) {
|
|
|
|
latest = message_list.all.last().id;
|
2013-12-19 17:03:08 +01:00
|
|
|
}
|
2014-02-03 19:02:45 +01:00
|
|
|
latest = Math.max(0, latest);
|
|
|
|
return truncate_precision(latest + local_id_increment);
|
2014-01-16 20:18:55 +01:00
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
|
2014-01-16 20:18:55 +01:00
|
|
|
function insert_local_message(message_request, local_id) {
|
2013-12-19 17:03:08 +01:00
|
|
|
// Shallow clone of message request object that is turned into something suitable
|
|
|
|
// for zulip.js:add_message
|
|
|
|
// Keep this in sync with changes to compose.create_message_object
|
|
|
|
var message = $.extend({}, message_request);
|
2017-01-20 16:40:45 +01:00
|
|
|
|
|
|
|
// Locally delivered messages cannot be unread (since we sent them), nor
|
|
|
|
// can they alert the user.
|
|
|
|
message.flags = ['read']; // we may add more flags later
|
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
message.raw_content = message.content;
|
|
|
|
// NOTE: This will parse synchronously. We're not using the async pipeline
|
2017-01-20 15:21:28 +01:00
|
|
|
exports.apply_markdown(message);
|
2013-12-19 17:03:08 +01:00
|
|
|
message.content_type = 'text/html';
|
2017-01-20 18:37:46 +01:00
|
|
|
message.sender_email = people.my_current_email();
|
2017-01-20 23:49:20 +01:00
|
|
|
message.sender_full_name = people.my_full_name();
|
2013-12-19 17:03:08 +01:00
|
|
|
message.avatar_url = page_params.avatar_url;
|
|
|
|
message.timestamp = new XDate().getTime() / 1000;
|
2014-01-16 20:18:55 +01:00
|
|
|
message.local_id = local_id;
|
2013-12-19 17:03:08 +01:00
|
|
|
message.id = message.local_id;
|
2014-01-08 20:39:56 +01:00
|
|
|
add_message_flags(message);
|
2014-01-24 21:48:56 +01:00
|
|
|
add_subject_links(message);
|
2013-12-19 17:03:08 +01:00
|
|
|
|
|
|
|
waiting_for_id[message.local_id] = message;
|
|
|
|
waiting_for_ack[message.local_id] = message;
|
|
|
|
|
|
|
|
if (message.type === 'stream') {
|
|
|
|
message.display_recipient = message.stream;
|
|
|
|
} else {
|
2016-11-15 06:40:19 +01:00
|
|
|
// Build a display recipient with the full names of each
|
|
|
|
// recipient. Note that it's important that use
|
|
|
|
// util.extract_pm_recipients, which filters out any spurious
|
|
|
|
// ", " at the end of the recipient list
|
|
|
|
var emails = util.extract_pm_recipients(message_request.private_message_recipient);
|
2013-12-19 17:03:08 +01:00
|
|
|
message.display_recipient = _.map(emails, function (email) {
|
|
|
|
email = email.trim();
|
2014-01-30 22:42:19 +01:00
|
|
|
var person = people.get_by_email(email);
|
2016-11-03 20:12:38 +01:00
|
|
|
if (person === undefined) {
|
|
|
|
// For unknown users, we return a skeleton object.
|
|
|
|
return {email: email, full_name: email,
|
|
|
|
unknown_local_echo_user: true};
|
2013-12-19 17:03:08 +01:00
|
|
|
}
|
2016-12-02 21:34:35 +01:00
|
|
|
// NORMAL PATH
|
|
|
|
return person;
|
2013-12-19 17:03:08 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-08 00:07:57 +01:00
|
|
|
message_store.insert_new_messages([message], local_id);
|
2013-12-19 17:03:08 +01:00
|
|
|
return message.local_id;
|
2014-01-16 20:18:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.try_deliver_locally = function try_deliver_locally(message_request) {
|
|
|
|
var next_local_id = get_next_local_id();
|
|
|
|
if (next_local_id % 1 === 0) {
|
|
|
|
blueslip.error("Incremented local id to next integer---100 local messages queued");
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exports.contains_bugdown(message_request.content)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2014-01-24 17:15:35 +01:00
|
|
|
if (narrow.active() && !narrow.filter().can_apply_locally()) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2014-01-16 20:18:55 +01:00
|
|
|
return insert_local_message(message_request, next_local_id);
|
2013-12-19 17:03:08 +01:00
|
|
|
};
|
|
|
|
|
2014-01-02 19:39:22 +01:00
|
|
|
exports.edit_locally = function edit_locally(message, raw_content, new_topic) {
|
|
|
|
message.raw_content = raw_content;
|
|
|
|
if (new_topic !== undefined) {
|
2016-10-28 17:31:18 +02:00
|
|
|
stream_data.process_message_for_recent_topics(message, true);
|
2014-01-02 19:39:22 +01:00
|
|
|
message.subject = new_topic;
|
2016-10-28 17:31:18 +02:00
|
|
|
stream_data.process_message_for_recent_topics(message);
|
2014-01-02 19:39:22 +01:00
|
|
|
}
|
|
|
|
|
2017-01-20 15:21:28 +01:00
|
|
|
exports.apply_markdown(message);
|
|
|
|
|
2014-01-02 19:39:22 +01:00
|
|
|
// We don't handle unread counts since local messages must be sent by us
|
|
|
|
|
2014-03-04 16:47:44 +01:00
|
|
|
home_msg_list.view.rerender_messages([message]);
|
2016-04-25 23:45:25 +02:00
|
|
|
if (current_msg_list === message_list.narrowed) {
|
|
|
|
message_list.narrowed.view.rerender_messages([message]);
|
2014-01-02 19:39:22 +01:00
|
|
|
}
|
|
|
|
stream_list.update_streams_sidebar();
|
2016-11-29 16:53:43 +01:00
|
|
|
pm_list.update_private_messages();
|
2014-01-02 19:39:22 +01:00
|
|
|
};
|
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
exports.reify_message_id = function reify_message_id(local_id, server_id) {
|
|
|
|
var message = waiting_for_id[local_id];
|
|
|
|
delete waiting_for_id[local_id];
|
|
|
|
|
|
|
|
// reify_message_id is called both on receiving a self-sent message
|
|
|
|
// from the server, and on receiving the response to the send request
|
|
|
|
// Reification is only needed the first time the server id is found
|
|
|
|
if (message === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
message.id = server_id;
|
|
|
|
delete message.local_id;
|
|
|
|
|
|
|
|
// We have the real message ID for this message
|
|
|
|
$(document).trigger($.Event('message_id_changed', {old_id: local_id, new_id: server_id}));
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.process_from_server = function process_from_server(messages) {
|
|
|
|
var updated = false;
|
|
|
|
var locally_processed_ids = [];
|
2014-02-11 16:19:42 +01:00
|
|
|
var msgs_to_rerender = [];
|
2013-12-19 17:03:08 +01:00
|
|
|
messages = _.filter(messages, function (message) {
|
|
|
|
// In case we get the sent message before we get the send ACK, reify here
|
|
|
|
exports.reify_message_id(message.local_id, message.id);
|
|
|
|
|
|
|
|
var client_message = waiting_for_ack[message.local_id];
|
|
|
|
if (client_message !== undefined) {
|
|
|
|
if (client_message.content !== message.content) {
|
|
|
|
client_message.content = message.content;
|
|
|
|
updated = true;
|
2014-01-16 21:38:30 +01:00
|
|
|
compose.mark_rendered_content_disparity(message.id, true);
|
2013-12-19 17:03:08 +01:00
|
|
|
}
|
2014-02-11 16:19:42 +01:00
|
|
|
msgs_to_rerender.push(client_message);
|
2013-12-19 17:03:08 +01:00
|
|
|
locally_processed_ids.push(client_message.id);
|
2014-01-31 22:08:58 +01:00
|
|
|
compose.report_as_received(client_message);
|
2013-12-19 17:03:08 +01:00
|
|
|
delete waiting_for_ack[client_message.id];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (updated) {
|
2014-02-11 16:19:42 +01:00
|
|
|
home_msg_list.view.rerender_messages(msgs_to_rerender);
|
2016-04-25 23:45:25 +02:00
|
|
|
if (current_msg_list === message_list.narrowed) {
|
|
|
|
message_list.narrowed.view.rerender_messages(msgs_to_rerender);
|
2013-12-19 17:03:08 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_.each(locally_processed_ids, function (id) {
|
|
|
|
ui.show_local_message_arrived(id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return messages;
|
|
|
|
};
|
|
|
|
|
2014-01-02 17:34:16 +01:00
|
|
|
exports.message_send_error = function message_send_error(local_id, error_response) {
|
2013-12-19 17:03:08 +01:00
|
|
|
// Error sending message, show inline
|
2014-01-31 22:14:57 +01:00
|
|
|
message_store.get(local_id).failed_request = true;
|
2014-01-02 17:34:16 +01:00
|
|
|
ui.show_message_failed(local_id, error_response);
|
2013-12-19 17:03:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function abort_message(message) {
|
|
|
|
// Remove in all lists in which it exists
|
2016-04-21 22:49:23 +02:00
|
|
|
_.each([message_list.all, home_msg_list, current_msg_list], function (msg_list) {
|
2013-12-19 17:03:08 +01:00
|
|
|
msg_list.remove_and_rerender([message]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-01-02 19:39:22 +01:00
|
|
|
function edit_failed_message(message) {
|
|
|
|
message_edit.start_local_failed_edit(current_msg_list.get_row(message.local_id), message);
|
|
|
|
}
|
|
|
|
|
2014-01-03 22:51:00 +01:00
|
|
|
|
|
|
|
function escape(html, encode) {
|
|
|
|
return html
|
|
|
|
.replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
2014-01-08 20:40:54 +01:00
|
|
|
.replace(/"/g, '"')
|
2014-01-03 22:51:00 +01:00
|
|
|
.replace(/'/g, ''');
|
|
|
|
}
|
|
|
|
|
2016-06-24 22:39:44 +02:00
|
|
|
function handleUnicodeEmoji(unicode_emoji) {
|
|
|
|
var hex_value = unicode_emoji.codePointAt(0).toString(16);
|
2016-06-29 22:16:59 +02:00
|
|
|
if (emoji.emojis_by_unicode.hasOwnProperty(hex_value)) {
|
|
|
|
var emoji_url = emoji.emojis_by_unicode[hex_value];
|
2016-06-24 22:39:44 +02:00
|
|
|
return '<img alt="' + unicode_emoji + '"' +
|
|
|
|
' class="emoji" src="' + emoji_url + '"' +
|
|
|
|
' title="' + unicode_emoji + '">';
|
|
|
|
}
|
2016-12-02 21:34:35 +01:00
|
|
|
return unicode_emoji;
|
2016-06-24 22:39:44 +02:00
|
|
|
}
|
|
|
|
|
2014-01-04 00:21:06 +01:00
|
|
|
function handleEmoji(emoji_name) {
|
|
|
|
var input_emoji = ':' + emoji_name + ":";
|
|
|
|
if (emoji.emojis_by_name.hasOwnProperty(emoji_name)) {
|
|
|
|
var emoji_url = emoji.emojis_by_name[emoji_name];
|
|
|
|
return '<img alt="' + input_emoji + '"' +
|
|
|
|
' class="emoji" src="' + emoji_url + '"' +
|
|
|
|
' title="' + input_emoji + '">';
|
|
|
|
}
|
2016-12-02 21:34:35 +01:00
|
|
|
return input_emoji;
|
2014-01-04 00:21:06 +01:00
|
|
|
}
|
|
|
|
|
2016-10-19 16:49:53 +02:00
|
|
|
function handleAvatar(email) {
|
|
|
|
return '<img alt="' + email + '"' +
|
|
|
|
' class="message_body_gravatar" src="/avatar/' + email + '?s=30"' +
|
|
|
|
' title="' + email + '">';
|
|
|
|
}
|
|
|
|
|
2016-11-10 20:20:40 +01:00
|
|
|
function handleStream(streamName) {
|
|
|
|
var stream = stream_data.get_sub(streamName);
|
|
|
|
if (stream === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return '<a class="stream" data-stream-id="' + stream.stream_id + '" ' +
|
|
|
|
'href="' + window.location.origin + '/#narrow/stream/' +
|
|
|
|
hashchange.encodeHashComponent(stream.name) + '"' +
|
|
|
|
'>' + '#' + stream.name + '</a>';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-06 23:42:52 +01:00
|
|
|
function handleRealmFilter(pattern, matches) {
|
|
|
|
var url = realm_filter_map[pattern];
|
|
|
|
|
|
|
|
var current_group = 1;
|
|
|
|
_.each(matches, function (match) {
|
|
|
|
var back_ref = "\\" + current_group;
|
|
|
|
url = url.replace(back_ref, match);
|
2016-11-30 19:05:04 +01:00
|
|
|
current_group += 1;
|
2014-01-06 23:42:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
function python_to_js_filter(pattern, url) {
|
|
|
|
// Converts a python named-group regex to a javascript-compatible numbered
|
|
|
|
// group regex... with a regex!
|
|
|
|
var named_group_re = /\(?P<([^>]+?)>/g;
|
|
|
|
var match = named_group_re.exec(pattern);
|
|
|
|
var current_group = 1;
|
|
|
|
while (match) {
|
|
|
|
var name = match[1];
|
|
|
|
// Replace named group with regular matching group
|
|
|
|
pattern = pattern.replace('(?P<' + name + '>', '(');
|
|
|
|
// Replace named reference in url to numbered reference
|
|
|
|
url = url.replace('%(' + name + ')s', '\\' + current_group);
|
|
|
|
|
|
|
|
match = named_group_re.exec(pattern);
|
2014-01-24 22:02:42 +01:00
|
|
|
|
2016-11-30 19:05:04 +01:00
|
|
|
current_group += 1;
|
2014-01-06 23:42:52 +01:00
|
|
|
}
|
2014-01-27 18:22:38 +01:00
|
|
|
// Convert any python in-regex flags to RegExp flags
|
|
|
|
var js_flags = 'g';
|
|
|
|
var inline_flag_re = /\(\?([iLmsux]+)\)/;
|
|
|
|
match = inline_flag_re.exec(pattern);
|
|
|
|
|
|
|
|
// JS regexes only support i (case insensitivity) and m (multiline)
|
|
|
|
// flags, so keep those and ignore the rest
|
|
|
|
if (match) {
|
|
|
|
var py_flags = match[1].split("");
|
|
|
|
_.each(py_flags, function (flag) {
|
|
|
|
if ("im".indexOf(flag) !== -1) {
|
|
|
|
js_flags += flag;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
pattern = pattern.replace(inline_flag_re, "");
|
|
|
|
}
|
|
|
|
return [new RegExp(pattern, js_flags), url];
|
2014-01-06 23:42:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.set_realm_filters = function set_realm_filters(realm_filters) {
|
|
|
|
// Update the marked parser with our particular set of realm filters
|
2014-01-27 18:21:58 +01:00
|
|
|
if (!feature_flags.local_echo) {
|
2014-01-27 17:47:07 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-01-27 18:21:58 +01:00
|
|
|
|
2014-01-06 23:42:52 +01:00
|
|
|
realm_filter_map = {};
|
2014-01-24 21:48:56 +01:00
|
|
|
realm_filter_list = [];
|
2014-01-06 23:42:52 +01:00
|
|
|
|
|
|
|
var marked_rules = [];
|
|
|
|
_.each(realm_filters, function (realm_filter) {
|
|
|
|
var pattern = realm_filter[0];
|
|
|
|
var url = realm_filter[1];
|
|
|
|
var js_filters = python_to_js_filter(pattern, url);
|
|
|
|
|
|
|
|
realm_filter_map[js_filters[0]] = js_filters[1];
|
2014-01-24 21:48:56 +01:00
|
|
|
realm_filter_list.push([js_filters[0], js_filters[1]]);
|
2014-01-06 23:42:52 +01:00
|
|
|
marked_rules.push(js_filters[0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
marked.InlineLexer.rules.zulip.realm_filters = marked_rules;
|
|
|
|
};
|
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
$(function () {
|
|
|
|
function disable_markdown_regex(rules, name) {
|
2016-12-02 14:06:06 +01:00
|
|
|
rules[name] = {exec: function () {
|
2013-12-19 17:03:08 +01:00
|
|
|
return false;
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2013-12-19 17:03:08 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure the marked markdown parser for our usage
|
|
|
|
var r = new marked.Renderer();
|
|
|
|
|
2016-12-02 15:16:33 +01:00
|
|
|
// No <code> around our code blocks instead a codehilite <div> and disable class-specific
|
|
|
|
// highlighting. We special-case the 'quote' language and output a blockquote.
|
2014-01-03 22:51:00 +01:00
|
|
|
r.code = function (code, lang) {
|
|
|
|
if (lang === 'quote') {
|
2014-01-21 20:46:43 +01:00
|
|
|
return '<blockquote>\n<p>' + escape(code, true) + '</p>\n</blockquote>\n\n\n';
|
2014-01-03 22:51:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return '<div class="codehilite"><pre>'
|
|
|
|
+ escape(code, true)
|
2014-01-21 20:46:43 +01:00
|
|
|
+ '\n</pre></div>\n\n\n';
|
2014-01-03 22:51:00 +01:00
|
|
|
};
|
|
|
|
|
2014-01-07 22:13:46 +01:00
|
|
|
// Our links have title= and target=_blank
|
|
|
|
r.link = function (href, title, text) {
|
|
|
|
title = title || href;
|
|
|
|
var out = '<a href="' + href + '"' + ' target="_blank" title="' + title + '"' + '>' + text + '</a>';
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2014-01-08 20:40:54 +01:00
|
|
|
// Put a newline after a <br> in the generated HTML to match bugdown
|
|
|
|
r.br = function () {
|
|
|
|
return '<br>\n';
|
|
|
|
};
|
|
|
|
|
2014-01-27 23:21:48 +01:00
|
|
|
function preprocess_code_blocks(src) {
|
|
|
|
return fenced_code.process_fenced_code(src);
|
|
|
|
}
|
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
// Disable ordered lists
|
|
|
|
// We used GFM + tables, so replace the list start regex for that ruleset
|
|
|
|
// We remove the |[\d+]\. that matches the numbering in a numbered list
|
|
|
|
marked.Lexer.rules.tables.list = /^( *)((?:\*)) [\s\S]+?(?:\n+(?=(?: *[\-*_]){3,} *(?:\n+|$))|\n{2,}(?! )(?!\1(?:\*) )\n*|\s*$)/;
|
2014-01-04 00:16:40 +01:00
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
// Disable headings
|
|
|
|
disable_markdown_regex(marked.Lexer.rules.tables, 'heading');
|
|
|
|
disable_markdown_regex(marked.Lexer.rules.tables, 'lheading');
|
|
|
|
|
2016-11-03 07:56:28 +01:00
|
|
|
// Disable __strong__ (keeping **strong**)
|
2014-01-04 00:16:40 +01:00
|
|
|
marked.InlineLexer.rules.zulip.strong = /^\*\*([\s\S]+?)\*\*(?!\*)/;
|
2016-11-03 07:56:28 +01:00
|
|
|
|
2016-11-08 07:26:38 +01:00
|
|
|
// Make sure <del> syntax matches the backend processor
|
|
|
|
marked.InlineLexer.rules.zulip.del = /^(?!<\~)\~\~([^~]+)\~\~(?!\~)/;
|
2016-11-03 07:56:28 +01:00
|
|
|
|
|
|
|
// Disable _emphasis_ (keeping *emphasis*)
|
|
|
|
// Text inside ** must start and end with a word character
|
|
|
|
// it need for things like "const char *x = (char *)y"
|
|
|
|
marked.InlineLexer.rules.zulip.em = /^\*(?!\s+)((?:\*\*|[\s\S])+?)((?:[\S]))\*(?!\*)/;
|
|
|
|
|
2014-01-04 01:18:30 +01:00
|
|
|
// Disable autolink as (a) it is not used in our backend and (b) it interferes with @mentions
|
|
|
|
disable_markdown_regex(marked.InlineLexer.rules.zulip, 'autolink');
|
2013-12-19 17:03:08 +01:00
|
|
|
|
2014-01-06 23:42:52 +01:00
|
|
|
exports.set_realm_filters(page_params.realm_filters);
|
|
|
|
|
2014-01-27 23:21:48 +01:00
|
|
|
// Tell our fenced code preprocessor how to insert arbitrary
|
|
|
|
// HTML into the output. This generated HTML is safe to not escape
|
|
|
|
fenced_code.set_stash_func(function (html) {
|
|
|
|
return marked.stashHtml(html, true);
|
|
|
|
});
|
2014-02-03 17:13:40 +01:00
|
|
|
fenced_code.set_escape_func(escape);
|
2014-01-27 23:21:48 +01:00
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
marked.setOptions({
|
|
|
|
gfm: true,
|
|
|
|
tables: true,
|
|
|
|
breaks: true,
|
|
|
|
pedantic: false,
|
|
|
|
sanitize: true,
|
|
|
|
smartLists: true,
|
|
|
|
smartypants: false,
|
2014-01-04 00:16:40 +01:00
|
|
|
zulip: true,
|
2014-01-04 00:21:06 +01:00
|
|
|
emojiHandler: handleEmoji,
|
2016-10-19 16:49:53 +02:00
|
|
|
avatarHandler: handleAvatar,
|
2016-06-24 22:39:44 +02:00
|
|
|
unicodeEmojiHandler: handleUnicodeEmoji,
|
2016-11-10 20:20:40 +01:00
|
|
|
streamHandler: handleStream,
|
2014-01-06 23:42:52 +01:00
|
|
|
realmFilterHandler: handleRealmFilter,
|
2014-01-27 23:21:48 +01:00
|
|
|
renderer: r,
|
2017-01-12 00:17:43 +01:00
|
|
|
preprocessors: [preprocess_code_blocks],
|
2013-12-19 17:03:08 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
function on_failed_action(action, callback) {
|
|
|
|
$("#main_div").on("click", "." + action + "-failed-message", function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
popovers.hide_all();
|
2014-01-03 20:39:12 +01:00
|
|
|
var row = $(this).closest(".message_row");
|
|
|
|
var message_id = rows.id(row);
|
2013-12-19 17:03:08 +01:00
|
|
|
// Message should be waiting for ack and only have a local id,
|
|
|
|
// otherwise send would not have failed
|
|
|
|
var message = waiting_for_ack[message_id];
|
|
|
|
if (message === undefined) {
|
2014-02-05 05:07:13 +01:00
|
|
|
blueslip.warn("Got resend or retry on failure request but did not find message in ack list " + message_id);
|
2013-12-19 17:03:08 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-01-03 20:39:12 +01:00
|
|
|
callback(message, row);
|
2013-12-19 17:03:08 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
on_failed_action('remove', abort_message);
|
|
|
|
on_failed_action('refresh', resend_message);
|
2014-01-02 19:39:22 +01:00
|
|
|
on_failed_action('edit', edit_failed_message);
|
2014-01-16 20:18:55 +01:00
|
|
|
|
|
|
|
$(document).on('home_view_loaded.zulip', function () {
|
|
|
|
home_view_loaded = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('socket_loaded_requests.zulip', function (event, data) {
|
|
|
|
var msgs_to_insert = [];
|
|
|
|
|
|
|
|
var next_local_id = get_next_local_id();
|
2016-12-02 14:06:06 +01:00
|
|
|
_.each(data.requests, function (socket_msg) {
|
2014-01-16 20:18:55 +01:00
|
|
|
var msg = socket_msg.msg;
|
|
|
|
// Check for any message objects, then insert them locally
|
|
|
|
if (msg.stream === undefined || msg.local_id === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
msg.local_id = next_local_id;
|
|
|
|
msg.queue_id = page_params.event_queue_id;
|
|
|
|
|
|
|
|
next_local_id = truncate_precision(next_local_id + 0.01);
|
|
|
|
msgs_to_insert.push(msg);
|
|
|
|
});
|
|
|
|
|
|
|
|
function echo_pending_messages() {
|
|
|
|
_.each(msgs_to_insert, function (msg) {
|
|
|
|
insert_local_message(msg, msg.local_id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (home_view_loaded) {
|
|
|
|
echo_pending_messages();
|
|
|
|
} else {
|
|
|
|
$(document).on('home_view_loaded.zulip', function () {
|
|
|
|
echo_pending_messages();
|
|
|
|
});
|
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
});
|
|
|
|
|
2013-12-04 17:16:08 +01:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = echo;
|
|
|
|
}
|