-
-
Notifications
You must be signed in to change notification settings - Fork 216
/
formatting.rb
87 lines (81 loc) · 2.58 KB
/
formatting.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# frozen_string_literal: true
module Slack
module Messages
module Formatting
class << self
#
# Unescape a message.
# @see https://api.slack.com/docs/formatting
#
def unescape(message)
CGI.unescapeHTML(message.gsub(/[“”]/, '"')
.gsub(/[‘’]/, "'")
.gsub(/<(?<sign>[?@#!]?)(?<dt>.*?)>/) do
sign = Regexp.last_match[:sign]
dt = Regexp.last_match[:dt]
rhs = dt.split('|', 2).last
case sign
when '@', '!'
"@#{rhs}"
when '#'
"##{rhs}"
else
rhs
end
end)
end
#
# Escape a message.
# @see https://api.slack.com/reference/surfaces/formatting#escaping
#
def escape(message)
message
.gsub('&', '&')
.gsub('>', '>')
.gsub('<', '<')
end
#
# Format a DateTime or Time object as a date and time in a Slack message
# @see https://api.slack.com/reference/surfaces/formatting#date-formatting
#
def date(time, format: '{date_num} {time_secs}', link: nil, text: nil)
args = [time.to_i, format, link].compact
"<!date^#{args.join('^')}|#{text || time}>"
end
#
# Embed a link to a Slack channel in a message by channel ID
# @see https://api.slack.com/reference/surfaces/formatting#linking-channels
#
def channel_link(channel_id)
"<##{channel_id}>"
end
#
# Embed a link to a user in a message by user ID
# @see https://api.slack.com/reference/surfaces/formatting#mentioning-users
#
def user_link(user_id)
"<@#{user_id}>"
end
#
# Embed a URL with custom link text in a message
# @see https://api.slack.com/reference/surfaces/formatting#linking-urls
#
def url_link(text, url)
"<#{url}|#{text}>"
end
#
# Converts text from basic markdown into Slack's mishmash
# @see https://api.slack.com/reference/surfaces/formatting#basic-formatting
#
def markdown(text)
text
.gsub(/(?<!\*)\*([^*\n]+)\*(?!\*)/, '_\1_') # italic
.gsub(/\*\*\*(.*?)\*\*\*/, '*_\1_*') # bold & italic
.gsub(/\*\*(.*?)\*\*/, '*\1*') # bold
.gsub(/~~(.*?)~~/, '~\1~') # strikethrough
.gsub(/\[(.*?)\]\((.*?)\)/, '<\2|\1>') # links
end
end
end
end
end