mirror of
https://github.com/axllent/mailpit.git
synced 2026-05-18 10:01:26 +02:00
Refactor html2text.Strip to return an error and handle it in storage and tools packages
This commit is contained in:
@@ -3,7 +3,7 @@ package html2text
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
@@ -30,18 +30,18 @@ func init() {
|
||||
}
|
||||
|
||||
// Strip will convert a HTML string to plain text
|
||||
func Strip(h string, includeLinks bool) string {
|
||||
func Strip(h string, includeLinks bool) (string, error) {
|
||||
h = spaceRe.ReplaceAllString(h, "</$1> <")
|
||||
h = brRe.ReplaceAllString(h, " ")
|
||||
h = imgRe.ReplaceAllString(h, " <$1")
|
||||
var buffer bytes.Buffer
|
||||
doc, err := html.Parse(strings.NewReader(h))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return "", fmt.Errorf("html2text: parsing HTML: %w", err)
|
||||
}
|
||||
|
||||
extract(doc, &buffer, includeLinks)
|
||||
return clean(buffer.String())
|
||||
return clean(buffer.String()), nil
|
||||
}
|
||||
|
||||
func extract(node *html.Node, buff *bytes.Buffer, includeLinks bool) {
|
||||
|
||||
@@ -57,7 +57,7 @@ func createSearchText(env *enmime.Envelope) string {
|
||||
b.WriteString(env.GetHeader("Reply-To") + " ")
|
||||
b.WriteString(env.GetHeader("Return-Path") + " ")
|
||||
|
||||
h := html2text.Strip(env.HTML, true)
|
||||
h, _ := html2text.Strip(env.HTML, true)
|
||||
if h != "" {
|
||||
b.WriteString(h + " ")
|
||||
} else {
|
||||
|
||||
@@ -20,13 +20,14 @@ func CreateSnippet(text, html string) string {
|
||||
}
|
||||
|
||||
if html != "" {
|
||||
data := html2text.Strip(html, false)
|
||||
data, err := html2text.Strip(html, false)
|
||||
if err == nil {
|
||||
if len(data) <= limit {
|
||||
return data
|
||||
}
|
||||
|
||||
if len(data) <= limit {
|
||||
return data
|
||||
return truncate(data, limit) + "..."
|
||||
}
|
||||
|
||||
return truncate(data, limit) + "..."
|
||||
}
|
||||
|
||||
if text != "" {
|
||||
|
||||
Reference in New Issue
Block a user