2017-05-09 18:01:43 +02:00
|
|
|
// This contains zulip's frontend markdown implementation; see
|
2017-11-08 17:55:36 +01:00
|
|
|
// docs/subsystems/markdown.md for docs on our Markdown syntax. The other
|
2017-05-09 18:01:43 +02:00
|
|
|
// main piece in rendering markdown client-side is
|
|
|
|
// static/third/marked/lib/marked.js, which we have significantly
|
|
|
|
// modified from the original implementation.
|
|
|
|
|
|
|
|
var markdown = (function () {
|
2018-11-30 00:48:13 +01:00
|
|
|
// Docs: https://zulip.readthedocs.io/en/latest/subsystems/markdown.html
|
2017-05-09 18:01:43 +02:00
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var realm_filter_map = {};
|
|
|
|
var realm_filter_list = [];
|
|
|
|
|
2018-03-29 00:25:58 +02:00
|
|
|
|
|
|
|
// Helper function
|
|
|
|
function escape(html, encode) {
|
2018-05-06 21:43:17 +02:00
|
|
|
return html
|
|
|
|
.replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/'/g, ''');
|
2018-03-29 00:25:58 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 18:01:43 +02:00
|
|
|
// Regexes that match some of our common bugdown markup
|
2017-07-29 02:48:47 +02:00
|
|
|
var backend_only_markdown_re = [
|
2017-05-09 18:01:43 +02:00
|
|
|
// 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
|
|
|
|
|
2017-12-27 19:46:57 +01:00
|
|
|
/[^\s]*(?:(?:\.bmp|\.gif|\.jpg|\.jpeg|\.png|\.webp)\)?)\s+/m,
|
|
|
|
/[^\s]*(?:(?:\.bmp|\.gif|\.jpg|\.jpeg|\.png|\.webp)\)?)$/m,
|
2017-05-09 18:01:43 +02:00
|
|
|
|
|
|
|
// Twitter and youtube links are given previews
|
|
|
|
|
|
|
|
/[^\s]*(?:twitter|youtube).com\/[^\s]*/,
|
|
|
|
];
|
|
|
|
|
2019-02-15 23:24:26 +01:00
|
|
|
// Helper function to update a mentioned user's name.
|
|
|
|
exports.set_name_in_mention_element = function (element, name) {
|
|
|
|
if ($(element).hasClass('silent')) {
|
|
|
|
$(element).text(name);
|
|
|
|
} else {
|
|
|
|
$(element).text("@" + name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-29 02:51:33 +02:00
|
|
|
exports.contains_backend_only_syntax = function (content) {
|
2017-05-09 18:01:43 +02:00
|
|
|
// Try to guess whether or not a message has bugdown in it
|
|
|
|
// If it doesn't, we can immediately render it client-side
|
2017-07-29 02:48:47 +02:00
|
|
|
var markedup = _.find(backend_only_markdown_re, function (re) {
|
2017-05-09 18:01:43 +02:00
|
|
|
return re.test(content);
|
|
|
|
});
|
2017-07-30 21:07:59 +02:00
|
|
|
|
|
|
|
// If a realm filter doesn't start with some specified characters
|
|
|
|
// then don't render it locally. It is workaround for the fact that
|
|
|
|
// javascript regex doesn't support lookbehind.
|
|
|
|
var false_filter_match = _.find(realm_filter_list, function (re) {
|
|
|
|
var pattern = /(?:[^\s'"\(,:<])/.source + re[0].source + /(?![\w])/.source;
|
|
|
|
var regex = new RegExp(pattern);
|
|
|
|
return regex.test(content);
|
|
|
|
});
|
|
|
|
return markedup !== undefined || false_filter_match !== undefined;
|
2017-05-09 18:01:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.apply_markdown = function (message) {
|
2017-12-16 23:25:31 +01:00
|
|
|
message_store.init_booleans(message);
|
2017-05-09 18:01:43 +02:00
|
|
|
|
|
|
|
// Our python-markdown processor appends two \n\n to input
|
|
|
|
var options = {
|
2019-01-08 09:30:19 +01:00
|
|
|
userMentionHandler: function (name, silently) {
|
2017-05-09 18:01:43 +02:00
|
|
|
var person = people.get_by_name(name);
|
2018-08-19 03:39:57 +02:00
|
|
|
|
|
|
|
var id_regex = /(.+)\|(\d+)$/g; // For @**user|id** syntax
|
|
|
|
var match = id_regex.exec(name);
|
|
|
|
if (match) {
|
|
|
|
if (people.is_known_user_id(match[2])) {
|
|
|
|
person = people.get_person_from_user_id(match[2]);
|
|
|
|
if (person.full_name !== match[1]) { // Invalid Syntax
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 18:01:43 +02:00
|
|
|
if (person !== undefined) {
|
2019-01-08 09:30:19 +01:00
|
|
|
if (people.is_my_user_id(person.user_id) && !silently) {
|
2017-12-16 23:25:31 +01:00
|
|
|
message.mentioned = true;
|
|
|
|
message.mentioned_me_directly = true;
|
2017-05-09 18:01:43 +02:00
|
|
|
}
|
2019-01-08 09:30:19 +01:00
|
|
|
var str = '';
|
|
|
|
if (silently) {
|
|
|
|
str += '<span class="user-mention silent" data-user-id="' + person.user_id + '">';
|
|
|
|
} else {
|
2019-02-15 20:58:54 +01:00
|
|
|
str += '<span class="user-mention" data-user-id="' + person.user_id + '">@';
|
2019-01-08 09:30:19 +01:00
|
|
|
}
|
2019-02-15 20:58:54 +01:00
|
|
|
return str + escape(person.full_name, true) + '</span>';
|
2018-04-03 17:55:57 +02:00
|
|
|
} else if (name === 'all' || name === 'everyone' || name === 'stream') {
|
2017-12-16 23:25:31 +01:00
|
|
|
message.mentioned = true;
|
2017-05-09 18:01:43 +02:00
|
|
|
return '<span class="user-mention" data-user-id="*">' +
|
|
|
|
'@' + name +
|
|
|
|
'</span>';
|
|
|
|
}
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-05-09 18:01:43 +02:00
|
|
|
},
|
2017-11-22 09:11:07 +01:00
|
|
|
groupMentionHandler: function (name) {
|
|
|
|
var group = user_groups.get_user_group_from_name(name);
|
|
|
|
if (group !== undefined) {
|
|
|
|
if (user_groups.is_member_of(group.id, people.my_current_user_id())) {
|
2017-12-16 23:25:31 +01:00
|
|
|
message.mentioned = true;
|
2017-11-22 09:11:07 +01:00
|
|
|
}
|
|
|
|
return '<span class="user-group-mention" data-user-group-id="' + group.id + '">' +
|
2018-03-29 00:25:58 +02:00
|
|
|
'@' + escape(group.name, true) +
|
2017-11-22 09:11:07 +01:00
|
|
|
'</span>';
|
|
|
|
}
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-11-22 09:11:07 +01:00
|
|
|
},
|
2019-01-08 11:30:13 +01:00
|
|
|
silencedMentionHandler: function (quote) {
|
|
|
|
// Silence quoted mentions.
|
2019-02-15 20:58:54 +01:00
|
|
|
var user_mention_re = /<span.*user-mention.*data-user-id="(\d+|\*)"[^>]*>@/gm;
|
2019-01-08 11:30:13 +01:00
|
|
|
quote = quote.replace(user_mention_re, function (match) {
|
2019-02-15 20:58:54 +01:00
|
|
|
match = match.replace(/"user-mention"/g, '"user-mention silent"');
|
|
|
|
match = match.replace(/>@/g, '>');
|
|
|
|
return match;
|
2019-01-08 11:30:13 +01:00
|
|
|
});
|
|
|
|
// In most cases, if you are being mentioned in the message you're quoting, you wouldn't
|
|
|
|
// mention yourself outside of the blockquote (and, above it). If that you do that, the
|
|
|
|
// following mentioned status is false; the backend rendering is authoritative and the
|
|
|
|
// only side effect is the lack red flash on immediately sending the message.
|
|
|
|
message.mentioned = false;
|
|
|
|
message.mentioned_me_directly = false;
|
|
|
|
return quote;
|
|
|
|
},
|
2017-05-09 18:01:43 +02:00
|
|
|
};
|
|
|
|
message.content = marked(message.raw_content + '\n\n', options).trim();
|
2018-01-21 19:27:36 +01:00
|
|
|
message.is_me_message = exports.is_status_message(message.raw_content, message.content);
|
2017-05-09 18:01:43 +02:00
|
|
|
};
|
|
|
|
|
2018-11-13 16:19:59 +01:00
|
|
|
exports.add_topic_links = function (message) {
|
2017-05-09 18:01:43 +02:00
|
|
|
if (message.type !== 'stream') {
|
2018-11-15 16:41:21 +01:00
|
|
|
util.set_topic_links(message, []);
|
2017-05-09 18:01:43 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-12-23 16:01:30 +01:00
|
|
|
var topic = util.get_message_topic(message);
|
2017-05-09 18:01:43 +02:00
|
|
|
var links = [];
|
|
|
|
_.each(realm_filter_list, function (realm_filter) {
|
|
|
|
var pattern = realm_filter[0];
|
|
|
|
var url = realm_filter[1];
|
|
|
|
var match;
|
2018-11-13 16:41:18 +01:00
|
|
|
while ((match = pattern.exec(topic)) !== null) {
|
2017-05-09 18:01:43 +02:00
|
|
|
var link_url = url;
|
|
|
|
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;
|
|
|
|
var back_ref = "\\" + current_group;
|
|
|
|
link_url = link_url.replace(back_ref, matched_group);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
links.push(link_url);
|
|
|
|
}
|
|
|
|
});
|
2019-05-25 16:10:30 +02:00
|
|
|
|
|
|
|
// Also make raw urls navigable
|
|
|
|
var url_re = /\b(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/g; // Slightly modified from third/marked.js
|
|
|
|
var match = topic.match(url_re);
|
|
|
|
if (match) {
|
|
|
|
links = links.concat(match);
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:41:21 +01:00
|
|
|
util.set_topic_links(message, links);
|
2017-05-09 18:01:43 +02:00
|
|
|
};
|
|
|
|
|
2018-01-21 19:27:36 +01:00
|
|
|
exports.is_status_message = function (raw_content, content) {
|
2018-06-06 18:19:09 +02:00
|
|
|
return raw_content.indexOf('/me ') === 0 &&
|
2018-01-21 19:27:36 +01:00
|
|
|
content.indexOf('<p>') === 0 &&
|
2018-12-29 11:07:27 +01:00
|
|
|
content.indexOf('</p>') !== -1;
|
2018-01-21 19:27:36 +01:00
|
|
|
};
|
|
|
|
|
2019-01-16 10:11:30 +01:00
|
|
|
function make_emoji_span(codepoint, title, alt_text) {
|
2019-01-14 08:45:37 +01:00
|
|
|
return '<span aria-label="' + title + '"' +
|
|
|
|
' class="emoji emoji-' + codepoint + '"' +
|
|
|
|
' role="img" title="' + title + '">' + alt_text +
|
2019-01-16 10:11:30 +01:00
|
|
|
'</span>';
|
|
|
|
}
|
|
|
|
|
2017-05-09 18:01:43 +02:00
|
|
|
function handleUnicodeEmoji(unicode_emoji) {
|
2017-09-27 19:39:42 +02:00
|
|
|
var codepoint = unicode_emoji.codePointAt(0).toString(16);
|
|
|
|
if (emoji_codes.codepoint_to_name.hasOwnProperty(codepoint)) {
|
|
|
|
var emoji_name = emoji_codes.codepoint_to_name[codepoint];
|
2017-06-09 10:30:24 +02:00
|
|
|
var alt_text = ':' + emoji_name + ':';
|
|
|
|
var title = emoji_name.split("_").join(" ");
|
2019-01-16 10:11:30 +01:00
|
|
|
return make_emoji_span(codepoint, title, alt_text);
|
2017-05-09 18:01:43 +02:00
|
|
|
}
|
|
|
|
return unicode_emoji;
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleEmoji(emoji_name) {
|
2017-09-27 19:39:42 +02:00
|
|
|
var alt_text = ':' + emoji_name + ':';
|
2017-06-09 10:30:24 +02:00
|
|
|
var title = emoji_name.split("_").join(" ");
|
2017-06-21 00:01:40 +02:00
|
|
|
if (emoji.active_realm_emojis.hasOwnProperty(emoji_name)) {
|
2017-09-27 19:39:42 +02:00
|
|
|
var emoji_url = emoji.active_realm_emojis[emoji_name].emoji_url;
|
|
|
|
return '<img alt="' + alt_text + '"' +
|
2017-05-09 18:01:43 +02:00
|
|
|
' class="emoji" src="' + emoji_url + '"' +
|
2017-06-09 10:30:24 +02:00
|
|
|
' title="' + title + '">';
|
2017-09-27 19:39:42 +02:00
|
|
|
} else if (emoji_codes.name_to_codepoint.hasOwnProperty(emoji_name)) {
|
|
|
|
var codepoint = emoji_codes.name_to_codepoint[emoji_name];
|
2019-01-16 10:11:30 +01:00
|
|
|
return make_emoji_span(codepoint, title, alt_text);
|
2017-05-09 18:01:43 +02:00
|
|
|
}
|
2017-09-27 19:39:42 +02:00
|
|
|
return alt_text;
|
2017-05-09 18:01:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleAvatar(email) {
|
|
|
|
return '<img alt="' + email + '"' +
|
|
|
|
' class="message_body_gravatar" src="/avatar/' + email + '?s=30"' +
|
|
|
|
' title="' + email + '">';
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleStream(streamName) {
|
|
|
|
var stream = stream_data.get_sub(streamName);
|
|
|
|
if (stream === undefined) {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-05-09 18:01:43 +02:00
|
|
|
}
|
2018-02-15 21:02:47 +01:00
|
|
|
var href = window.location.origin + '/#narrow/stream/' + hash_util.encode_stream_name(stream.name);
|
2017-05-09 18:01:43 +02:00
|
|
|
return '<a class="stream" data-stream-id="' + stream.stream_id + '" ' +
|
2018-02-15 21:02:47 +01:00
|
|
|
'href="' + href + '"' +
|
2018-03-29 00:25:58 +02:00
|
|
|
'>' + '#' + escape(stream.name) + '</a>';
|
2017-05-09 18:01:43 +02: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);
|
|
|
|
current_group += 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleTex(tex, fullmatch) {
|
|
|
|
try {
|
|
|
|
return katex.renderToString(tex);
|
|
|
|
} catch (ex) {
|
2017-08-08 17:56:34 +02:00
|
|
|
if (ex.message.indexOf('KaTeX parse error') === 0) { // TeX syntax error
|
2017-05-09 18:01:43 +02:00
|
|
|
return '<span class="tex-error">' + escape(fullmatch) + '</span>';
|
|
|
|
}
|
|
|
|
blueslip.error(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-02-11 22:54:18 +01:00
|
|
|
// Reset the RegExp state
|
|
|
|
named_group_re.lastIndex = 0;
|
2017-05-09 18:01:43 +02:00
|
|
|
match = named_group_re.exec(pattern);
|
|
|
|
|
|
|
|
current_group += 1;
|
|
|
|
}
|
|
|
|
// 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, "");
|
|
|
|
}
|
2017-07-30 21:07:59 +02:00
|
|
|
// Ideally we should have been checking that realm filters
|
|
|
|
// begin with certain characters but since there is no
|
|
|
|
// support for negative lookbehind in javascript, we check
|
|
|
|
// for this condition in `contains_backend_only_syntax()`
|
|
|
|
// function. If the condition is satisfied then the message
|
|
|
|
// is rendered locally, otherwise, we return false there and
|
|
|
|
// message is rendered on the backend which has proper support
|
|
|
|
// for negative lookbehind.
|
|
|
|
pattern = pattern + /(?![\w])/.source;
|
2019-02-12 22:30:57 +01:00
|
|
|
var final_regex = null;
|
|
|
|
try {
|
|
|
|
final_regex = new RegExp(pattern, js_flags);
|
|
|
|
} catch (ex) {
|
|
|
|
// We have an error computing the generated regex syntax.
|
|
|
|
// We'll ignore this realm filter for now, but log this
|
|
|
|
// failure for debugging later.
|
|
|
|
blueslip.error('python_to_js_filter: ' + ex.message);
|
|
|
|
}
|
|
|
|
return [final_regex, url];
|
2017-05-09 18:01:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.set_realm_filters = function (realm_filters) {
|
|
|
|
// Update the marked parser with our particular set of realm filters
|
|
|
|
realm_filter_map = {};
|
|
|
|
realm_filter_list = [];
|
|
|
|
|
|
|
|
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);
|
2019-02-12 22:30:57 +01:00
|
|
|
if (!js_filters[0]) {
|
|
|
|
// Skip any realm filters that could not be converted
|
|
|
|
return;
|
|
|
|
}
|
2017-05-09 18:01:43 +02:00
|
|
|
|
|
|
|
realm_filter_map[js_filters[0]] = js_filters[1];
|
|
|
|
realm_filter_list.push([js_filters[0], js_filters[1]]);
|
|
|
|
marked_rules.push(js_filters[0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
marked.InlineLexer.rules.zulip.realm_filters = marked_rules;
|
|
|
|
};
|
|
|
|
|
2017-12-03 15:24:21 +01:00
|
|
|
var preprocess_auto_olists = (function () {
|
|
|
|
var TAB_LENGTH = 2;
|
|
|
|
var re = /^( *)(\d+)\. +(.*)/;
|
|
|
|
|
|
|
|
function getIndent(match) {
|
|
|
|
return Math.floor(match[1].length / TAB_LENGTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
function extendArray(arr, arr2) {
|
|
|
|
Array.prototype.push.apply(arr, arr2);
|
|
|
|
}
|
|
|
|
|
|
|
|
function renumber(mlist) {
|
|
|
|
if (!mlist.length) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var startNumber = parseInt(mlist[0][2], 10);
|
|
|
|
var changeNumbers = _.every(mlist, function (m) {
|
|
|
|
return startNumber === parseInt(m[2], 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
var counter = startNumber;
|
|
|
|
return _.map(mlist, function (m) {
|
|
|
|
var number = changeNumbers ? counter.toString() : m[2];
|
|
|
|
counter += 1;
|
|
|
|
return m[1] + number + '. ' + m[3];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return function (src) {
|
|
|
|
var newLines = [];
|
|
|
|
var currentList = [];
|
|
|
|
var currentIndent = 0;
|
|
|
|
|
|
|
|
_.each(src.split('\n'), function (line) {
|
|
|
|
var m = line.match(re);
|
|
|
|
var isNextItem = m && currentList.length && currentIndent === getIndent(m);
|
2019-03-01 19:46:24 +01:00
|
|
|
var isBlankLine = line.trim() === "";
|
|
|
|
if (!isNextItem && !isBlankLine) {
|
|
|
|
// This is a non-blank line that doesn't start with a
|
|
|
|
// bullet, so we're done with the previous numbered
|
|
|
|
// list and can start a new one.
|
2017-12-03 15:24:21 +01:00
|
|
|
extendArray(newLines, renumber(currentList));
|
|
|
|
currentList = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m) {
|
2019-03-01 19:46:24 +01:00
|
|
|
// This line doesn't start with a bullet. If it's not
|
|
|
|
// between bullets of a list (i.e. `currentList =
|
|
|
|
// []`), this is just normal content outside a bulleted
|
|
|
|
// list and we can append it to `new_lines`.
|
|
|
|
if (!currentList.length) {
|
|
|
|
newLines.push(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, it's a blank line in between bullets,
|
|
|
|
// because if this was a bullet, `m` would be truthy,
|
|
|
|
// and if it wasn't blank, we could have terminated the
|
|
|
|
// list (see above). We can just skip this blank line
|
|
|
|
// syntax, as our bulleted list CSS styling will
|
|
|
|
// control vertical spacing between bullets.
|
2017-12-03 15:24:21 +01:00
|
|
|
} else if (isNextItem) {
|
|
|
|
currentList.push(m);
|
|
|
|
} else {
|
|
|
|
currentList = [m];
|
|
|
|
currentIndent = getIndent(m);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
extendArray(newLines, renumber(currentList));
|
|
|
|
|
|
|
|
return newLines.join('\n');
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
2017-05-09 18:01:43 +02:00
|
|
|
exports.initialize = function () {
|
|
|
|
|
|
|
|
function disable_markdown_regex(rules, name) {
|
|
|
|
rules[name] = {exec: function () {
|
2018-05-06 21:43:17 +02:00
|
|
|
return false;
|
|
|
|
}};
|
2017-05-09 18:01:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Configure the marked markdown parser for our usage
|
|
|
|
var r = new marked.Renderer();
|
|
|
|
|
|
|
|
// No <code> around our code blocks instead a codehilite <div> and disable
|
|
|
|
// class-specific highlighting.
|
|
|
|
r.code = function (code) {
|
|
|
|
return '<div class="codehilite"><pre>'
|
|
|
|
+ escape(code, true)
|
|
|
|
+ '\n</pre></div>\n\n\n';
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Put a newline after a <br> in the generated HTML to match bugdown
|
|
|
|
r.br = function () {
|
|
|
|
return '<br>\n';
|
|
|
|
};
|
|
|
|
|
|
|
|
function preprocess_code_blocks(src) {
|
|
|
|
return fenced_code.process_fenced_code(src);
|
|
|
|
}
|
|
|
|
|
2018-01-15 19:36:32 +01:00
|
|
|
function preprocess_translate_emoticons(src) {
|
|
|
|
if (!page_params.translate_emoticons) {
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In this scenario, the message has to be from the user, so the only
|
|
|
|
// requirement should be that they have the setting on.
|
|
|
|
return emoji.translate_emoticons_to_names(src);
|
|
|
|
}
|
|
|
|
|
2017-05-09 18:01:43 +02: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*$)/;
|
|
|
|
|
|
|
|
// Disable headings
|
|
|
|
disable_markdown_regex(marked.Lexer.rules.tables, 'heading');
|
|
|
|
disable_markdown_regex(marked.Lexer.rules.tables, 'lheading');
|
|
|
|
|
|
|
|
// Disable __strong__ (keeping **strong**)
|
|
|
|
marked.InlineLexer.rules.zulip.strong = /^\*\*([\s\S]+?)\*\*(?!\*)/;
|
|
|
|
|
|
|
|
// Make sure <del> syntax matches the backend processor
|
|
|
|
marked.InlineLexer.rules.zulip.del = /^(?!<\~)\~\~([^~]+)\~\~(?!\~)/;
|
|
|
|
|
|
|
|
// Disable _emphasis_ (keeping *emphasis*)
|
|
|
|
// Text inside ** must start and end with a word character
|
2018-04-22 19:53:04 +02:00
|
|
|
// to prevent mis-parsing things like "char **x = (char **)y"
|
2017-05-09 18:01:43 +02:00
|
|
|
marked.InlineLexer.rules.zulip.em = /^\*(?!\s+)((?:\*\*|[\s\S])+?)((?:[\S]))\*(?!\*)/;
|
|
|
|
|
|
|
|
// 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');
|
|
|
|
|
|
|
|
exports.set_realm_filters(page_params.realm_filters);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
fenced_code.set_escape_func(escape);
|
|
|
|
|
|
|
|
marked.setOptions({
|
|
|
|
gfm: true,
|
|
|
|
tables: true,
|
|
|
|
breaks: true,
|
|
|
|
pedantic: false,
|
|
|
|
sanitize: true,
|
|
|
|
smartLists: true,
|
|
|
|
smartypants: false,
|
|
|
|
zulip: true,
|
|
|
|
emojiHandler: handleEmoji,
|
|
|
|
avatarHandler: handleAvatar,
|
|
|
|
unicodeEmojiHandler: handleUnicodeEmoji,
|
|
|
|
streamHandler: handleStream,
|
|
|
|
realmFilterHandler: handleRealmFilter,
|
|
|
|
texHandler: handleTex,
|
|
|
|
renderer: r,
|
2018-01-15 19:36:32 +01:00
|
|
|
preprocessors: [
|
|
|
|
preprocess_code_blocks,
|
|
|
|
preprocess_auto_olists,
|
|
|
|
preprocess_translate_emoticons,
|
|
|
|
],
|
2017-05-09 18:01:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = markdown;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.markdown = markdown;
|