Skip to content

Commit

Permalink
Improve message pre processing
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok committed Jun 5, 2015
1 parent 4690531 commit d69dc2e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
52 changes: 39 additions & 13 deletions client/views/app/chatMessageDashboard.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<br/>'

# 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] = "<pre><code class='hljs " + result.language + "'>" + result.value + "</code></pre>"
else
# Escape html and fix line breaks for non code blocks
part = _.escapeHTML part
part = part.replace /\n/g, '<br/>'
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, '<strong>$1</strong>')
msg = msg.replace(/\_([^_]+)\_/g, '<i>$1</i>')
msg = msg.replace(/\~([^_]+)\~/g, '<strike>$1</strike>')

# 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, "<a href=\"\" class=\"mention-link\" data-username=\"#{username}\">#{mention}</a>"
mentions = mentions.join('|')
msg = msg.replace new RegExp("(?:^|\\s)(@(#{mentions}))(?:\\s|$)", 'g'), (match, mention, username) ->
return match.replace mention, "<a href=\"\" class=\"mention-link\" data-username=\"#{username}\">#{mention}</a>"

return msg

Expand Down
2 changes: 1 addition & 1 deletion client/views/app/chatMessageDashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</div>
{{else}}
<div>
{{#markdown}}{{#emojione}}{{autolinkerAndMentions message}}{{/emojione}}{{/markdown}}
{{#emojione}}{{preProcessingMessage message}}{{/emojione}}
</div>
{{/if}}
{{/if}}
Expand Down

0 comments on commit d69dc2e

Please sign in to comment.