Merge pull request #1764 from RocketChat/improvements/tokenize-message

Tokenize message on message render to prevent re processing
pull/1772/head
Gabriel Engel 10 years ago
commit 07a22635f7
  1. 11
      packages/rocketchat-highlight/highlight.coffee
  2. 17
      packages/rocketchat-markdown/markdown.coffee
  3. 4
      packages/rocketchat-ui-message/message/message.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

@ -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')

@ -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

Loading…
Cancel
Save