Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve message pre processing #157

Merged
merged 1 commit into from
Jun 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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