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: fetch excerpt, if contain link, show link name

kumfo42f315423382
diff --git a/pkg/htmltext/htmltext.go b/pkg/htmltext/htmltext.go
index 31ae1a22..e0463e10 100644
--- a/pkg/htmltext/htmltext.go
+++ b/pkg/htmltext/htmltext.go
@@ -17,8 +17,8 @@ func ClearText(html string) (text string) {
re *regexp.Regexp
codeReg = `(?ism)<(pre)>.*<\/pre>`
codeRepl = "{code...}"
- linkReg = `(?ism)<a.*?[^<]>.*?<\/a>`
- linkRepl = "[link]"
+ linkReg = `(?ism)<a.*?[^<]>(.*)?<\/a>`
+ linkRepl = " [$1] "
spaceReg = ` +`
spaceRepl = " "
)
diff --git a/pkg/htmltext/htmltext_test.go b/pkg/htmltext/htmltext_test.go
index 71aafbb6..a320ef12 100644
--- a/pkg/htmltext/htmltext_test.go
+++ b/pkg/htmltext/htmltext_test.go
@@ -17,8 +17,8 @@ func TestClearText(t *testing.T) {
assert.Equal(t, expected, clearedText)
// test link clear text
- expected = "hello[link]"
- clearedText = ClearText("<p>hello<a href=\"http://example.com/\">example.com</a></p>")
+ expected = "hello [example.com]"
+ clearedText = ClearText("<p>hello <a href=\"http://example.com/\">example.com</a></p>")
assert.Equal(t, expected, clearedText)
clearedText = ClearText("<p>hello<a href=\"https://example.com/\">example.com</a></p>")
assert.Equal(t, expected, clearedText)