Merge branch 'feat/0.6.0/seo' into 'test'

Feat/0.6.0/seo

See merge request opensource/answer!323
This commit is contained in:
杨光富 2022-12-07 07:52:18 +00:00
commit 9c2e386e4c
6 changed files with 73 additions and 16 deletions

View File

@ -5,6 +5,7 @@ import (
"io/fs"
"math"
"os"
"regexp"
"strconv"
"strings"
"time"
@ -65,20 +66,41 @@ func NewHTTPServer(debug bool,
answerRouter.RegisterAnswerCmsAPIRouter(cmsauthV1)
funcMap := template.FuncMap{
"replaceHTMLTag": func(src string, tags ...string) string {
p := `(?U)<(\d+)>.+</(\d+)>`
re := regexp.MustCompile(p)
ms := re.FindAllStringSubmatch(src, -1)
for _, mi := range ms {
if mi[1] == mi[2] {
i, err := strconv.Atoi(mi[1])
if err != nil || len(tags) < i {
break
}
src = strings.ReplaceAll(src, mi[0], tags[i-1])
}
}
return src
},
"join": func(sep string, elems ...string) string {
return strings.Join(elems, sep)
},
"templateHTML": func(data string) template.HTML {
return template.HTML(data)
},
"translator": func(la i18n.Language, data string, params ...interface{}) string {
// todo
/*if len(params) > 0 && len(params)%2 == 0 {
trans := translator.GlobalTrans.Tr(la, data)
if len(params) > 0 && len(params)%2 == 0 {
for i := 0; i < len(params); i += 2 {
k := converter.InterfaceToString(params[i])
v := converter.InterfaceToString(params[i+1])
data = strings.ReplaceAll(data, "{{ "+k+" }}", v)
trans = strings.ReplaceAll(trans, "{{ "+k+" }}", v)
}
}*/
}
trans := translator.GlobalTrans.Tr(la, data)
return trans
},
"timeFormatISO": func(tz string, timestamp int64) string {
@ -116,7 +138,7 @@ func NewHTTPServer(debug bool,
}
if between >= 3600 && between < 3600*24 {
h := math.Floor(float64(between / 60))
h := math.Floor(float64(between / 3600))
trans = translator.GlobalTrans.Tr(la, "ui.dates.x_hours_ago")
return strings.ReplaceAll(trans, "{{count}}", strconv.FormatFloat(h, 'f', 0, 64))
}

View File

@ -78,7 +78,9 @@ func (tc *TemplateController) SiteInfo(ctx *gin.Context) *schema.TemplateSiteInf
// Index question list
func (tc *TemplateController) Index(ctx *gin.Context) {
req := &schema.QuestionSearch{}
req := &schema.QuestionSearch{
Order: "newest",
}
if handler.BindAndCheck(ctx, req) {
tc.Page404(ctx)
return
@ -101,7 +103,9 @@ func (tc *TemplateController) Index(ctx *gin.Context) {
}
func (tc *TemplateController) QuestionList(ctx *gin.Context) {
req := &schema.QuestionSearch{}
req := &schema.QuestionSearch{
Order: "newest",
}
if handler.BindAndCheck(ctx, req) {
tc.Page404(ctx)
return
@ -307,7 +311,7 @@ func (tc *TemplateController) Page404(ctx *gin.Context) {
func (tc *TemplateController) html(ctx *gin.Context, code int, tpl string, siteInfo *schema.TemplateSiteInfoResp, data gin.H) {
data["siteinfo"] = siteInfo
data["scriptPath"] = tc.scriptPath
data["scriptPath"] = "" //tc.scriptPath
data["cssPath"] = tc.cssPath
data["keywords"] = siteInfo.Keywords
if siteInfo.Description == "" {

View File

@ -2,6 +2,7 @@ package converter
import (
"fmt"
"github.com/segmentfault/pacman/log"
"strconv"
)
@ -25,6 +26,29 @@ func IntToString(data int64) string {
return fmt.Sprintf("%d", data)
}
// InterfaceToString converts data to string
// It will be used in template render
func InterfaceToString(data interface{}) string {
return fmt.Sprintf("%d", data)
switch t := data.(type) {
case int:
i := data.(int)
return strconv.Itoa(i)
case int8:
i := data.(int8)
return strconv.Itoa(int(i))
case int16:
i := data.(int16)
return strconv.Itoa(int(i))
case int32:
i := data.(int32)
return string(i)
case int64:
i := data.(int64)
return strconv.FormatInt(i, 10)
case string:
return data.(string)
default:
log.Warn("can't convert type:", t)
}
return ""
}

View File

@ -1,12 +1,11 @@
{{define "footer"}}
</div>
<footer class="bg-light py-3">
<div class="container">
<p class="text-center mb-0 fs-14 text-secondary">
Built on <a href="https://answer.dev/" target="_blank"> Answer </a>-
the open-source software that power Q&amp;A communities.<br />Made
with love © {{.siteinfo.Year}} {{.siteinfo.General.Name}}.
{{$cc := (join " " .siteinfo.Year .siteinfo.General.Name) -}}
{{- $ft := translator $.language "ui.footer.build_on" "cc" $cc -}}
{{templateHTML (replaceHTMLTag $ft "<a href=\"https://answer.dev/\" target=\"_blank\"> Answer </a>")}}
</p>
</div>
</footer>

View File

@ -21,10 +21,17 @@
class="fw-bold" title="Reputation">{{.UserInfo.Rank}}</span>
</div>
{{if eq .CreateTime .UpdateTime}}
<time class="text-secondary ms-1"
datetime="{{timeFormatISO $.timezone .CreateTime}}"
title="{{translatorTimeFormatLongDate $.language $.timezone .CreateTime}}">{{translator $.language "ui.question.asked"}} {{translatorTimeFormat $.language $.timezone .CreateTime}}
</time>
{{else if gt .UpdateTime 0}}
<time class="text-secondary ms-1"
datetime="{{timeFormatISO $.timezone .UpdateTime}}"
title="{{translatorTimeFormatLongDate $.language $.timezone .UpdateTime}}">{{translator $.language "ui.question.modified"}} {{translatorTimeFormat $.language $.timezone .UpdateTime}}
</time>
{{end}}
</div>
<div class="ms-0 ms-md-3 mt-2 mt-md-0">
<span><i class="br bi-hand-thumbs-up-fill"></i><em

View File

@ -12,8 +12,9 @@
</div>
<div>
<div class="mb-3 d-flex flex-wrap justify-content-between">
<h5 class="fs-5 text-nowrap mb-3 mb-md-0">{{ .questionCount }}
{{translator ($.language) ("ui.question.questions")}}</h5>
<h5 class="fs-5 text-nowrap mb-3 mb-md-0">
{{translator ($.language) "ui.question.x_questions" "count" .questionCount}}
</h5>
</div>
<div class="border-top border-bottom-0 list-group list-group-flush">
{{range .questionList}}