zulip/frontend_tests/node_tests/markdown.js

528 lines
22 KiB
JavaScript

/*global Dict */
zrequire('hash_util');
zrequire('katex', 'node_modules/katex/dist/katex.min.js');
zrequire('marked', 'third/marked/lib/marked');
zrequire('util');
zrequire('fenced_code');
zrequire('stream_data');
zrequire('people');
zrequire('user_groups');
zrequire('emoji_codes', 'generated/emoji/emoji_codes');
zrequire('emoji');
zrequire('message_store');
zrequire('markdown');
global.patch_builtin('window', {
location: {
origin: 'http://zulip.zulipdev.com',
},
});
set_global('page_params', {
realm_users: [],
realm_emoji: {
1: {id: 1,
name: 'burrito',
source_url: '/static/generated/emoji/images/emoji/burrito.png',
deactivated: false,
},
},
realm_filters: [
[
"#(?P<id>[0-9]{2,8})",
"https://trac.zulip.net/ticket/%(id)s",
],
[
"ZBUG_(?P<id>[0-9]{2,8})",
"https://trac2.zulip.net/ticket/%(id)s",
],
[
"ZGROUP_(?P<id>[0-9]{2,8}):(?P<zone>[0-9]{1,8})",
"https://zone_%(zone)s.zulip.net/ticket/%(id)s",
],
],
translate_emoticons: false,
});
set_global('blueslip', global.make_zblueslip());
set_global('Image', function () {
return {};
});
emoji.initialize();
var doc = "";
set_global('document', doc);
set_global('$', global.make_zjquery());
set_global('feature_flags', {local_echo: true});
var people = global.people;
var cordelia = {
full_name: 'Cordelia Lear',
user_id: 101,
email: '[email protected]',
};
people.add(cordelia);
people.add({
full_name: 'Leo',
user_id: 102,
email: '[email protected]',
});
people.add({
full_name: 'Bobby <h1>Tables</h1>',
user_id: 103,
email: '[email protected]',
});
people.add({
full_name: 'Mark Twin',
user_id: 104,
email: '[email protected]',
});
people.add({
full_name: 'Mark Twin',
user_id: 105,
email: '[email protected]',
});
people.add({
full_name: 'Brother of Bobby|123',
user_id: 106,
email: '[email protected]',
});
people.initialize_current_user(cordelia.user_id);
var hamletcharacters = {
name: "hamletcharacters",
id: 1,
description: "Characters of Hamlet",
members: [cordelia.user_id],
};
var backend = {
name: "Backend",
id: 2,
description: "Backend team",
members: [],
};
var edgecase_group = {
name: "Bobby <h1>Tables</h1>",
id: 3,
description: "HTML Syntax to check for Markdown edge cases.",
members: [],
};
global.user_groups.add(hamletcharacters);
global.user_groups.add(backend);
global.user_groups.add(edgecase_group);
var stream_data = global.stream_data;
var denmark = {
subscribed: false,
color: 'blue',
name: 'Denmark',
stream_id: 1,
in_home_view: false,
};
var social = {
subscribed: true,
color: 'red',
name: 'social',
stream_id: 2,
in_home_view: true,
invite_only: true,
};
var edgecase_stream = {
subscribed: true,
color: 'green',
name: 'Bobby <h1>Tables</h1>',
stream_id: 3,
in_home_view: true,
};
stream_data.add_sub('Denmark', denmark);
stream_data.add_sub('social', social);
stream_data.add_sub('Bobby <h1>Tables</h1>', edgecase_stream);
// Check the default behavior of fenced code blocks
// works properly before markdown is initialized.
run_test('fenced_block_defaults', () => {
var input = '\n```\nfenced code\n```\n\nand then after\n';
var expected = '\n\n<div class="codehilite"><pre><span></span>fenced code\n</pre></div>\n\n\n\nand then after\n\n';
var output = fenced_code.process_fenced_code(input);
assert.equal(output, expected);
});
markdown.initialize();
var bugdown_data = global.read_fixture_data('markdown_test_cases.json');
run_test('bugdown_detection', () => {
var no_markup = [
"This is a plaintext message",
"This is a plaintext: message",
"This is a :plaintext message",
"This is a :plaintext message: message",
"Contains a not an image.jpeg/ok file",
"Contains a not an http://www.google.com/ok/image.png/stop file",
"No png to be found here, a png",
"No user mention **leo**",
"No user mention @what there",
"No group mention *hamletcharacters*",
"We like to code\n~~~\ndef code():\n we = \"like to do\"\n~~~",
"This is a\nmultiline :emoji: here\n message",
"This is an :emoji: message",
"User Mention @**leo**",
"User Mention @**leo f**",
"User Mention @**leo with some name**",
"Group Mention @*hamletcharacters*",
"Stream #**Verona**",
"This contains !gravatar([email protected])",
"And an avatar !avatar([email protected]) is here",
];
var markup = [
"Contains a https://zulip.com/image.png file",
"Contains a https://zulip.com/image.jpg file",
"https://zulip.com/image.jpg",
"also https://zulip.com/image.jpg",
"https://zulip.com/image.jpg too",
"Contains a zulip.com/foo.jpeg file",
"Contains a https://zulip.com/image.png file",
"twitter url https://twitter.com/jacobian/status/407886996565016579",
"https://twitter.com/jacobian/status/407886996565016579",
"then https://twitter.com/jacobian/status/407886996565016579",
"twitter url http://twitter.com/jacobian/status/407886996565016579",
"youtube url https://www.youtube.com/watch?v=HHZ8iqswiCw&feature=youtu.be&a",
];
no_markup.forEach(function (content) {
assert.equal(markdown.contains_backend_only_syntax(content), false);
});
markup.forEach(function (content) {
assert.equal(markdown.contains_backend_only_syntax(content), true);
});
});
run_test('marked_shared', () => {
var tests = bugdown_data.regular_tests;
tests.forEach(function (test) {
// Ignore tests if specified
if (test.ignore === true) {
return;
}
var message = {raw_content: test.input};
page_param