mirror of https://gitee.com/answerdev/answer.git
fix seo page
This commit is contained in:
parent
e3f8e37647
commit
783bb31cac
|
@ -115,6 +115,7 @@ func (tc *TemplateController) Index(ctx *gin.Context) {
|
||||||
"data": data,
|
"data": data,
|
||||||
"useTitle": UrlUseTitle,
|
"useTitle": UrlUseTitle,
|
||||||
"page": templaterender.Paginator(page, req.PageSize, count),
|
"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")
|
id := ctx.Param("id")
|
||||||
title := ctx.Param("title")
|
title := ctx.Param("title")
|
||||||
titleIsAnswerID := false
|
titleIsAnswerID := false
|
||||||
|
@ -168,7 +169,7 @@ func (tc *TemplateController) QuestionInfo301Jump(ctx *gin.Context, siteInfo *sc
|
||||||
return true, url
|
return true, url
|
||||||
} else {
|
} else {
|
||||||
//have title
|
//have title
|
||||||
if len(title) > 0 && !titleIsAnswerID {
|
if len(title) > 0 && !titleIsAnswerID && correctTitle {
|
||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
detail, err := tc.templateRenderController.QuestionDetail(ctx, id)
|
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
|
// QuestionInfo question and answers info
|
||||||
func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
|
func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
|
||||||
id := ctx.Param("id")
|
id := ctx.Param("id")
|
||||||
|
title := ctx.Param("title")
|
||||||
answerid := ctx.Param("answerid")
|
answerid := ctx.Param("answerid")
|
||||||
|
|
||||||
siteInfo := tc.SiteInfo(ctx)
|
correctTitle := false
|
||||||
jump, jumpurl := tc.QuestionInfo301Jump(ctx, siteInfo)
|
|
||||||
if jump {
|
|
||||||
ctx.Redirect(http.StatusMovedPermanently, jumpurl)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
detail, err := tc.templateRenderController.QuestionDetail(ctx, id)
|
detail, err := tc.templateRenderController.QuestionDetail(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tc.Page404(ctx)
|
tc.Page404(ctx)
|
||||||
return
|
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
|
// answers
|
||||||
answerReq := &schema.AnswerListReq{
|
answerReq := &schema.AnswerListReq{
|
||||||
|
@ -223,7 +231,6 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
|
||||||
tc.Page404(ctx)
|
tc.Page404(ctx)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
encodeTitle := htmltext.UrlTitle(detail.Title)
|
|
||||||
siteInfo.Canonical = fmt.Sprintf("%s/questions/%s/%s", siteInfo.General.SiteUrl, id, encodeTitle)
|
siteInfo.Canonical = fmt.Sprintf("%s/questions/%s/%s", siteInfo.General.SiteUrl, id, encodeTitle)
|
||||||
if siteInfo.SiteSeo.PermaLink == schema.PermaLinkQuestionID {
|
if siteInfo.SiteSeo.PermaLink == schema.PermaLinkQuestionID {
|
||||||
siteInfo.Canonical = fmt.Sprintf("%s/questions/%s", siteInfo.General.SiteUrl, id)
|
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)
|
answerList := make([]*schema.SuggestedAnswerItem, 0)
|
||||||
for _, answer := range answers {
|
for _, answer := range answers {
|
||||||
if answer.Adopted == schema.AnswerAdoptedEnable {
|
if answer.Adopted == schema.AnswerAdoptedEnable {
|
||||||
jsonLD.MainEntity.AcceptedAnswer.Type = "Answer"
|
acceptedAnswerItem := &schema.AcceptedAnswerItem{}
|
||||||
jsonLD.MainEntity.AcceptedAnswer.Text = answer.HTML
|
acceptedAnswerItem.Type = "Answer"
|
||||||
jsonLD.MainEntity.AcceptedAnswer.UpvoteCount = answer.VoteCount
|
acceptedAnswerItem.Text = answer.HTML
|
||||||
jsonLD.MainEntity.AcceptedAnswer.URL = fmt.Sprintf("%s/%s", siteInfo.Canonical, answer.ID)
|
acceptedAnswerItem.DateCreated = time.Unix(answer.CreateTime, 0)
|
||||||
jsonLD.MainEntity.AcceptedAnswer.Author.Type = "Person"
|
acceptedAnswerItem.UpvoteCount = answer.VoteCount
|
||||||
jsonLD.MainEntity.AcceptedAnswer.Author.Name = answer.UserInfo.DisplayName
|
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 {
|
} else {
|
||||||
item := &schema.SuggestedAnswerItem{}
|
item := &schema.SuggestedAnswerItem{}
|
||||||
item.Type = "Answer"
|
item.Type = "Answer"
|
||||||
|
@ -405,6 +414,10 @@ func (tc *TemplateController) html(ctx *gin.Context, code int, tpl string, siteI
|
||||||
data["description"] = siteInfo.Description
|
data["description"] = siteInfo.Description
|
||||||
data["language"] = handler.GetLang(ctx)
|
data["language"] = handler.GetLang(ctx)
|
||||||
data["timezone"] = siteInfo.Interface.TimeZone
|
data["timezone"] = siteInfo.Interface.TimeZone
|
||||||
|
_, ok := data["path"]
|
||||||
|
if !ok {
|
||||||
|
data["path"] = ""
|
||||||
|
}
|
||||||
|
|
||||||
ctx.HTML(code, tpl, data)
|
ctx.HTML(code, tpl, data)
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ type QAPageJsonLD struct {
|
||||||
Type string `json:"@type"`
|
Type string `json:"@type"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
} `json:"author"`
|
} `json:"author"`
|
||||||
AcceptedAnswer AcceptedAnswerItem `json:"acceptedAnswer"`
|
AcceptedAnswer *AcceptedAnswerItem `json:"acceptedAnswer,omitempty"`
|
||||||
SuggestedAnswer []*SuggestedAnswerItem `json:"suggestedAnswer"`
|
SuggestedAnswer []*SuggestedAnswerItem `json:"suggestedAnswer"`
|
||||||
} `json:"mainEntity"`
|
} `json:"mainEntity"`
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@ type QAPageJsonLD struct {
|
||||||
type AcceptedAnswerItem struct {
|
type AcceptedAnswerItem struct {
|
||||||
Type string `json:"@type"`
|
Type string `json:"@type"`
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
|
DateCreated time.Time `json:"dateCreated"`
|
||||||
UpvoteCount int `json:"upvoteCount"`
|
UpvoteCount int `json:"upvoteCount"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
Author struct {
|
Author struct {
|
||||||
|
|
|
@ -2,26 +2,26 @@
|
||||||
<ul class="d-inline-flex mb-0 pagination pagination-sm">
|
<ul class="d-inline-flex mb-0 pagination pagination-sm">
|
||||||
{{ if ne .page.Currpage 1 }}
|
{{ if ne .page.Currpage 1 }}
|
||||||
<li class="page-item">
|
<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>
|
class="visually-hidden">{{translator $.language "ui.pagination.prev"}}</span></a>
|
||||||
</li>
|
</li>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ range $value := .page.Pages }}
|
{{ range $value := .page.Pages }}
|
||||||
{{ if eq $.page.Currpage $value }}
|
{{ if eq $.page.Currpage $value }}
|
||||||
<li class="page-item active">
|
<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 class="visually-hidden">(current)</span>
|
||||||
</span>
|
</span>
|
||||||
</li>
|
</li>
|
||||||
{{ else }}
|
{{ else }}
|
||||||
<li class="page-item">
|
<li class="page-item">
|
||||||
<a class="page-link" href="?page={{$value}}">{{$value}}</a>
|
<a class="page-link" href="{{$.path}}?page={{$value}}">{{$value}}</a>
|
||||||
</li>
|
</li>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ if lt $.page.Currpage $.page.Totalpages }}
|
{{ if lt $.page.Currpage $.page.Totalpages }}
|
||||||
<li class="page-item">
|
<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>
|
class="visually-hidden">{{translator $.language "ui.pagination.next"}}</span></a>
|
||||||
</li>
|
</li>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
Loading…
Reference in New Issue