Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pkg/htmltext/htmltext.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,16 @@ func convertChinese(content string) string {
}

func cutLongTitle(title string) string {
if len(title) > 150 {
return title[0:150]
maxBytes := 150
if len(title) <= maxBytes {
return title
}
return title

truncated := title[:maxBytes]
for len(truncated) > 0 && !utf8.ValidString(truncated) {
truncated = truncated[:len(truncated)-1]
}
return truncated
}

// FetchExcerpt return the excerpt from the HTML string
Expand Down
22 changes: 22 additions & 0 deletions pkg/htmltext/htmltext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package htmltext

import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -178,6 +179,27 @@ func TestFetchRangedExcerpt(t *testing.T) {
assert.Equal(t, expected, actual)
}

func TestCutLongTitle(t *testing.T) {
// Short title, no cutting needed
short := "hello"
assert.Equal(t, short, cutLongTitle(short))

// Exactly max bytes, no cutting needed
exact150 := strings.Repeat("a", 150)
assert.Equal(t, 150, len(cutLongTitle(exact150)))

// Just over max bytes, should be cut
exact151 := strings.Repeat("a", 151)
assert.Equal(t, 150, len(cutLongTitle(exact151)))

// Multi-byte rune at boundary gets removed properly
asciiPart := strings.Repeat("a", 149) // 149 bytes
multiByteChar := "中" // 3 bytes - will span bytes 149-151
title := asciiPart + multiByteChar // 152 bytes total

assert.Equal(t, asciiPart, cutLongTitle(title))
}

func TestFetchMatchedExcerpt(t *testing.T) {
var (
expected,
Expand Down