// This contains zulip's frontend markdown implementation; see // docs/subsystems/markdown.md for docs on our Markdown syntax. The other // 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 () { var exports = {}; var realm_filter_map = {}; var realm_filter_list = []; // Regexes that match some of our common bugdown markup var backend_only_markdown_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 /[^\s]*(?:twitter|youtube).com\/[^\s]*/, ]; exports.contains_backend_only_syntax = function (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(backend_only_markdown_re, function (re) { return re.test(content); }); // 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; }; exports.apply_markdown = function (message) { message_store.init_booleans(message); // Our python-markdown processor appends two \n\n to input var options = { userMentionHandler: function (name) { var person = people.get_by_name(name); if (person !== undefined) { if (people.is_my_user_id(person.user_id)) { message.mentioned = true; message.mentioned_me_directly = true; } return '' + '@' + person.full_name + ''; } else if (name === 'all' || name === 'everyone') { message.mentioned = true; return '' + '@' + name + ''; } return; }, 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())) { message.mentioned = true; } return '' + '@' + group.name + ''; } return; }, }; message.content = marked(message.raw_content + '\n\n', options).trim(); message.is_me_message = exports.is_status_message(message.raw_content, message.content); }; exports.add_subject_links = function (message) { if (message.type !== 'stream') { message.subject_links = []; return; } 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; 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); } }); message.subject_links = links; }; exports.is_status_message = function (raw_content, content) { return (raw_content.indexOf('/me ') === 0 && raw_content.indexOf('\n') === -1 && content.indexOf('
') === 0 && content.lastIndexOf('
') === content.length - 4); }; function escape(html, encode) { return html .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, '''); } function handleUnicodeEmoji(unicode_emoji) { 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]; var alt_text = ':' + emoji_name + ':'; var title = emoji_name.split("_").join(" "); return '' + alt_text + ''; } return unicode_emoji; } function handleEmoji(emoji_name) { var alt_text = ':' + emoji_name + ':'; var title = emoji_name.split("_").join(" "); if (emoji.active_realm_emojis.hasOwnProperty(emoji_name)) { var emoji_url = emoji.active_realm_emojis[emoji_name].emoji_url; return ' around our code blocks instead a codehilite  and disable
    // class-specific highlighting.
    r.code = function (code) {
        return ''
          + escape(code, true)
          + '\n\n\n\n';
    };
    // Our links have title= and target=_blank
    r.link = function (href, title, text) {
        title = title || href;
        var out = '' + text + '';
        return out;
    };
    // Put a newline after a 
 in the generated HTML to match bugdown
    r.br = function () {
        return '
\n';
    };
    function preprocess_code_blocks(src) {
        return fenced_code.process_fenced_code(src);
    }
    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);
    }
    // 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  syntax matches the backend processor
    marked.InlineLexer.rules.zulip.del = /^(?!<\~)\~\~([^~]+)\~\~(?!\~)/;
    // 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]))\*(?!\*)/;
    // 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,
        preprocessors: [
            preprocess_code_blocks,
            preprocess_auto_olists,
            preprocess_translate_emoticons,
        ],
    });
};
return exports;
}());
if (typeof module !== 'undefined') {
    module.exports = markdown;
}