chat_sdk-slack 0.7.0 → 0.8.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1110d3c38cf19a4274fab211b64f496c6fc0fdc9461375a6c55b87711e3395c3
4
- data.tar.gz: b78730c7b156b9f2ae0c4b548fe671b062fa12cb7ee31de6993a9e28caca553b
3
+ metadata.gz: 229d477d831f90275df64bc7eb47447ba4fcb574f435806d2839c3aa3154d106
4
+ data.tar.gz: a538ab5f51da90868d3fd4b018ba33d98b97d703d952844909cdc614e5a08e10
5
5
  SHA512:
6
- metadata.gz: f44439e7c4fe05bac0c9b4d886af0fa01e3a49171712220e82649dca86e3b8f8d7d4277a614d6c9a9c3f3df82694deb3661bc11200b0036934ae5878b0222195
7
- data.tar.gz: d845dbde4c82f06e22658e563294ab69b2067dea31f931d2229baf12c6d13230355bf3dad2e731084996789292181139617cbbeef23b798cc7d1975bda6f1466
6
+ metadata.gz: 86a3e66cc7be0f102beb2ce1d12c4b7e6ba60df6e0dbaa6887a43a3ff15dbbc16c1d003573cb12093a763fb338eb7d37205be62717c3feffbd6560533f73ea62
7
+ data.tar.gz: 0b78d5335690d409deedc6a69f45a57093ab559a35a0e20f1ee4518b883afc924f5919a4b2f46e1b50dc12fd443ae7eb9547c3c268daff1928c6db37244f1902
@@ -0,0 +1,88 @@
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
+ # ── Slack mrkdwn → Markdown ──────────────────────────────────────────
25
+
26
+ def convert_mrkdwn_to_md(text)
27
+ result = text
28
+
29
+ # Mentions must be processed before generic links to avoid <#C123|name> matching <url|text>
30
+ # User mentions: <@U123ABC> -> @U123ABC
31
+ result = result.gsub(/<@([A-Z0-9]+)>/, '@\1')
32
+
33
+ # Channel mentions with label: <#C123|general> -> #general
34
+ result = result.gsub(/<#[A-Z0-9]+\|([^>]+)>/, '#\1')
35
+
36
+ # Channel mentions without label: <#C123> -> #C123
37
+ result = result.gsub(/<#([A-Z0-9]+)>/, '#\1')
38
+
39
+ # Special mentions: <!everyone>, <!here>, <!channel>
40
+ result = result.gsub("<!everyone>", "@everyone")
41
+ result = result.gsub("<!here>", "@here")
42
+ result = result.gsub("<!channel>", "@channel")
43
+
44
+ # Links with labels: <url|text> -> [text](url) (after mentions are consumed)
45
+ result = result.gsub(/<([^>|]+)\|([^>]+)>/) { "[#{Regexp.last_match(2)}](#{Regexp.last_match(1)})" }
46
+
47
+ # Bare links: <url> -> url (must come after labeled links)
48
+ result = result.gsub(/<([^@#!][^>|]*)>/, '\1')
49
+
50
+ # Bold: *text* -> **text** (only single asterisks, not already doubled)
51
+ # Negative lookbehind/lookahead to avoid matching inside ** pairs
52
+ result = result.gsub(/(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)/, '**\1**')
53
+
54
+ # Italic: _text_ -> *text*
55
+ result = result.gsub(/(?<![a-zA-Z0-9])_(.+?)_(?![a-zA-Z0-9])/, '*\1*')
56
+
57
+ # Strikethrough: ~text~ -> ~~text~~
58
+ result = result.gsub(/(?<!~)~(?!~)(.+?)(?<!~)~(?!~)/, '~~\1~~')
59
+
60
+ # HTML entities (must come last so earlier patterns can match on encoded text)
61
+ result = result.gsub("&gt;", ">")
62
+ result = result.gsub("&lt;", "<")
63
+ result.gsub("&amp;", "&")
64
+ end
65
+
66
+ # ── Markdown → Slack mrkdwn ──────────────────────────────────────────
67
+
68
+ def convert_md_to_mrkdwn(text)
69
+ result = text
70
+
71
+ # Links: [text](url) -> <url|text>
72
+ result = result.gsub(/\[([^\]]+)\]\(([^)]+)\)/) { "<#{Regexp.last_match(2)}|#{Regexp.last_match(1)}>" }
73
+
74
+ # Bold/italic with placeholder to prevent double conversion
75
+ result = convert_bold_and_italic_from_md(result, bold_char: "*", italic_char: "_")
76
+
77
+ # Strikethrough: ~~text~~ -> ~text~
78
+ result = result.gsub(/~~(.+?)~~/, '~\1~')
79
+
80
+ # Blockquote lines: > text -> &gt; text (only at line start)
81
+ result.gsub(/^> /m, "&gt; ")
82
+
83
+ # HTML entities for remaining special chars are not converted here —
84
+ # Slack handles raw < > & in normal text. Only blockquote > needs encoding.
85
+ end
86
+ end
87
+ end
88
+ 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.7.0
4
+ version: 0.8.1
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