Files
chenhg5-cc-connect/core/markdown_slack_test.go
cg33 33976574a6 fix(slack): convert Markdown to Slack mrkdwn format (#667) (#680)
Add MarkdownToSlackMrkdwn() converter that transforms standard Markdown
to Slack's mrkdwn format before sending messages:
- **bold** → *bold*
- ***bold italic*** → *_bold italic_*
- ~~strike~~ → ~strike~
- [text](url) → <url|text>
- # Heading → *Heading*
- Code blocks and inline code are preserved untouched.

Applied in both Reply() and Send() methods of the Slack platform.

Closes #667

Co-authored-by: Claude <noreply@anthropic.com>
2026-04-24 06:49:32 +08:00

82 lines
1.8 KiB
Go

package core
import "testing"
func TestMarkdownToSlackMrkdwn(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "bold conversion",
input: "This is **bold** text",
want: "This is *bold* text",
},
{
name: "bold-italic conversion",
input: "This is ***bold italic*** text",
want: "This is *_bold italic_* text",
},
{
name: "strikethrough",
input: "This is ~~deleted~~ text",
want: "This is ~deleted~ text",
},
{
name: "link conversion",
input: "Check [Google](https://google.com) out",
want: "Check <https://google.com|Google> out",
},
{
name: "heading",
input: "# Title\n## Subtitle",
want: "*Title*\n*Subtitle*",
},
{
name: "inline code preserved",
input: "Use `**not bold**` here",
want: "Use `**not bold**` here",
},
{
name: "code block preserved",
input: "Before\n```go\nfmt.Println(\"**hello**\")\n```\nAfter **bold**",
want: "Before\n```go\nfmt.Println(\"**hello**\")\n```\nAfter *bold*",
},
{
name: "multiple bold in one line",
input: "**first** and **second**",
want: "*first* and *second*",
},
{
name: "mixed formatting",
input: "**bold** and ~~strike~~ and [link](http://x.com)",
want: "*bold* and ~strike~ and <http://x.com|link>",
},
{
name: "no formatting",
input: "Plain text here",
want: "Plain text here",
},
{
name: "image tag",
input: "See ![screenshot](http://img.png) here",
want: "See screenshot here",
},
{
name: "empty string",
input: "",
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := MarkdownToSlackMrkdwn(tt.input)
if got != tt.want {
t.Errorf("MarkdownToSlackMrkdwn(%q)\n got: %q\n want: %q", tt.input, got, tt.want)
}
})
}
}