add question seo Json ld

This commit is contained in:
aichy126 2022-12-02 16:16:49 +08:00
parent 332ec7c966
commit 1960bcfb4b
4 changed files with 69 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package controller
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
@ -127,9 +128,6 @@ func (tc *TemplateController) QuestionList(ctx *gin.Context) {
func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
id := ctx.Param("id")
answerid := ctx.Param("answerid")
siteInfo := tc.SiteInfo(ctx)
encodeTitle := url.QueryEscape("title")
siteInfo.Canonical = fmt.Sprintf("%s/questions/%s/%s", siteInfo.General.SiteUrl, id, encodeTitle)
detail, err := tc.templateRenderController.QuestionDetail(ctx, id)
if err != nil {
@ -145,7 +143,7 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
PageSize: 999,
LoginUserID: "",
}
answers, _, err := tc.templateRenderController.AnswerList(ctx, answerReq)
answers, answerCount, err := tc.templateRenderController.AnswerList(ctx, answerReq)
if err != nil {
tc.Page404(ctx)
return
@ -161,6 +159,37 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
tc.Page404(ctx)
return
}
siteInfo := tc.SiteInfo(ctx)
encodeTitle := url.QueryEscape("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.AnswerCount = int(answerCount)
jsonLD.MainEntity.UpvoteCount = detail.VoteCount
jsonLD.MainEntity.DateCreated = time.Unix(detail.CreateTime, 0)
jsonLD.MainEntity.Author.Type = "Person"
jsonLD.MainEntity.Author.Name = detail.UserInfo.DisplayName
answerList := make([]*schema.SuggestedAnswerItem, 0)
for _, answer := range answers {
item := &schema.SuggestedAnswerItem{}
item.Type = "Answer"
item.Text = answer.HTML
item.DateCreated = time.Unix(answer.CreateTime, 0)
item.UpvoteCount = answer.VoteCount
item.URL = fmt.Sprintf("%s/%s", siteInfo.Canonical, answer.ID)
item.Author.Type = "Person"
item.Author.Name = answer.UserInfo.DisplayName
answerList = append(answerList, item)
}
jsonLD.MainEntity.SuggestedAnswer = answerList
jsonLDStr, err := json.Marshal(jsonLD)
if err == nil {
siteInfo.JsonLD = `<script data-react-helmet="true" type="application/ld+json">` + string(jsonLDStr) + ` </script>`
}
ctx.HTML(http.StatusOK, "question-detail.html", gin.H{
"id": id,

View File

@ -101,6 +101,7 @@ type TemplateSiteInfoResp struct {
Branding *SiteBrandingResp `json:"branding"`
Year string
Canonical string
JsonLD string
}
// UpdateSMTPConfigReq get smtp config request

View File

@ -1,5 +1,7 @@
package schema
import "time"
type Paginator struct {
Pages []int
Totalpages int
@ -7,3 +9,33 @@ type Paginator struct {
Nextpage int
Currpage int
}
type QAPageJsonLD struct {
Context string `json:"@context"`
Type string `json:"@type"`
MainEntity struct {
Type string `json:"@type"`
Name string `json:"name"`
Text string `json:"text"`
AnswerCount int `json:"answerCount"`
UpvoteCount int `json:"upvoteCount"`
DateCreated time.Time `json:"dateCreated"`
Author struct {
Type string `json:"@type"`
Name string `json:"name"`
} `json:"author"`
SuggestedAnswer []*SuggestedAnswerItem `json:"suggestedAnswer"`
} `json:"mainEntity"`
}
type SuggestedAnswerItem struct {
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"`
} `json:"author"`
}

View File

@ -10,9 +10,12 @@
<link rel="manifest" href="/manifest.json"/>
<link href="{{.cssPath}}" rel="stylesheet" />
<!-- <script defer="defer" src="{{.scriptPath}}"></script> -->
{{if $.siteinfo.JsonLD }}{{ .siteinfo.JsonLD | templateHTML}}{{end}}
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root">
<nav id="header" class="sticky-top navbar navbar-expand-lg navbar-dark">