update Canonical

This commit is contained in:
aichy126 2022-12-02 16:42:40 +08:00
parent 1960bcfb4b
commit 8e0607470b
3 changed files with 26 additions and 6 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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 {