feat: get text excerpt

This commit is contained in:
kumfo 2022-11-07 14:29:49 +08:00
parent 8f567e0abd
commit ad8cb70d49
2 changed files with 4 additions and 4 deletions

View File

@ -39,6 +39,6 @@ func ClearText(html string) (text string) {
func FetchExcerpt(html, trimMarker string, limit int) (text string) {
text = ClearText(html)
runeText := []rune(text)
text = string(runeText[0:limit])
text = string(runeText[0:limit]) + trimMarker
return
}

View File

@ -35,17 +35,17 @@ func TestFetchExcerpt(t *testing.T) {
)
// test english string
expected = "hello"
expected = "hello..."
text = FetchExcerpt("<p>hello world</p>", "...", 5)
assert.Equal(t, expected, text)
// test mixed string
expected = "hello你好"
expected = "hello你好..."
text = FetchExcerpt("<p>hello你好world</p>", "...", 7)
assert.Equal(t, expected, text)
// test mixed string with emoticon
expected = "hello你好😂"
expected = "hello你好😂..."
text = FetchExcerpt("<p>hello你好😂world</p>", "...", 8)
assert.Equal(t, expected, text)
}