Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat: get text excerpt

kumfoad8cb70d4977
diff --git a/pkg/htmltext/htmltext.go b/pkg/htmltext/htmltext.go
index 0694d990..fa93ff2d 100644
--- a/pkg/htmltext/htmltext.go
+++ b/pkg/htmltext/htmltext.go
@@ -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
}
diff --git a/pkg/htmltext/htmltext_test.go b/pkg/htmltext/htmltext_test.go
index 353ca20b..71aafbb6 100644
--- a/pkg/htmltext/htmltext_test.go
+++ b/pkg/htmltext/htmltext_test.go
@@ -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)
}