chat_sdk-slack 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/chat_sdk/slack/format_converter.rb +104 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 891f26261fed4ebb89b98b19ec57f2b304d9d3f8bedea493faa1457ac1bb742e
|
|
4
|
+
data.tar.gz: 9c1cc01f502f92642b89944aafc0ad24188d92c3c734d67735f00299c3bc6571
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4f59922bead78ae2f1f8a81cfc7f990d4ab2ef3c1fd193463def34897cb286696ac61c34699156435e21ed61c436099cfe1e3935ae84600b49c739f8beae8264
|
|
7
|
+
data.tar.gz: 07257e9662397d48d5c19798a2d8e5292b541ee600f046683a44b4b43b285dd1ab96900aa2eb0c5c593da885a98e26d8269c1c17a31b8c81d7cf7d549cefb3ca
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ChatSDK
|
|
4
|
+
module Slack
|
|
5
|
+
class FormatConverter < ChatSDK::Format::Converter
|
|
6
|
+
# Slack mrkdwn -> standard Markdown
|
|
7
|
+
def to_markdown(mrkdwn)
|
|
8
|
+
return "" if mrkdwn.nil? || mrkdwn.empty?
|
|
9
|
+
|
|
10
|
+
parts = split_code_blocks(mrkdwn)
|
|
11
|
+
parts.map.with_index { |part, i| i.odd? ? part : convert_mrkdwn_to_md(part) }.join
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Standard Markdown -> Slack mrkdwn
|
|
15
|
+
def from_markdown(markdown)
|
|
16
|
+
return "" if markdown.nil? || markdown.empty?
|
|
17
|
+
|
|
18
|
+
parts = split_code_blocks(markdown)
|
|
19
|
+
parts.map.with_index { |part, i| i.odd? ? part : convert_md_to_mrkdwn(part) }.join
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
# Splits text into alternating [text, code, text, code, ...] segments.
|
|
25
|
+
# Odd-indexed segments are code blocks (including their delimiters) and
|
|
26
|
+
# should be passed through without conversion.
|
|
27
|
+
def split_code_blocks(text)
|
|
28
|
+
# Match fenced code blocks (```...```) and inline code (`...`)
|
|
29
|
+
# The fenced block pattern must come first so triple backticks aren't
|
|
30
|
+
# eaten by the inline pattern.
|
|
31
|
+
text.split(/(```[\s\S]*?```|`[^`]+`)/)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# ── Slack mrkdwn → Markdown ──────────────────────────────────────────
|
|
35
|
+
|
|
36
|
+
def convert_mrkdwn_to_md(text)
|
|
37
|
+
result = text
|
|
38
|
+
|
|
39
|
+
# Mentions must be processed before generic links to avoid <#C123|name> matching <url|text>
|
|
40
|
+
# User mentions: <@U123ABC> -> @U123ABC
|
|
41
|
+
result = result.gsub(/<@([A-Z0-9]+)>/, '@\1')
|
|
42
|
+
|
|
43
|
+
# Channel mentions with label: <#C123|general> -> #general
|
|
44
|
+
result = result.gsub(/<#[A-Z0-9]+\|([^>]+)>/, '#\1')
|
|
45
|
+
|
|
46
|
+
# Channel mentions without label: <#C123> -> #C123
|
|
47
|
+
result = result.gsub(/<#([A-Z0-9]+)>/, '#\1')
|
|
48
|
+
|
|
49
|
+
# Special mentions: <!everyone>, <!here>, <!channel>
|
|
50
|
+
result = result.gsub("<!everyone>", "@everyone")
|
|
51
|
+
result = result.gsub("<!here>", "@here")
|
|
52
|
+
result = result.gsub("<!channel>", "@channel")
|
|
53
|
+
|
|
54
|
+
# Links with labels: <url|text> -> [text](url) (after mentions are consumed)
|
|
55
|
+
result = result.gsub(/<([^>|]+)\|([^>]+)>/) { "[#{Regexp.last_match(2)}](#{Regexp.last_match(1)})" }
|
|
56
|
+
|
|
57
|
+
# Bare links: <url> -> url (must come after labeled links)
|
|
58
|
+
result = result.gsub(/<([^@#!][^>|]*)>/, '\1')
|
|
59
|
+
|
|
60
|
+
# Bold: *text* -> **text** (only single asterisks, not already doubled)
|
|
61
|
+
# Negative lookbehind/lookahead to avoid matching inside ** pairs
|
|
62
|
+
result = result.gsub(/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/, '**\1**')
|
|
63
|
+
|
|
64
|
+
# Italic: _text_ -> *text*
|
|
65
|
+
result = result.gsub(/(?<![a-zA-Z0-9])_(.+?)_(?![a-zA-Z0-9])/, '*\1*')
|
|
66
|
+
|
|
67
|
+
# Strikethrough: ~text~ -> ~~text~~
|
|
68
|
+
result = result.gsub(/(?<!~)~(?!~)(.+?)(?<!~)~(?!~)/, '~~\1~~')
|
|
69
|
+
|
|
70
|
+
# HTML entities (must come last so earlier patterns can match on encoded text)
|
|
71
|
+
result = result.gsub(">", ">")
|
|
72
|
+
result = result.gsub("<", "<")
|
|
73
|
+
result.gsub("&", "&")
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# ── Markdown → Slack mrkdwn ──────────────────────────────────────────
|
|
77
|
+
|
|
78
|
+
def convert_md_to_mrkdwn(text)
|
|
79
|
+
result = text
|
|
80
|
+
|
|
81
|
+
# Links: [text](url) -> <url|text>
|
|
82
|
+
result = result.gsub(/\[([^\]]+)\]\(([^)]+)\)/) { "<#{Regexp.last_match(2)}|#{Regexp.last_match(1)}>" }
|
|
83
|
+
|
|
84
|
+
# Bold: **text** -> *text* (use placeholder to prevent italic pass from catching it)
|
|
85
|
+
result = result.gsub(/\*\*(.+?)\*\*/, "\x00BOLD\\1BOLD\x00")
|
|
86
|
+
|
|
87
|
+
# Italic: *text* -> _text_ (only single asterisks remaining after bold placeholder)
|
|
88
|
+
result = result.gsub(/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/, '_\1_')
|
|
89
|
+
|
|
90
|
+
# Restore bold placeholders
|
|
91
|
+
result = result.gsub(/\x00BOLD(.+?)BOLD\x00/, '*\1*')
|
|
92
|
+
|
|
93
|
+
# Strikethrough: ~~text~~ -> ~text~
|
|
94
|
+
result = result.gsub(/~~(.+?)~~/, '~\1~')
|
|
95
|
+
|
|
96
|
+
# Blockquote lines: > text -> > text (only at line start)
|
|
97
|
+
result.gsub(/^> /m, "> ")
|
|
98
|
+
|
|
99
|
+
# HTML entities for remaining special chars are not converted here —
|
|
100
|
+
# Slack handles raw < > & in normal text. Only blockquote > needs encoding.
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: chat_sdk-slack
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Quentin Rousseau
|
|
@@ -48,6 +48,7 @@ files:
|
|
|
48
48
|
- lib/chat_sdk/slack/adapter.rb
|
|
49
49
|
- lib/chat_sdk/slack/block_kit_renderer.rb
|
|
50
50
|
- lib/chat_sdk/slack/event_parser.rb
|
|
51
|
+
- lib/chat_sdk/slack/format_converter.rb
|
|
51
52
|
- lib/chat_sdk/slack/modal_renderer.rb
|
|
52
53
|
- lib/chat_sdk/slack/socket_mode.rb
|
|
53
54
|
homepage: https://github.com/rootlyhq/chat-sdk
|