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.

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

kumfo83a78737c3b4
diff --git a/pkg/htmltext/htmltext.go b/pkg/htmltext/htmltext.go
index 7f8e6e07..0130d11f 100644
--- a/pkg/htmltext/htmltext.go
+++ b/pkg/htmltext/htmltext.go
@@ -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
}
diff --git a/pkg/htmltext/htmltext_test.go b/pkg/htmltext/htmltext_test.go
index 9e405541..f67a20db 100644
--- a/pkg/htmltext/htmltext_test.go
+++ b/pkg/htmltext/htmltext_test.go
@@ -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) {