Merge branch 'feat/0.6.0/seo' of git.backyard.segmentfault.com:opensource/answer into feat/0.6.0/seo

This commit is contained in:
aichy126 2022-12-06 12:19:57 +08:00
commit dcd8108fa5
7 changed files with 81 additions and 58 deletions

View File

@ -68,8 +68,18 @@ func NewHTTPServer(debug bool,
"templateHTML": func(data string) template.HTML {
return template.HTML(data)
},
"translator": func(la i18n.Language, data string) string {
return translator.GlobalTrans.Tr(la, data)
"translator": func(la i18n.Language, data string, params ...interface{}) string {
// todo
/*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 := translator.GlobalTrans.Tr(la, data)
return trans
},
"timeFormatISO": func(tz string, timestamp int64) string {
_, _ = time.LoadLocation(tz)

View File

@ -9,6 +9,8 @@ import (
"strings"
"time"
"github.com/segmentfault/pacman/i18n"
"github.com/answerdev/answer/internal/base/handler"
templaterender "github.com/answerdev/answer/internal/controller/template_render"
"github.com/answerdev/answer/internal/schema"
@ -319,6 +321,7 @@ func (tc *TemplateController) html(ctx *gin.Context, code int, tpl string, siteI
data["siteinfo"] = siteInfo
data["scriptPath"] = tc.scriptPath
data["cssPath"] = tc.cssPath
data["language"] = i18n.LanguageChinese //handler.GetLang(ctx)
data["keywords"] = siteInfo.Keywords
if siteInfo.Description == "" {
siteInfo.Description = siteInfo.General.Description

View File

@ -24,3 +24,7 @@ func StringToInt(str string) int {
func IntToString(data int64) string {
return fmt.Sprintf("%d", data)
}
func InterfaceToString(data interface{}) string {
return fmt.Sprintf("%d", data)
}

View File

@ -17,30 +17,35 @@
<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">
<div class="d-flex align-items-center container">
<button aria-controls="navBarContent" id="navBarToggle" type="button" aria-label="Toggle navigation"
class="answer-navBar me-2 navbar-toggler collapsed">
<span class="navbar-toggler-icon"></span>
</button>
<div class="d-flex justify-content-between align-items-center nav-grow flex-nowrap">
<a class="lh-1 me-0 me-sm-3 navbar-brand" href="/">
{{.siteinfo.General.Name}}
</a>
</div>
<div class="me-auto navbar-collapse collapse" id="navBarContent">
<hr class="hr lg-none mb-2" style="margin-top: 12px" />
<div class="col-md-4">
<div class="navbar-nav">
<a class="nav-link" href="/questions">Questions</a><a class="nav-link" href="/tags">Tags</a>
</div>
</div>
<hr class="hr lg-none mt-2" />
<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">
<div class="d-flex align-items-center container">
<button aria-controls="navBarContent" id="navBarToggle" type="button"
aria-label="Toggle navigation"
class="answer-navBar me-2 navbar-toggler collapsed">
<span class="navbar-toggler-icon"></span>
</button>
<div
class="d-flex justify-content-between align-items-center nav-grow flex-nowrap">
<a class="lh-1 me-0 me-sm-3 navbar-brand" href="/">
{{.siteinfo.General.Name}}
</a>
</div>
<div class="me-auto navbar-collapse collapse" id="navBarContent">
<hr class="hr lg-none mb-2" style="margin-top: 12px"/>
<div class="col-md-4">
<div class="navbar-nav">
<a class="nav-link"
href="/questions">{{translator $.language "ui.header.nav.question"}}</a>
<a class="nav-link"
href="/tags">{{translator $.language "ui.header.nav.tag"}}</a>
</div>
</div>
<hr class="hr lg-none mt-2"/>
</div>
</div>
</nav>
<div class="position-relative page-wrap">
{{end}}
</div>
</div>
</nav>
<div class="position-relative page-wrap">
{{end}}

View File

@ -2,8 +2,8 @@
<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">Prev</span><span
class="visually-hidden">Prev</span></a>
<a class="page-link" href="?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 }}
@ -21,8 +21,8 @@
{{ end }}
{{ if lt $.page.Currpage $.page.Totalpages }}
<li class="page-item">
<a class="page-link" href="?page={{$.page.Nextpage}}"><span aria-hidden="true">Next</span><span
class="visually-hidden">Next</span></a>
<a class="page-link" href="?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 }}
</ul>

View File

@ -1,21 +1,22 @@
{{template "header" . }}
<div class="pt-4 mt-2 mb-5 container">
<div class="justify-content-center row">
<div class="col-xxl-7 col-lg-8 col-sm-12">
<div class="tag-box mb-5">
<h3 class="mb-3">
<a class="link-dark" href="/tags/{{$.tag.SlugName}}">{{$.tag.SlugName}}</a>
</h3>
<p class="text-break">
{{templateHTML $.tag.ParsedText}}
</p>
</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}} Questions</h5>
</div>
<div class="border-top border-bottom-0 list-group list-group-flush">
{{range .questionList}}
<div class="justify-content-center row">
<div class="col-xxl-7 col-lg-8 col-sm-12">
<div class="tag-box mb-5">
<h3 class="mb-3">
<a class="link-dark" href="/tags/{{$.tag.SlugName}}">{{$.tag.SlugName}}</a>
</h3>
<p class="text-break">
{{templateHTML $.tag.ParsedText}}
</p>
</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>
</div>
<div class="border-top border-bottom-0 list-group list-group-flush">
{{range .questionList}}
<div class="border-bottom pt-3 pb-2 px-0 list-group-item">
<h5 class="text-wrap text-break">
<a class="link-dark" href="/questions/{{.ID}}">{{.Title}}</a>
@ -30,8 +31,8 @@
</div>
<time class="text-secondary ms-1"
datetime="2022-11-29T07:38:07.000Z"
title="Nov 29, 2022 at 15:38">asked 1h ago
datetime="{{timeFormatISO $.timezone .CreateTime}}"
title="{{translatorTimeFormatLongDate $.language $.timezone .CreateTime}}">{{translator $.language "ui.question.asked"}} {{translatorTimeFormat $.language $.timezone .CreateTime}}
</time>
</div>
<div class="ms-0 ms-md-3 mt-2 mt-md-0">
@ -54,13 +55,13 @@
</div>
</div>
{{end}}
</div>
<div class="mt-4 mb-2 d-flex justify-content-center"></div>
</div>
</div>
<div class="d-flex justify-content-center">
{{template "page" .}}
</div>
<div class="mt-4 mb-2 d-flex justify-content-center"></div>
</div>
</div>
<div class="d-flex justify-content-center">
{{template "page" .}}
</div>
</div>
</div>
{{template "footer" .}}

View File

@ -12,7 +12,7 @@
<div class="py-3 my-3 container">
<div class="mb-4 d-flex justify-content-center row">
<div class="col-xxl-10 col-sm-12">
<h3 class="mb-4">Tags</h3>
<h3 class="mb-4">{{translator $.language "ui.page_title.tags"}}</h3>
</div>
<div class="mt-4 col-xxl-10 col-sm-12">
<div class="row">
@ -26,7 +26,7 @@
</p>
<div class="d-flex align-items-center">
<span class="text-secondary fs-14 text-nowrap">{{.QuestionCount}}
questions</span>
{{translator $.language "ui.tags.tag_label"}}</span>
</div>
</div>
</div>