From 8f3576fbd647ed9168658d3006e3b815a512b99d Mon Sep 17 00:00:00 2001
From: Rodrigo Nascimento <rodrigoknascimento@gmail.com>
Date: Mon, 28 Dec 2015 10:21:28 -0200
Subject: [PATCH] Tokenize message on message render to prevent re processing

---
 packages/rocketchat-highlight/highlight.coffee  | 11 ++++++++++-
 packages/rocketchat-markdown/markdown.coffee    | 17 ++++++++++++++---
 .../message/message.coffee                      |  4 ++++
 3 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/packages/rocketchat-highlight/highlight.coffee b/packages/rocketchat-highlight/highlight.coffee
index c98270151ae..57944340bcd 100644
--- a/packages/rocketchat-highlight/highlight.coffee
+++ b/packages/rocketchat-highlight/highlight.coffee
@@ -8,6 +8,7 @@ class Highlight
 	constructor: (message) ->
 
 		if s.trim message.html
+			message.tokens ?= []
 
 			# Count occurencies of ```
 			count = (message.html.match(/```/g) || []).length
@@ -40,7 +41,15 @@ class Highlight
 							result = hljs.highlightAuto code
 						else
 							result = hljs.highlight lang, code
-						msgParts[index] = "<pre><code class='hljs " + result.language + "'><span class='copyonly'>```<br></span>" + result.value + "<span class='copyonly'><br>```</span></code></pre>"
+
+						token = "$#{Random.id()}$"
+
+						message.tokens.push
+							highlight: true
+							token: token
+							text: "<pre><code class='hljs " + result.language + "'><span class='copyonly'>```<br></span>" + result.value + "<span class='copyonly'><br>```</span></code></pre>"
+
+						msgParts[index] = token
 					else
 						msgParts[index] = part
 
diff --git a/packages/rocketchat-markdown/markdown.coffee b/packages/rocketchat-markdown/markdown.coffee
index c578fec7160..f33c817e527 100644
--- a/packages/rocketchat-markdown/markdown.coffee
+++ b/packages/rocketchat-markdown/markdown.coffee
@@ -13,6 +13,20 @@ class Markdown
 			else
 				return message
 
+		# Support `text`
+		if _.isString message
+			msg = msg.replace(/(^|&gt;|[ >_*~])\`([^`\r\n]+)\`([<_*~]|\B|\b|$)/gm, '$1<span class="copyonly">`</span><span><code class="inline">$2</code></span><span class="copyonly">`</span>$3')
+		else
+			message.tokens ?= []
+			msg = msg.replace /(^|&gt;|[ >_*~])\`([^`\r\n]+)\`([<_*~]|\B|\b|$)/gm, (match, p1, p2, p3, offset, text) ->
+				token = "$#{Random.id()}$"
+
+				message.tokens.push
+					token: token
+					text: "#{p1}<span class=\"copyonly\">`</span><span><code class=\"inline\">#{p2}</code></span><span class=\"copyonly\">`</span>#{p3}"
+
+				return token
+
 		# Support ![alt text](http://image url)
 		msg = msg.replace(/!\[([^\]]+)\]\((https?:\/\/[^\)]+)\)/gm, '<a href="$2" title="$1" class="swipebox" target="_blank"><div class="inline-image" style="background-image: url($2);"></div></a>')
 
@@ -35,9 +49,6 @@ class Markdown
 			# Support # Text for h4
 			msg = msg.replace(/^#### (([\w\d-_\/\*\.,\\] ?)+)/gm, '<h4>$1</h4>')
 
-		# Support `text`
-		msg = msg.replace(/(^|&gt;|[ >_*~])\`([^`\r\n]+)\`([<_*~]|\B|\b|$)/gm, '$1<span class="copyonly">`</span><code class="inline">$2</code><span class="copyonly">`</span>$3')
-
 		# Support *text* to make bold
 		msg = msg.replace(/(^|&gt;|[ >_~`])\*{1,2}([^\*\r\n]+)\*{1,2}([<_~`]|\B|\b|$)/gm, '$1<span class="copyonly">*</span><strong>$2</strong><span class="copyonly">*</span>$3')
 
diff --git a/packages/rocketchat-ui-message/message/message.coffee b/packages/rocketchat-ui-message/message/message.coffee
index 7d9bcffbe28..dff309fa1fb 100644
--- a/packages/rocketchat-ui-message/message/message.coffee
+++ b/packages/rocketchat-ui-message/message/message.coffee
@@ -102,6 +102,10 @@ Template.message.onCreated ->
 				msg.html = _.escapeHTML msg.html
 
 			message = RocketChat.callbacks.run 'renderMessage', msg
+			if message.tokens?.length > 0
+				for token in message.tokens
+					message.html = message.html.replace token.token, token.text
+
 			# console.log JSON.stringify message
 			msg.html = message.html.replace /\n/gm, '<br/>'
 			return msg.html
-- 
GitLab