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.

update Canonical

aichy1268e0607470b7a
diff --git a/internal/controller/template_controller.go b/internal/controller/template_controller.go
index 9772d07f..e9b51e57 100644
--- a/internal/controller/template_controller.go
+++ b/internal/controller/template_controller.go
@@ -5,7 +5,6 @@ import (
"fmt"
"html/template"
"net/http"
- "net/url"
"regexp"
"time"
@@ -13,6 +12,7 @@ import (
templaterender "github.com/answerdev/answer/internal/controller/template_render"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service/siteinfo_common"
+ "github.com/answerdev/answer/pkg/htmltext"
"github.com/answerdev/answer/ui"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman/log"
@@ -160,14 +160,14 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
return
}
siteInfo := tc.SiteInfo(ctx)
- encodeTitle := url.QueryEscape("title")
+ encodeTitle := htmltext.UrlTitle(detail.Title)
siteInfo.Canonical = fmt.Sprintf("%s/questions/%s/%s", siteInfo.General.SiteUrl, id, encodeTitle)
jsonLD := &schema.QAPageJsonLD{}
jsonLD.Context = "https://schema.org"
jsonLD.Type = "QAPage"
jsonLD.MainEntity.Type = "Question"
jsonLD.MainEntity.Name = detail.Title
- jsonLD.MainEntity.Text = detail.HTML
+ jsonLD.MainEntity.Text = htmltext.ClearText(detail.HTML)
jsonLD.MainEntity.AnswerCount = int(answerCount)
jsonLD.MainEntity.UpvoteCount = detail.VoteCount
jsonLD.MainEntity.DateCreated = time.Unix(detail.CreateTime, 0)
@@ -177,7 +177,7 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
for _, answer := range answers {
item := &schema.SuggestedAnswerItem{}
item.Type = "Answer"
- item.Text = answer.HTML
+ item.Text = htmltext.ClearText(answer.HTML)
item.DateCreated = time.Unix(answer.CreateTime, 0)
item.UpvoteCount = answer.VoteCount
item.URL = fmt.Sprintf("%s/%s", siteInfo.Canonical, answer.ID)
diff --git a/internal/router/template_router.go b/internal/router/template_router.go
index 33a8a958..b72a54d4 100644
--- a/internal/router/template_router.go
+++ b/internal/router/template_router.go
@@ -30,7 +30,7 @@ func (a *TemplateRouter) RegisterTemplateRouter(r *gin.RouterGroup) {
r.GET("/questions", a.templateController.QuestionList)
r.GET("/questions/:id/", a.templateController.QuestionInfo)
r.GET("/questions/:id/:title/", a.templateController.QuestionInfo)
- r.GET("/questions/:id/:title/:answerid", a.templateController.QuestionList)
+ r.GET("/questions/:id/:title/:answerid", a.templateController.QuestionInfo)
r.GET("/tags", a.templateController.TagList)
r.GET("/tags/:tag", a.templateController.TagInfo)
diff --git a/pkg/htmltext/htmltext.go b/pkg/htmltext/htmltext.go
index e0463e10..0470ef9f 100644
--- a/pkg/htmltext/htmltext.go
+++ b/pkg/htmltext/htmltext.go
@@ -1,9 +1,11 @@
package htmltext
import (
- "github.com/grokify/html-strip-tags-go"
+ "net/url"
"regexp"
"strings"
+
+ strip "github.com/grokify/html-strip-tags-go"
)
// ClearText clear HTML, get the clear text
@@ -40,6 +42,24 @@ func ClearText(html string) (text string) {
return
}
+func UrlTitle(title string) (text string) {
+ title = ClearEmoji(title)
+ title = strings.ReplaceAll(title, " ", "-")
+ title = url.QueryEscape(title)
+ return title
+}
+
+func ClearEmoji(s string) string {
+ ret := ""
+ rs := []rune(s)
+ for i := 0; i < len(rs); i++ {
+ if len(string(rs[i])) != 4 {
+ ret += string(rs[i])
+ }
+ }
+ return ret
+}
+
// FetchExcerpt return the excerpt from the HTML string
func FetchExcerpt(html, trimMarker string, limit int) (text string) {
if len(html) == 0 {