From a01dbc0061cc2ba1c5bee4ba62d513982efbeaf2 Mon Sep 17 00:00:00 2001 From: Paul Hammond Date: Wed, 17 Dec 2014 19:01:24 -0800 Subject: [PATCH] Much better link formatting based on feedback from @kballard --- src/slack.coffee | 48 +++++++++++++++++++++++------------------------ test/slack.coffee | 12 ++++++++++-- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/slack.coffee b/src/slack.coffee index 9ddabd91..67cacdde 100644 --- a/src/slack.coffee +++ b/src/slack.coffee @@ -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 diff --git a/test/slack.coffee b/test/slack.coffee index ab7e135b..181b4ce3 100644 --- a/test/slack.coffee +++ b/test/slack.coffee @@ -99,15 +99,23 @@ describe 'Removing message formatting', -> it 'Should remove formatting around links with a label', -> foo = slackbot.removeFormatting 'foo 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 links with a substring label', -> + foo = slackbot.removeFormatting 'foo bar' + foo.should.equal 'foo https://www.example.com bar' it 'Should remove formatting around links', -> foo = slackbot.removeFormatting 'foo bar' foo.should.equal 'foo name@example.com bar' + it 'Should remove formatting around links with an email label', -> + foo = slackbot.removeFormatting 'foo bar' + foo.should.equal 'foo name@example.com bar' + it 'Should change multiple links at once', -> foo = slackbot.removeFormatting 'foo <@U123|label> bar <#C123> ' - 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', ->