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: unified processing excerpt

kumfo8b312cde7e62
diff --git a/internal/repo/search_common/search_repo.go b/internal/repo/search_common/search_repo.go
index b2db83c3..22a4cbb5 100644
--- a/internal/repo/search_common/search_repo.go
+++ b/internal/repo/search_common/search_repo.go
@@ -3,6 +3,7 @@ package search_common
import (
"context"
"fmt"
+ "github.com/answerdev/answer/pkg/htmltext"
"strings"
"time"
@@ -25,7 +26,7 @@ var (
"`question`.`id`",
"`question`.`id` as `question_id`",
"`title`",
- "`original_text`",
+ "`parsed_text`",
"`question`.`created_at`",
"`user_id`",
"`vote_count`",
@@ -38,7 +39,7 @@ var (
"`answer`.`id` as `id`",
"`question_id`",
"`question`.`title` as `title`",
- "`answer`.`original_text` as `original_text`",
+ "`answer`.`parsed_text` as `parsed_text`",
"`answer`.`created_at`",
"`answer`.`user_id` as `user_id`",
"`answer`.`vote_count` as `vote_count`",
@@ -412,7 +413,7 @@ func (sr *searchRepo) parseResult(ctx context.Context, res []map[string][]byte)
object = schema.SearchObject{
ID: string(r["id"]),
Title: string(r["title"]),
- Excerpt: cutOutParsedText(string(r["original_text"])),
+ Excerpt: htmltext.FetchExcerpt(string(r["parsed_text"]), "...", 240),
CreatedAtParsed: tp.Unix(),
UserInfo: userInfo,
Tags: tags,
@@ -443,15 +444,6 @@ func (sr *searchRepo) userBasicInfoFormat(ctx context.Context, dbinfo *entity.Us
}
}
-func cutOutParsedText(parsedText string) string {
- parsedText = strings.TrimSpace(parsedText)
- idx := strings.Index(parsedText, "\n")
- if idx >= 0 {
- parsedText = parsedText[0:idx]
- }
- return parsedText
-}
-
func addRelevanceField(searchFields, words, fields []string) (res []string, args []interface{}) {
relevanceRes := []string{}
args = []interface{}{}
diff --git a/internal/service/report_backyard/report_backyard.go b/internal/service/report_backyard/report_backyard.go
index 947e7197..69b87c85 100644
--- a/internal/service/report_backyard/report_backyard.go
+++ b/internal/service/report_backyard/report_backyard.go
@@ -2,9 +2,8 @@ package report_backyard
import (
"context"
- "strings"
-
"github.com/answerdev/answer/internal/service/config"
+ "github.com/answerdev/answer/pkg/htmltext"
"github.com/answerdev/answer/internal/base/pager"
"github.com/answerdev/answer/internal/base/reason"
@@ -180,20 +179,20 @@ func (rs *ReportBackyardService) parseObject(ctx context.Context, resp *[]*schem
case "question":
r.QuestionID = questionId
r.Title = question.Title
- r.Excerpt = rs.cutOutTagParsedText(question.OriginalText)
+ r.Excerpt = htmltext.FetchExcerpt(question.ParsedText, "...", 240)
case "answer":
r.QuestionID = questionId
r.AnswerID = answerId
r.Title = question.Title
- r.Excerpt = rs.cutOutTagParsedText(answer.OriginalText)
+ r.Excerpt = htmltext.FetchExcerpt(answer.ParsedText, "...", 240)
case "comment":
r.QuestionID = questionId
r.AnswerID = answerId
r.CommentID = commentId
r.Title = question.Title
- r.Excerpt = rs.cutOutTagParsedText(cmt.OriginalText)
+ r.Excerpt = htmltext.FetchExcerpt(cmt.ParsedText, "...", 240)
}
// parse reason
@@ -214,12 +213,3 @@ func (rs *ReportBackyardService) parseObject(ctx context.Context, resp *[]*schem
}
resp = &res
}
-
-func (rs *ReportBackyardService) cutOutTagParsedText(parsedText string) string {
- parsedText = strings.TrimSpace(parsedText)
- idx := strings.Index(parsedText, "\n")
- if idx >= 0 {
- parsedText = parsedText[0:idx]
- }
- return parsedText
-}
diff --git a/internal/service/tag/tag_service.go b/internal/service/tag/tag_service.go
index cf6fc02e..6384eb82 100644
--- a/internal/service/tag/tag_service.go
+++ b/internal/service/tag/tag_service.go
@@ -3,9 +3,8 @@ package tag
import (
"context"
"encoding/json"
- "strings"
-
"github.com/answerdev/answer/internal/service/revision_common"
+ "github.com/answerdev/answer/pkg/htmltext"
"github.com/answerdev/answer/internal/base/pager"
"github.com/answerdev/answer/internal/base/reason"
@@ -344,12 +343,13 @@ func (ts *TagService) GetTagWithPage(ctx context.Context, req *schema.GetTagWith
resp := make([]*schema.GetTagPageResp, 0)
for _, tag := range tags {
+ excerpt := htmltext.FetchExcerpt(tag.ParsedText, "...", 240)
resp = append(resp, &schema.GetTagPageResp{
TagID: tag.ID,
SlugName: tag.SlugName,
DisplayName: tag.DisplayName,
- OriginalText: cutOutTagParsedText(tag.OriginalText),
- ParsedText: cutOutTagParsedText(tag.ParsedText),
+ OriginalText: excerpt,
+ ParsedText: excerpt,
FollowCount: tag.FollowCount,
QuestionCount: tag.QuestionCount,
IsFollower: ts.checkTagIsFollow(ctx, req.UserID, tag.ID),
@@ -371,12 +371,3 @@ func (ts *TagService) checkTagIsFollow(ctx context.Context, userID, tagID string
}
return followed
}
-
-func cutOutTagParsedText(parsedText string) string {
- parsedText = strings.TrimSpace(parsedText)
- idx := strings.Index(parsedText, "\n")
- if idx >= 0 {
- parsedText = parsedText[0:idx]
- }
- return parsedText
-}