diff --git a/internal/markdown/markdown.go b/internal/markdown/markdown.go index 30fa687..6b29731 100644 --- a/internal/markdown/markdown.go +++ b/internal/markdown/markdown.go @@ -12,6 +12,7 @@ import ( ) var userRE = regexp.MustCompile("<@[A-Z0-9]+>") +var linkRE = regexp.MustCompile(`<(https?://[^|>]+)\|([^>]+)>`) type UserProvider interface { UsernameForID(string) (string, error) @@ -60,6 +61,22 @@ func parseUnixTimestamp(s string) (*time.Time, error) { return &result, nil } +func convert(client UserProvider, b *strings.Builder, s string) error { + text, err := interpolateUsers(client, s) + if err != nil { + return err + } + + text = linkRE.ReplaceAllString(text, "[$2]($1)") + + for _, line := range strings.Split(text, "\n") { + // TODO: Might be a good idea to escape 'line' + fmt.Fprintf(b, "> %s\n", line) + } + + return nil +} + func FromMessages(client *slackclient.SlackClient, history *slackclient.HistoryResponse) (string, error) { b := &strings.Builder{} messages := history.Messages @@ -101,21 +118,17 @@ func FromMessages(client *slackclient.SlackClient, history *slackclient.HistoryR msgTimes[message.Ts].Format("2006-01-02 15:04")) if message.Text != "" { - text, err := interpolateUsers(client, message.Text) + err = convert(client, b, message.Text) if err != nil { return "", err } - - for _, line := range strings.Split(text, "\n") { - // TODO: Might be a good idea to escape 'line' - fmt.Fprintf(b, "> %s\n", line) - } } // These seem to be mostly bot messages so far. Perhaps we should just skip them? for _, a := range message.Attachments { - for _, line := range strings.Split(a.Text, "\n") { - fmt.Fprintf(b, "> %s\n", line) + err = convert(client, b, a.Text) + if err != nil { + return "", err } } diff --git a/internal/markdown/markdown_test.go b/internal/markdown/markdown_test.go index a7f61d3..2b62d0b 100644 --- a/internal/markdown/markdown_test.go +++ b/internal/markdown/markdown_test.go @@ -46,3 +46,20 @@ func TestInterpolateUsers(t *testing.T) { } } } + +func TestLinkify(t *testing.T) { + table := [][]string{ + {"Hello end", "Hello [text](https://example.com) end"}, + {"Hello end and here is another link go check it out", "Hello [text](https://example.com) end and here is another link [GitHub!!](http://github.com) go check it out"}, + } + + for _, test := range table { + input := test[0] + expected := test[1] + actual := linkRE.ReplaceAllString(input, "[$2]($1)") + + if actual != expected { + t.Errorf("expected %q, actual %q", expected, actual) + } + } +}