fix(htmltext): FetchExcerpt if actual string length <= expected string length, no need trim marker

This commit is contained in:
kumfo 2022-12-27 10:46:34 +08:00
parent 7085cfa7ed
commit 83a78737c3
2 changed files with 6 additions and 3 deletions

View File

@ -75,11 +75,10 @@ func FetchExcerpt(html, trimMarker string, limit int) (text string) {
runeText := []rune(text)
if len(runeText) <= limit {
text = string(runeText)
} else {
text = string(runeText[0:limit])
return
}
text += trimMarker
text = string(runeText[0:limit]) + trimMarker
return
}

View File

@ -50,6 +50,10 @@ func TestFetchExcerpt(t *testing.T) {
expected = "hello你好😂..."
text = FetchExcerpt("<p>hello你好😂world</p>", "...", 8)
assert.Equal(t, expected, text)
expected = "hello你好"
text = FetchExcerpt("<p>hello你好</p>", "...", 8)
assert.Equal(t, expected, text)
}
func TestUrlTitle(t *testing.T) {