1
0
mirror of https://github.com/axllent/mailpit.git synced 2025-03-27 22:01:33 +02:00
mailpit/internal/tools/snippets.go

43 lines
820 B
Go
Raw Normal View History

2023-10-05 17:01:13 +13:00
package tools
import (
"regexp"
"strings"
)
// CreateSnippet returns a message snippet. It will use the HTML version (if it exists)
// and fall back to the text version.
func CreateSnippet(text, html string) string {
text = strings.TrimSpace(text)
html = strings.TrimSpace(html)
characters := 200
spaceRe := regexp.MustCompile(`\s+`)
nlRe := regexp.MustCompile(`\r?\n`)
if text == "" && html == "" {
return ""
}
if html != "" {
data := nlRe.ReplaceAllString(stripHTML(html), " ")
data = strings.TrimSpace(spaceRe.ReplaceAllString(data, " "))
if len(data) <= characters {
return data
}
return data[0:characters] + "..."
}
if text != "" {
text = spaceRe.ReplaceAllString(text, " ")
if len(text) <= characters {
return text
}
return text[0:characters] + "..."
}
return ""
}