From d25a515f9e0889c9bbd366ef296ecb68de230d80 Mon Sep 17 00:00:00 2001 From: Gabriel Delavald Date: Fri, 30 Jun 2017 17:36:27 -0300 Subject: [PATCH] Refactoring markdowncode a bit and fixes messages showing unescaped when just sent --- packages/rocketchat-markdown/markdowncode.js | 31 ++++++------------- packages/rocketchat-mentions/Mentions.js | 3 +- .../client/renderMessageBody.js | 6 ++-- 3 files changed, 13 insertions(+), 27 deletions(-) diff --git a/packages/rocketchat-markdown/markdowncode.js b/packages/rocketchat-markdown/markdowncode.js index 65f00ce79ff..e00b030d01b 100644 --- a/packages/rocketchat-markdown/markdowncode.js +++ b/packages/rocketchat-markdown/markdowncode.js @@ -60,29 +60,16 @@ class MarkdownCode { if (codeMatch != null) { // Process highlight if this part is code - let code; - let lang; - let result; const singleLine = codeMatch[0].indexOf('\n') === -1; - - if (singleLine) { - lang = ''; - code = _.unescapeHTML(codeMatch[1] + codeMatch[2]); - } else { - lang = codeMatch[1]; - code = _.unescapeHTML(codeMatch[2]); - } - - if (s.trim(lang) === '') { - lang = ''; - } - - if (!Array.from(hljs.listLanguages()).includes(s.trim(lang))) { - result = hljs.highlightAuto((lang + code)); - } else { - result = hljs.highlight(s.trim(lang), code); - } - + const lang = !singleLine && Array.from(hljs.listLanguages()).includes(s.trim(codeMatch[1])) ? s.trim(codeMatch[1]) : ''; + const code = + singleLine ? + _.unescapeHTML(codeMatch[1]) : + lang === '' ? + _.unescapeHTML(codeMatch[1] + codeMatch[2]) : + _.unescapeHTML(codeMatch[2]); + + const result = lang === '' ? hljs.highlightAuto((lang + code)) : hljs.highlight(lang, code); const token = `=!=${ Random.id() }=!=`; message.tokens.push({ diff --git a/packages/rocketchat-mentions/Mentions.js b/packages/rocketchat-mentions/Mentions.js index 55959a9bed5..c9a744a9ac4 100644 --- a/packages/rocketchat-mentions/Mentions.js +++ b/packages/rocketchat-mentions/Mentions.js @@ -50,7 +50,8 @@ export default class { }); } replaceChannels(str, message) { - return str.replace(this.channelMentionRegex, (match, name) => { + //since apostrophe escaped contains # we need to unescape it + return str.replace(/'/g, '\'').replace(this.channelMentionRegex, (match, name) => { if (message.temp == null && _.findWhere(message.channels, {name}) == null) { return match; } diff --git a/packages/rocketchat-ui-message/client/renderMessageBody.js b/packages/rocketchat-ui-message/client/renderMessageBody.js index ec6dc6a8905..ebaa6a9eded 100644 --- a/packages/rocketchat-ui-message/client/renderMessageBody.js +++ b/packages/rocketchat-ui-message/client/renderMessageBody.js @@ -1,10 +1,8 @@ /* global renderMessageBody:true */ renderMessageBody = function(msg) { - msg.html = msg.msg; - - if (_.trim(msg.html) !== '') { - msg.html = _.escapeHTML(msg.html); + if (_.trim(msg.msg) !== '') { + msg.html = _.escapeHTML(msg.msg); } const message = RocketChat.callbacks.run('renderMessage', msg);