Skip to content

Commit

Permalink
Remove formatting around non-slack links
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhammond committed Dec 16, 2014
1 parent 274dc28 commit c1fec0c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/slack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ class SlackBot extends Adapter
@receive new TextMessage user, txt, msg.ts

removeFormatting: (txt) ->
#https://api.slack.com/docs/formatting
txt.replace /\<([\@\#\!])(\w+)(?:\|([^>]+))?\>/g, (m, type, id, label) =>
# https://api.slack.com/docs/formatting
txt = txt.replace /\<([\@\#\!])(\w+)(?:\|([^>]+))?\>/g, (m, type, id, label) =>

if label then return label

Expand All @@ -143,6 +143,12 @@ class SlackBot extends Adapter
if id in ['channel','group','everyone']
return "@#{id}"
"#{type}#{id}"
txt = txt.replace /<([^>\|]+)(?:\|([^>]+))?\>/g, (m, link, label) =>
if label
"#{label} #{link}"
else
link
txt

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 @@ -85,9 +85,25 @@ describe 'Removing message formatting', ->
foo = slackbot.removeFormatting 'foo <!group> bar'
foo.should.equal 'foo @group bar'

it 'Should remove formatting around <http> links', ->
foo = slackbot.removeFormatting 'foo <http://www.example.com> bar'
foo.should.equal 'foo http://www.example.com bar'

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

it 'Should remove formatting around <mailto> links', ->
foo = slackbot.removeFormatting 'foo <mailto:[email protected]> bar'
foo.should.equal 'foo mailto:[email protected] bar'

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'

it 'Should change multiple links at once', ->
foo = slackbot.removeFormatting 'foo <@U123|label> bar <#C123> <!channel>'
foo.should.equal 'foo label bar #general @channel'
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'

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

0 comments on commit c1fec0c

Please sign in to comment.