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

kumfoc7ee44d33c71
diff --git a/pkg/htmltext/htmltext.go b/pkg/htmltext/htmltext.go
index fa93ff2d..31ae1a22 100644
--- a/pkg/htmltext/htmltext.go
+++ b/pkg/htmltext/htmltext.go
@@ -8,6 +8,11 @@ import (
// ClearText clear HTML, get the clear text
func ClearText(html string) (text string) {
+ if len(html) == 0 {
+ text = html
+ return
+ }
+
var (
re *regexp.Regexp
codeReg = `(?ism)<(pre)>.*<\/pre>`
@@ -37,8 +42,19 @@ func ClearText(html string) (text string) {
// FetchExcerpt return the excerpt from the HTML string
func FetchExcerpt(html, trimMarker string, limit int) (text string) {
+ if len(html) == 0 {
+ text = html
+ return
+ }
+
text = ClearText(html)
runeText := []rune(text)
- text = string(runeText[0:limit]) + trimMarker
+ if len(runeText) <= limit {
+ text = string(runeText)
+ } else {
+ text = string(runeText[0:limit])
+ }
+
+ text += trimMarker
return
}