Skip to content

Commit f86e981

Browse files
ferhatelmasLinkinStars
authored andcommitted
fix: multi byte run boundary for cut long title
Signed-off-by: ferhat elmas <elmas.ferhat@gmail.com>
1 parent 9b64d7d commit f86e981

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

pkg/htmltext/htmltext.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,16 @@ func convertChinese(content string) string {
9696
}
9797

9898
func cutLongTitle(title string) string {
99-
if len(title) > 150 {
100-
return title[0:150]
99+
maxBytes := 150
100+
if len(title) <= maxBytes {
101+
return title
101102
}
102-
return title
103+
104+
truncated := title[:maxBytes]
105+
for len(truncated) > 0 && !utf8.ValidString(truncated) {
106+
truncated = truncated[:len(truncated)-1]
107+
}
108+
return truncated
103109
}
104110

105111
// FetchExcerpt return the excerpt from the HTML string

pkg/htmltext/htmltext_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package htmltext
2121

2222
import (
2323
"fmt"
24+
"strings"
2425
"testing"
2526

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

182+
func TestCutLongTitle(t *testing.T) {
183+
// Short title, no cutting needed
184+
short := "hello"
185+
assert.Equal(t, short, cutLongTitle(short))
186+
187+
// Exactly max bytes, no cutting needed
188+
exact150 := strings.Repeat("a", 150)
189+
assert.Equal(t, 150, len(cutLongTitle(exact150)))
190+
191+
// Just over max bytes, should be cut
192+
exact151 := strings.Repeat("a", 151)
193+
assert.Equal(t, 150, len(cutLongTitle(exact151)))
194+
195+
// Multi-byte rune at boundary gets removed properly
196+
asciiPart := strings.Repeat("a", 149) // 149 bytes
197+
multiByteChar := "中" // 3 bytes - will span bytes 149-151
198+
title := asciiPart + multiByteChar // 152 bytes total
199+
200+
assert.Equal(t, asciiPart, cutLongTitle(title))
201+
}
202+
181203
func TestFetchMatchedExcerpt(t *testing.T) {
182204
var (
183205
expected,

0 commit comments

Comments
 (0)