Merge branch 'feat/0.7.0/seo' into feat/0.7.0/user-manage

This commit is contained in:
aichy126 2022-12-13 17:59:17 +08:00
commit ba4b26229f
3 changed files with 39 additions and 25 deletions

View File

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

View File

@ -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"`

View File

@ -2,26 +2,26 @@
<ul class="d-inline-flex mb-0 pagination pagination-sm">
{{ if ne .page.Currpage 1 }}
<li class="page-item">
<a class="page-link" href="?page={{$.page.Prevpage}}"><span aria-hidden="true">{{translator $.language "ui.pagination.prev"}}</span><span
<a class="page-link" href="{{$.path}}?page={{$.page.Prevpage}}"><span aria-hidden="true">{{translator $.language "ui.pagination.prev"}}</span><span
class="visually-hidden">{{translator $.language "ui.pagination.prev"}}</span></a>
</li>
{{ end }}
{{ range $value := .page.Pages }}
{{ if eq $.page.Currpage $value }}
<li class="page-item active">
<span class="page-link" href="?page={{$value}}">{{$value}}
<span class="page-link" href="{{$.path}}?page={{$value}}">{{$value}}
<span class="visually-hidden">(current)</span>
</span>
</li>
{{ else }}
<li class="page-item">
<a class="page-link" href="?page={{$value}}">{{$value}}</a>
<a class="page-link" href="{{$.path}}?page={{$value}}">{{$value}}</a>
</li>
{{ end }}
{{ end }}
{{ if lt $.page.Currpage $.page.Totalpages }}
<li class="page-item">
<a class="page-link" href="?page={{$.page.Nextpage}}"><span aria-hidden="true">{{translator $.language "ui.pagination.next"}}</span><span
<a class="page-link" href="{{$.path}}?page={{$.page.Nextpage}}"><span aria-hidden="true">{{translator $.language "ui.pagination.next"}}</span><span
class="visually-hidden">{{translator $.language "ui.pagination.next"}}</span></a>
</li>
{{ end }}