From d69dc2e99c7ed7ee8371c1b115e160fb3533b21b Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Fri, 5 Jun 2015 12:27:26 -0300 Subject: [PATCH] Improve message pre processing --- client/views/app/chatMessageDashboard.coffee | 52 +++++++++++++++----- client/views/app/chatMessageDashboard.html | 2 +- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/client/views/app/chatMessageDashboard.coffee b/client/views/app/chatMessageDashboard.coffee index f8470b50f940..9919bbd24af3 100644 --- a/client/views/app/chatMessageDashboard.coffee +++ b/client/views/app/chatMessageDashboard.coffee @@ -11,23 +11,49 @@ Template.chatMessageDashboard.helpers isEditing: -> return this._id is Session.get('editingMessageId') - autolinkerAndMentions: -> + preProcessingMessage: -> msg = this.msg - msg = Autolinker.link(_.escapeHTML(msg), { stripPrefix: false, twitter: false }) - - # TODO Create as very last helper, is braking MD - # msg = msg.replace /\n/g, '
' - + # Separate text in code blocks and non code blocks + msgParts = msg.split(/(```.*\n[\s\S]*?\n```)/) + + for part, index in msgParts + # Verify if this part is code + codeMatch = part.match(/```(.*)\n([\s\S]*?)\n```/) + if codeMatch? + # Process highlight if this part is code + lang = codeMatch[1] + code = codeMatch[2] + if lang not in hljs.listLanguages() + result = hljs.highlightAuto code + else + result = hljs.highlight lang, code + msgParts[index] = "
" + result.value + "
" + else + # Escape html and fix line breaks for non code blocks + part = _.escapeHTML part + part = part.replace /\n/g, '
' + msgParts[index] = part + + # Re-mount message + msg = msgParts.join('') + + # Process links in message + msg = Autolinker.link(msg, { stripPrefix: false, twitter: false }) + + # Process MD like for strong, italic and strike + msg = msg.replace(/\*([^*]+)\*/g, '$1') + msg = msg.replace(/\_([^_]+)\_/g, '$1') + msg = msg.replace(/\~([^_]+)\~/g, '$1') + + # Highlight mentions if not this.mentions? or this.mentions.length is 0 - return msg - - mentions = _.map this.mentions, (mention) -> - return mention.username or mention + mentions = _.map this.mentions, (mention) -> + return mention.username or mention - mentions = mentions.join('|') - msg = msg.replace new RegExp("(?:^|\\s)(@(#{mentions}))(?:\\s|$)", 'g'), (match, mention, username) -> - return match.replace mention, "#{mention}" + mentions = mentions.join('|') + msg = msg.replace new RegExp("(?:^|\\s)(@(#{mentions}))(?:\\s|$)", 'g'), (match, mention, username) -> + return match.replace mention, "#{mention}" return msg diff --git a/client/views/app/chatMessageDashboard.html b/client/views/app/chatMessageDashboard.html index ed85d434fbf1..e1388040d527 100644 --- a/client/views/app/chatMessageDashboard.html +++ b/client/views/app/chatMessageDashboard.html @@ -22,7 +22,7 @@ {{else}}
- {{#markdown}}{{#emojione}}{{autolinkerAndMentions message}}{{/emojione}}{{/markdown}} + {{#emojione}}{{preProcessingMessage message}}{{/emojione}}
{{/if}} {{/if}}