mirror of https://gitee.com/answerdev/answer.git
feat: get text excerpt
This commit is contained in:
parent
8f567e0abd
commit
ad8cb70d49
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue