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 +4 -4
- data/lib/chat_sdk/slack/format_converter.rb +88 -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: 229d477d831f90275df64bc7eb47447ba4fcb574f435806d2839c3aa3154d106
|
|
4
|
+
data.tar.gz: a538ab5f51da90868d3fd4b018ba33d98b97d703d952844909cdc614e5a08e10
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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(">", ">")
|
|
62
|
+
result = result.gsub("<", "<")
|
|
63
|
+
result.gsub("&", "&")
|
|
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 -> > text (only at line start)
|
|
81
|
+
result.gsub(/^> /m, "> ")
|
|
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.
|
|
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
|