/
sitemap.go
68 lines (57 loc) · 1.61 KB
/
sitemap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: MIT
package loader
import "github.com/issue9/sliceutil"
// Sitemap sitemap 相关的配置
type Sitemap struct {
Title string `yaml:"title"`
Priority float64 `yaml:"priority"` // 默认的优先级
Changefreq string `yaml:"changefreq"` // 默认的更新频率
EnableTag bool `yaml:"enableTag,omitempty"` // 是否将标签相关的页面写入 sitemap
// 文章可以指定一个专门的值
PostPriority float64 `yaml:"postPriority"`
PostChangefreq string `yaml:"postChangefreq"`
}
func (s *Sitemap) sanitize() *FieldError {
chkPriority := func(v float64, field string) *FieldError {
if v > 1 || v < 0 {
return &FieldError{Message: "介于[0,1]之间的浮点数", Field: field, Value: v}
}
return nil
}
chkChangefreq := func(v, field string) *FieldError {
if !inStrings(v, changereqs) {
return &FieldError{Message: "取值不正确", Field: field, Value: v}
}
return nil
}
if s.Title == "" {
return &FieldError{Message: "不能为空", Field: "title"}
}
if err := chkPriority(s.Priority, "priority"); err != nil {
return err
}
if err := chkPriority(s.PostPriority, "postPriority"); err != nil {
return err
}
if err := chkChangefreq(s.Changefreq, "changefreq"); err != nil {
return err
}
if err := chkChangefreq(s.PostChangefreq, "postChangefreq"); err != nil {
return err
}
return nil
}
var changereqs = []string{
"never",
"yearly",
"monthly",
"weekly",
"daily",
"hourly",
"always",
}
func inStrings(val string, values []string) bool {
return sliceutil.Count(values, func(i int) bool {
return values[i] == val
}) > 0
}