diff options
author | Diep Pham <mrfavadi@gmail.com> | 2017-02-02 21:28:27 +0700 |
---|---|---|
committer | Harrison Healey <harrisonmhealey@gmail.com> | 2017-02-02 09:28:27 -0500 |
commit | f1a4c0738301f50b6a9e8bd635fbcc172decb199 (patch) | |
tree | 698b6337e2c6622b8fdbb965e82568ab393cebb9 /app/slackimport.go | |
parent | 609d4f43d9eef504d852fbf02af5473b0d1424c8 (diff) | |
download | chat-f1a4c0738301f50b6a9e8bd635fbcc172decb199.tar.gz chat-f1a4c0738301f50b6a9e8bd635fbcc172decb199.tar.bz2 chat-f1a4c0738301f50b6a9e8bd635fbcc172decb199.zip |
PLT-495 Improves slack markup conversion (#4914)
* improves slack markup conversion
* bold
* strikethrough
* blockquotes
* handles blockquotes in slack_import
* removes unnecessary formatting
* fixes various format problems
Diffstat (limited to 'app/slackimport.go')
-rw-r--r-- | app/slackimport.go | 57 |
1 files changed, 54 insertions, 3 deletions
diff --git a/app/slackimport.go b/app/slackimport.go index 75a1606af..e54d0724b 100644 --- a/app/slackimport.go +++ b/app/slackimport.go @@ -564,12 +564,63 @@ func SlackConvertChannelMentions(channels []SlackChannel, posts map[string][]Sla } func SlackConvertPostsMarkup(posts map[string][]SlackPost) map[string][]SlackPost { - // Convert URLs in Slack's format to Markdown format. - regex := regexp.MustCompile(`<([^|<>]+)\|([^|<>]+)>`) + regexReplaceAllString := []struct { + regex *regexp.Regexp + rpl string + }{ + // URL + { + regexp.MustCompile(`<([^|<>]+)\|([^|<>]+)>`), + "[$2]($1)", + }, + // bold + { + regexp.MustCompile(`(^|[\s.;,])\*(\S[^*\n]+)\*`), + "$1**$2**", + }, + // strikethrough + { + regexp.MustCompile(`(^|[\s.;,])\~(\S[^~\n]+)\~`), + "$1~~$2~~", + }, + // single paragraph blockquote + // Slack converts > character to > + { + regexp.MustCompile(`(?sm)^>`), + ">", + }, + } + + regexReplaceAllStringFunc := []struct { + regex *regexp.Regexp + fn func(string) string + }{ + // multiple paragraphs blockquotes + { + regexp.MustCompile(`(?sm)^>>>(.+)$`), + func(src string) string { + // remove >>> prefix, might have leading \n + prefixRegexp := regexp.MustCompile(`^([\n])?>>>(.*)`) + src = prefixRegexp.ReplaceAllString(src, "$1$2") + // append > to start of line + appendRegexp := regexp.MustCompile(`(?m)^`) + return appendRegexp.ReplaceAllString(src, ">$0") + }, + }, + } for channelName, channelPosts := range posts { for postIdx, post := range channelPosts { - posts[channelName][postIdx].Text = regex.ReplaceAllString(post.Text, "[$2]($1)") + result := post.Text + + for _, rule := range regexReplaceAllString { + result = rule.regex.ReplaceAllString(result, rule.rpl) + } + + for _, rule := range regexReplaceAllStringFunc { + result = rule.regex.ReplaceAllStringFunc(result, rule.fn) + } + posts[channelName][postIdx].Text = result } } |