From 6d08a5ff6c541d7633bbbb96b5e1126bd711a70d Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 25 Aug 2018 17:00:31 +0300 Subject: [PATCH] Use inline code tags for single-line whatsapp monospace blocks --- portal.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/portal.go b/portal.go index 51ca508..a32e2fc 100644 --- a/portal.go +++ b/portal.go @@ -309,7 +309,6 @@ var boldRegex = regexp.MustCompile("([\\s>_~]|^)\\*(.+?)\\*([^a-zA-Z\\d]|$)") var strikethroughRegex = regexp.MustCompile("([\\s>_*]|^)~(.+?)~([^a-zA-Z\\d]|$)") var whatsAppFormat = map[*regexp.Regexp]string{ - codeBlockRegex: "
$1
", italicRegex: "$1$2$3", boldRegex: "$1$2$3", strikethroughRegex: "$1$2$3", @@ -320,6 +319,13 @@ func (portal *Portal) ParseWhatsAppFormat(input string) string { for regex, replacement := range whatsAppFormat { output = regex.ReplaceAllString(output, replacement) } + output = codeBlockRegex.ReplaceAllStringFunc(output, func(str string) string { + str = str[3 : len(str)-3] + if strings.ContainsRune(str, '\n') { + return fmt.Sprintf("
%s
", str) + } + return fmt.Sprintf("%s", str) + }) return output }