-
Notifications
You must be signed in to change notification settings - Fork 8
/
markdown.js
351 lines (302 loc) · 11.5 KB
/
markdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// This contains zulip's frontend markdown implementation; see
// docs/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.
// prettier-disable
/* eslint-disable */
// prettier-ignore
var forEach = require('lodash.foreach');
var find = require('lodash.find');
var contains = require('lodash.contains');
const marked = require('./marked');
const hash_util = require('./hash_util');
const fenced_code = require('./fenced_code');
const emoji = require('./emoji');
var emoji_codes = require('./emoji_codes');
const katex = require('katex');
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);
});
return markedup !== undefined;
};
function push_uniquely(lst, elem) {
if (!contains(lst, elem)) {
lst.push(elem);
}
}
exports.apply_markdown = function (message) {
// if (message.flags === undefined) {
// message.flags = [];
// }
return marked(message + '\n\n').trim();
};
exports.add_message_flags = function (message) {
// Note: mention flags are set in apply_markdown()
if (message.raw_content.indexOf('/me ') === 0 &&
message.content.indexOf('<p>') === 0 &&
message.content.lastIndexOf('</p>') === message.content.length - 4) {
message.flags.push('is_me_message');
}
};
exports.add_subject_links = function (message) {
if (message.type !== 'stream') {
message.subject_links = [];
return;
}
var subject = message.subject;
var links = [];
forEach(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;
};
function escape(html, encode) {
return html
.replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
.replace(/</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 '<span class="emoji emoji-' + codepoint + '"' +
' title="' + title + '">' + alt_text +
'</span>';
}
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 '<img alt="' + alt_text + '"' +
' class="emoji" src="' + emoji_url + '"' +
' title="' + title + '">';
} else if (emoji_codes.name_to_codepoint.hasOwnProperty(emoji_name)) {
var codepoint = emoji_codes.name_to_codepoint[emoji_name];
return '<span class="emoji emoji-' + codepoint + '"' +
' title="' + title + '">' + alt_text +
'</span>';
}
return alt_text;
}
function handleAvatar(email) {
return '<img alt="' + email + '"' +
' class="message_body_gravatar" src="/avatar/' + email + '?s=30"' +
' title="' + email + '">';
}
function handleStream(streamName) {
var stream = this.stream_data.get_sub(streamName);
if (stream === undefined) {
return undefined;
}
return '<a class="stream" data-stream-id="' + stream.stream_id + '" ' +
'href="' + this.realm + '/#narrow/stream/' +
hash_util.encodeHashComponent(stream.name) + '"' +
'>' + '#' + stream.name + '</a>';
}
function handleUser(name) {
var person = this.people.get_by_name(name);
if (person !== undefined) {
if (this.people.is_my_user_id(person.user_id)) {
push_uniquely(message.flags, 'mentioned');
}
return '<span class="user-mention" data-user-id="' + person.user_id + '">' +
'@' + person.full_name +
'</span>';
} else if (name === 'all' || name === 'everyone') {
push_uniquely(message.flags, 'mentioned');
return '<span class="user-mention" data-user-id="*">' +
'@' + name +
'</span>';
}
return undefined;
}
function handleRealmFilter(pattern, matches) {
var url = realm_filter_map[pattern];
var current_group = 1;
forEach(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) {
if (ex.message.startsWith('ParseError: KaTeX parse error')) { // TeX syntax error
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);
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("");
forEach(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];
}
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 = [];
forEach(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];
realm_filter_list.push([js_filters[0], js_filters[1]]);
marked_rules.push(js_filters[0]);
});
marked.InlineLexer.rules.zulip.realm_filters = marked_rules;
};
exports.initialize = function (people, stream_data, realm, realm_filters, realm_emoji) {
function disable_markdown_regex(rules, name) {
rules[name] = {exec: function () {
return false;
},
};
}
// 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);
}
// 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
// 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(realm_filters);
emoji.set_realm_emoji(realm_emoji);
// 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,
userMentionHandler: handleUser,
realmFilterHandler: handleRealmFilter,
texHandler: handleTex,
renderer: r,
preprocessors: [preprocess_code_blocks],
people: people,
stream_data: stream_data,
realm: realm
});
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = markdown;
}