소스 검색

Use inline code tags for single-line whatsapp monospace blocks

Tulir Asokan 6 년 전
부모
커밋
6d08a5ff6c
1개의 변경된 파일7개의 추가작업 그리고 1개의 파일을 삭제
  1. 7 1
      portal.go

+ 7 - 1
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:     "<pre>$1</pre>",
 	italicRegex:        "$1<em>$2</em>$3",
 	boldRegex:          "$1<strong>$2</strong>$3",
 	strikethroughRegex: "$1<del>$2</del>$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("<pre><code>%s</code></pre>", str)
+		}
+		return fmt.Sprintf("<code>%s</code>", str)
+	})
 	return output
 }