Skip to content

Commit 040a9be

Browse files
authored
Add Slack::Messages::Formatting#escape (#454)
1 parent e1338c4 commit 040a9be

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### 2.2.0 (Next)
22

33
* Your contribution here.
4+
* [#454](https://github.com/slack-ruby/slack-ruby-client/pull/454): Added `Slack::Messages::Formatting#escape` - [@marfoldi](https://github.com/marfoldi).
45
* [#452](https://github.com/slack-ruby/slack-ruby-client/pull/452): Automatically generate Web API multi-argument requirements from docs - [@jmanian](https://github.com/jmanian).
56
* [#448](https://github.com/slack-ruby/slack-ruby-client/pull/448), [#453](https://github.com/slack-ruby/slack-ruby-client/pull/453): Automatically convert more Web API arguments to JSON-encoded strings - [@jmanian](https://github.com/jmanian).
67

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ The `verify!` call may raise `Slack::Events::Request::MissingSigningSecret`, `Sl
585585

586586
### Message Parsing
587587

588-
All text in Slack uses the same [system of escaping](https://api.slack.com/docs/formatting): chat messages, direct messages, file comments, etc. Use [Slack::Messages::Formatting](lib/slack/messages/formatting.rb) to unescape incoming messages. This comes handy, for example, you want to treat all input to a real time bot as plain text.
588+
All text in Slack uses the same [system of escaping](https://api.slack.com/docs/formatting): chat messages, direct messages, file comments, etc. Use [Slack::Messages::Formatting](lib/slack/messages/formatting.rb) to escape or unescape messages. This comes handy, for example, you want to treat all input to a real time bot as plain text.
589589

590590
```ruby
591591
Slack::Messages::Formatting.unescape('Hello & <world>'))
@@ -610,6 +610,12 @@ Slack::Messages::Formatting.unescape('‘hello’'))
610610
# => "'hello'"
611611
```
612612

613+
614+
```ruby
615+
Slack::Messages::Formatting.escape('Hello & <world>')
616+
# => 'Hello &amp; &lt;world&gt;'
617+
```
618+
613619
### Command-Line Client
614620

615621
The slack command-line client returns JSON data from the Slack API.

lib/slack/messages/formatting.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ def unescape(message)
2424
end
2525
end)
2626
end
27+
28+
#
29+
# Escape a message.
30+
# @see https://api.slack.com/reference/surfaces/formatting#escaping
31+
#
32+
def escape(message)
33+
message
34+
.gsub('&', '&amp;')
35+
.gsub('>', '&gt;')
36+
.gsub('<', '&lt;')
37+
end
2738
end
2839
end
2940
end

spec/slack/messages/formatting_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,14 @@
6161
expect(formatting.unescape('‘hello’')).to eq "'hello'"
6262
end
6363
end
64+
65+
context '#escape' do
66+
it 'plain text' do
67+
expect(formatting.escape('plain text')).to eq 'plain text'
68+
end
69+
70+
it 'escapes a message' do
71+
expect(formatting.escape('Hello & <world>')).to eq 'Hello &amp; &lt;world&gt;'
72+
end
73+
end
6474
end

0 commit comments

Comments
 (0)