Skip to content

Commit

Permalink
Much better link formatting based on feedback from @kballard
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhammond committed Dec 18, 2014
1 parent 64d7c57 commit a01dbc0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
48 changes: 24 additions & 24 deletions src/slack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -134,43 +134,43 @@ 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

send: (envelope, messages...) ->
channel = @client.getChannelGroupOrDMByName envelope.room
Expand Down
12 changes: 10 additions & 2 deletions test/slack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,23 @@ 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 <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 a01dbc0

Please sign in to comment.