Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Regression on 0.64.0 was freezing the application when posting some URLs #10627

Merged
merged 2 commits into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions packages/rocketchat-lib/server/functions/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,31 +121,22 @@ RocketChat.sendMessage = function(user, message, room, upsert = false) {
}

if (message.parseUrls !== false) {
const urlRegex = /([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\(\)\w]*)?\??([-\+=&!:;%@\/\.\,\w]+)?(?:#([^\s\)]+))?)?/g;
const urls = message.msg.match(urlRegex);
message.html = message.msg;
message = RocketChat.Markdown.code(message);

const urls = message.msg.match(/([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\(\)\w]*)?\??([-\+=&!:;%@\/\.\,\w]+)?(?:#([^\s\)]+))?)?/g);
if (urls) {
// ignoredUrls contain blocks of quotes with urls inside
const ignoredUrls = message.msg.match(/(?:(?:\`{1,3})(?:[\n\r]*?.*?)*?)(([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\(\)\w]*)?\??([-\+=&!:;%@\/\.\,\w]+)?(?:#([^\s\)]+))?)?)(?:(?:[\n\r]*.*?)*?(?:\`{1,3}))/gm);
if (ignoredUrls) {
ignoredUrls.forEach((url) => {
const shouldBeIgnored = url.match(urlRegex);
if (shouldBeIgnored) {
shouldBeIgnored.forEach((match) => {
const matchIndex = urls.indexOf(match);
urls.splice(matchIndex, 1);
});
}
});
}
if (urls) {
// use the Set to remove duplicity, so it doesn't embed the same link twice
message.urls = [...new Set(urls)].map(function(url) {
return {
url
};
});
}
message.urls = urls.map(function(url) {
return {
url
};
});
}

message = RocketChat.Markdown.mountTokensBack(message, false);
message.msg = message.html;
delete message.html;
delete message.tokens;
}

message = RocketChat.callbacks.run('beforeSaveMessage', message);
Expand Down
12 changes: 9 additions & 3 deletions packages/rocketchat-markdown/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { RocketChat } from 'meteor/rocketchat:lib';
import { marked } from './parser/marked/marked.js';
import { original } from './parser/original/original.js';

import { code } from './parser/original/code.js';

const parsers = {
original,
marked
Expand Down Expand Up @@ -43,15 +45,19 @@ class MarkdownClass {
return parsers['original'](message);
}

mountTokensBack(message) {
mountTokensBack(message, useHtml = true) {
if (message.tokens && message.tokens.length > 0) {
for (const {token, text} of message.tokens) {
message.html = message.html.replace(token, () => text); // Uses lambda so doesn't need to escape $
for (const {token, text, noHtml} of message.tokens) {
message.html = message.html.replace(token, () => useHtml ? text : noHtml); // Uses lambda so doesn't need to escape $
}
}

return message;
}

code(...args) {
return code(...args);
}
}

const Markdown = new MarkdownClass;
Expand Down
4 changes: 2 additions & 2 deletions packages/rocketchat-markdown/parser/original/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const codeblocks = (message) => {
for (let index = 0; index < msgParts.length; index++) {
// Verify if this part is code
const part = msgParts[index];
const codeMatch = part.match(/^```(.*[\r\n\ ]?)([\s\S]*?)```+?$/);
const codeMatch = part.match(/^```[\r\n]*(.*[\r\n\ ]?)[\r\n]*([\s\S]*?)```+?$/);

if (codeMatch != null) {
// Process highlight if this part is code
Expand All @@ -59,7 +59,7 @@ const codeblocks = (message) => {
highlight: true,
token,
text: `<pre><code class='code-colors hljs ${ result.language }'><span class='copyonly'>\`\`\`<br></span>${ result.value }<span class='copyonly'><br>\`\`\`</span></code></pre>`,
noHtml: `\`\`\`\n${ s.stripTags(result.value) }\n\`\`\``
noHtml: codeMatch[0]
});

msgParts[index] = token;
Expand Down
6 changes: 3 additions & 3 deletions packages/rocketchat-markdown/tests/client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ const inlinecode = {
const code = {
'```code```': codeWrapper('<span class="hljs-keyword">code</span>', 'clean'),
'```code': codeWrapper('<span class="hljs-selector-tag">code</span>\n', 'stylus'),
'```code\n': codeWrapper('<span class="hljs-selector-tag">code</span>\n\n', 'stylus'),
'```\ncode\n```': codeWrapper('\n<span class="hljs-selector-tag">code</span>\n', 'stylus'),
'```code\n': codeWrapper('<span class="hljs-selector-tag">code</span>\n', 'stylus'),
'```\ncode\n```': codeWrapper('<span class="hljs-selector-tag">code</span>\n', 'stylus'),
'```code\n```': codeWrapper('<span class="hljs-selector-tag">code</span>\n', 'stylus'),
'```\ncode```': codeWrapper('\n<span class="hljs-keyword">code</span>', 'clean'),
'```\ncode```': codeWrapper('<span class="hljs-keyword">code</span>', 'clean'),
'```javascript\nvar a = \'log\';\nconsole.log(a);```': codeWrapper('<span class="hljs-keyword">var</span> a = <span class="hljs-string">\'log\'</span>;\n<span class="hljs-built_in">console</span>.log(a);', 'javascript'),
'```*code*```': codeWrapper('*<span class="hljs-meta">code</span>*', 'armasm'),
'```**code**```': codeWrapper('**<span class="hljs-meta">code</span>**', 'armasm'),
Expand Down