diff --git a/internal/controller/template_controller.go b/internal/controller/template_controller.go index 60eeed24..bfc2299f 100644 --- a/internal/controller/template_controller.go +++ b/internal/controller/template_controller.go @@ -115,6 +115,7 @@ func (tc *TemplateController) Index(ctx *gin.Context) { "data": data, "useTitle": UrlUseTitle, "page": templaterender.Paginator(page, req.PageSize, count), + "path": "questions", }) } @@ -147,7 +148,7 @@ func (tc *TemplateController) QuestionList(ctx *gin.Context) { }) } -func (tc *TemplateController) QuestionInfo301Jump(ctx *gin.Context, siteInfo *schema.TemplateSiteInfoResp) (jump bool, url string) { +func (tc *TemplateController) QuestionInfo301Jump(ctx *gin.Context, siteInfo *schema.TemplateSiteInfoResp, correctTitle bool) (jump bool, url string) { id := ctx.Param("id") title := ctx.Param("title") titleIsAnswerID := false @@ -168,7 +169,7 @@ func (tc *TemplateController) QuestionInfo301Jump(ctx *gin.Context, siteInfo *sc return true, url } else { //have title - if len(title) > 0 && !titleIsAnswerID { + if len(title) > 0 && !titleIsAnswerID && correctTitle { return false, "" } detail, err := tc.templateRenderController.QuestionDetail(ctx, id) @@ -184,20 +185,27 @@ func (tc *TemplateController) QuestionInfo301Jump(ctx *gin.Context, siteInfo *sc // QuestionInfo question and answers info func (tc *TemplateController) QuestionInfo(ctx *gin.Context) { id := ctx.Param("id") + title := ctx.Param("title") answerid := ctx.Param("answerid") - siteInfo := tc.SiteInfo(ctx) - jump, jumpurl := tc.QuestionInfo301Jump(ctx, siteInfo) - if jump { - ctx.Redirect(http.StatusMovedPermanently, jumpurl) - return - } + correctTitle := false detail, err := tc.templateRenderController.QuestionDetail(ctx, id) if err != nil { tc.Page404(ctx) return } + encodeTitle := htmltext.UrlTitle(detail.Title) + if encodeTitle == title { + correctTitle = true + } + + siteInfo := tc.SiteInfo(ctx) + jump, jumpurl := tc.QuestionInfo301Jump(ctx, siteInfo, correctTitle) + if jump { + ctx.Redirect(http.StatusMovedPermanently, jumpurl) + return + } // answers answerReq := &schema.AnswerListReq{ @@ -223,7 +231,6 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) { tc.Page404(ctx) return } - encodeTitle := htmltext.UrlTitle(detail.Title) siteInfo.Canonical = fmt.Sprintf("%s/questions/%s/%s", siteInfo.General.SiteUrl, id, encodeTitle) if siteInfo.SiteSeo.PermaLink == schema.PermaLinkQuestionID { siteInfo.Canonical = fmt.Sprintf("%s/questions/%s", siteInfo.General.SiteUrl, id) @@ -242,13 +249,15 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) { answerList := make([]*schema.SuggestedAnswerItem, 0) for _, answer := range answers { if answer.Adopted == schema.AnswerAdoptedEnable { - jsonLD.MainEntity.AcceptedAnswer.Type = "Answer" - jsonLD.MainEntity.AcceptedAnswer.Text = answer.HTML - jsonLD.MainEntity.AcceptedAnswer.UpvoteCount = answer.VoteCount - jsonLD.MainEntity.AcceptedAnswer.URL = fmt.Sprintf("%s/%s", siteInfo.Canonical, answer.ID) - jsonLD.MainEntity.AcceptedAnswer.Author.Type = "Person" - jsonLD.MainEntity.AcceptedAnswer.Author.Name = answer.UserInfo.DisplayName - + acceptedAnswerItem := &schema.AcceptedAnswerItem{} + acceptedAnswerItem.Type = "Answer" + acceptedAnswerItem.Text = answer.HTML + acceptedAnswerItem.DateCreated = time.Unix(answer.CreateTime, 0) + acceptedAnswerItem.UpvoteCount = answer.VoteCount + acceptedAnswerItem.URL = fmt.Sprintf("%s/%s", siteInfo.Canonical, answer.ID) + acceptedAnswerItem.Author.Type = "Person" + acceptedAnswerItem.Author.Name = answer.UserInfo.DisplayName + jsonLD.MainEntity.AcceptedAnswer = acceptedAnswerItem } else { item := &schema.SuggestedAnswerItem{} item.Type = "Answer" @@ -405,6 +414,10 @@ func (tc *TemplateController) html(ctx *gin.Context, code int, tpl string, siteI data["description"] = siteInfo.Description data["language"] = handler.GetLang(ctx) data["timezone"] = siteInfo.Interface.TimeZone + _, ok := data["path"] + if !ok { + data["path"] = "" + } ctx.HTML(code, tpl, data) } diff --git a/internal/schema/template_schema.go b/internal/schema/template_schema.go index 39f55429..7273f960 100644 --- a/internal/schema/template_schema.go +++ b/internal/schema/template_schema.go @@ -24,16 +24,17 @@ type QAPageJsonLD struct { Type string `json:"@type"` Name string `json:"name"` } `json:"author"` - AcceptedAnswer AcceptedAnswerItem `json:"acceptedAnswer"` + AcceptedAnswer *AcceptedAnswerItem `json:"acceptedAnswer,omitempty"` SuggestedAnswer []*SuggestedAnswerItem `json:"suggestedAnswer"` } `json:"mainEntity"` } type AcceptedAnswerItem struct { - Type string `json:"@type"` - Text string `json:"text"` - UpvoteCount int `json:"upvoteCount"` - URL string `json:"url"` + Type string `json:"@type"` + Text string `json:"text"` + DateCreated time.Time `json:"dateCreated"` + UpvoteCount int `json:"upvoteCount"` + URL string `json:"url"` Author struct { Type string `json:"@type"` Name string `json:"name"` diff --git a/ui/template/page.html b/ui/template/page.html index ef37bea5..1faab2f8 100644 --- a/ui/template/page.html +++ b/ui/template/page.html @@ -2,26 +2,26 @@