Skip to content

Commit

Permalink
Merge pull request slackapi#131 from slackhq/better-formatting-removal
Browse files Browse the repository at this point in the history
Better formatting code
  • Loading branch information
paulhammond committed Dec 18, 2014
2 parents 64d7c57 + ea562ae commit 4d8bc12
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
51 changes: 27 additions & 24 deletions src/slack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -134,43 +134,46 @@ class SlackBot extends Adapter

@receive new TextMessage user, txt, msg.ts

removeFormatting: (txt) ->
removeFormatting: (text) ->
# https://api.slack.com/docs/formatting
txt = txt.replace ///
text = text.replace ///
< # opening angle bracket
([\@\#\!]) # link type
(\w+) # id
(?:\|([^>]+))? # |label (optional)
([@#!])? # link type
([^>|]+) # link
(?:\| # start of |label (optional)
([^>]+) # label
)? # end of label
> # closing angle bracket
///g, (m, type, id, label) =>
if label then return label
///g, (m, type, link, label) =>

switch type

when '@'
user = @client.getUserByID id
if label then return label
user = @client.getUserByID link
if user
return "@#{user.name}"

when '#'
channel = @client.getChannelByID id
if label then return label
channel = @client.getChannelByID link
if channel
return "\##{channel.name}"

when '!'
if id in ['channel','group','everyone']
return "@#{id}"
"#{type}#{id}"
if link in ['channel','group','everyone']
return "@#{link}"

txt = txt.replace ///
< # opening angle bracket
([^>\|]+) # link
(?:\|([^>]+))? # label
> # closing angle bracket
///g, (m, link, label) =>
link = link.replace /^mailto:/, ''
if label
"#{label} #{link}"
else
link
txt
else
link = link.replace /^mailto:/, ''
if label and -1 == link.indexOf label
"#{label} (#{link})"
else
link
text = text.replace /&lt;/g, '<'
text = text.replace /&gt;/g, '>'
text = text.replace /&amp;/g, '&'
text

send: (envelope, messages...) ->
channel = @client.getChannelGroupOrDMByName envelope.room
Expand Down
20 changes: 18 additions & 2 deletions test/slack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ describe 'Removing message formatting', ->
foo = slackbot.removeFormatting 'foo'
foo.should.equal 'foo'

it 'Should decode entities', ->
foo = slackbot.removeFormatting 'foo &gt; &amp; &lt; &gt;&amp;&lt;'
foo.should.equal 'foo > & < >&<'

it 'Should change <@U1234> links to @name', ->
foo = slackbot.removeFormatting 'foo <@U123> bar'
foo.should.equal 'foo @name bar'
Expand Down Expand Up @@ -99,15 +103,27 @@ describe 'Removing message formatting', ->

it 'Should remove formatting around <https> links with a label', ->
foo = slackbot.removeFormatting 'foo <https://www.example.com|label> bar'
foo.should.equal 'foo label https://www.example.com bar'
foo.should.equal 'foo label (https://www.example.com) bar'

it 'Should remove formatting around <https> links with a substring label', ->
foo = slackbot.removeFormatting 'foo <https://www.example.com|example.com> bar'
foo.should.equal 'foo https://www.example.com bar'

it 'Should remove formatting around <https> links with a label containing entitles', ->
foo = slackbot.removeFormatting 'foo <https://www.example.com|label &gt; &amp; &lt;> bar'
foo.should.equal 'foo label > & < (https://www.example.com) bar'

it 'Should remove formatting around <mailto> links', ->
foo = slackbot.removeFormatting 'foo <mailto:name@example.com> bar'
foo.should.equal 'foo name@example.com bar'

it 'Should remove formatting around <mailto> links with an email label', ->
foo = slackbot.removeFormatting 'foo <mailto:name@example.com|name@example.com> bar'
foo.should.equal 'foo name@example.com bar'

it 'Should change multiple links at once', ->
foo = slackbot.removeFormatting 'foo <@U123|label> bar <#C123> <!channel> <https://www.example.com|label>'
foo.should.equal 'foo label bar #general @channel label https://www.example.com'
foo.should.equal 'foo label bar #general @channel label (https://www.example.com)'

describe 'Send Messages', ->
it 'Should send multiple messages', ->
Expand Down

0 comments on commit 4d8bc12

Please sign in to comment.