Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

add question seo Json ld

aichy1261960bcfb4b62
diff --git a/internal/controller/template_controller.go b/internal/controller/template_controller.go
index 6c9a9700..9772d07f 100644
--- a/internal/controller/template_controller.go
+++ b/internal/controller/template_controller.go
@@ -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,
diff --git a/internal/schema/siteinfo_schema.go b/internal/schema/siteinfo_schema.go
index aace77bf..92c6153f 100644
--- a/internal/schema/siteinfo_schema.go
+++ b/internal/schema/siteinfo_schema.go
@@ -101,6 +101,7 @@ type TemplateSiteInfoResp struct {
Branding *SiteBrandingResp `json:"branding"`
Year string
Canonical string
+ JsonLD string
}
// UpdateSMTPConfigReq get smtp config request
diff --git a/internal/schema/template_schema.go b/internal/schema/template_schema.go
index 6446565e..5f67d57f 100644
--- a/internal/schema/template_schema.go
+++ b/internal/schema/template_schema.go
@@ -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"`
+}
diff --git a/ui/template/header.html b/ui/template/header.html
index d87cf718..baf712e5 100644
--- a/ui/template/header.html
+++ b/ui/template/header.html
@@ -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">