From 45bf1fd63a62b46fc80fe5143e667f6779705a0d Mon Sep 17 00:00:00 2001 From: Wim Date: Sun, 27 Oct 2019 01:10:59 +0200 Subject: [PATCH] Convert slack bold/strike to correct markdown (slack). Fixes #918 (#930) --- bridge/slack/handlers.go | 1 + bridge/slack/helpers.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/bridge/slack/handlers.go b/bridge/slack/handlers.go index 44a57db0..829cfb6a 100644 --- a/bridge/slack/handlers.go +++ b/bridge/slack/handlers.go @@ -30,6 +30,7 @@ func (b *Bslack) handleSlack() { message.Text = b.replaceVariable(message.Text) message.Text = b.replaceChannel(message.Text) message.Text = b.replaceURL(message.Text) + message.Text = b.replaceb0rkedMarkDown(message.Text) message.Text = html.UnescapeString(message.Text) // Add the avatar diff --git a/bridge/slack/helpers.go b/bridge/slack/helpers.go index 7c012a4b..b95ae878 100644 --- a/bridge/slack/helpers.go +++ b/bridge/slack/helpers.go @@ -188,6 +188,36 @@ func (b *Bslack) replaceURL(text string) string { return text } +func (b *Bslack) replaceb0rkedMarkDown(text string) string { + // taken from https://github.com/mattermost/mattermost-server/blob/master/app/slackimport.go + // + regexReplaceAllString := []struct { + regex *regexp.Regexp + rpl string + }{ + // 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)^>`), + ">", + }, + } + for _, rule := range regexReplaceAllString { + text = rule.regex.ReplaceAllString(text, rule.rpl) + } + return text +} + func (b *Bslack) replaceCodeFence(text string) string { return codeFenceRE.ReplaceAllString(text, "```") }