Refactor html2text.Strip to return an error and handle it in storage and tools packages

This commit is contained in:
Ralph Slooten
2026-05-09 17:21:36 +12:00
parent 6e2c42d2bc
commit c1fbbffded
3 changed files with 11 additions and 10 deletions

View File

@@ -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) {