-
Notifications
You must be signed in to change notification settings - Fork 2
/
profile.go
48 lines (40 loc) · 1.03 KB
/
profile.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
// SPDX-License-Identifier: MIT
package loader
import (
"strings"
"unicode"
)
// Profile 用于生成 github.com/profile 中的 README.md 内容
type Profile struct {
// 以下字段提供了类似于以下格式的 markdown 内容:
// ### Title
//
// post1
// post2
// post3
//
// ##### Footer
//
// Title 和 Footer 的前缀 # 是固定的,不需要用户给字,即使用户给了,也会被删除。
Title string `yaml:"title"`
Footer string `yaml:"footer"` // 页脚
Size int `yaml:"size"` // 显示最近添加的文章条数
}
func (p *Profile) sanitize() *FieldError {
if p.Title == "" {
return &FieldError{Field: "title", Message: "不能为空"}
}
p.Title = "### " + trimHeadPrefix(p.Title)
if p.Size <= 0 {
return &FieldError{Field: "size", Message: "必须大于 0"}
}
if p.Footer != "" {
p.Footer = "##### " + trimHeadPrefix(p.Footer)
}
return nil
}
func trimHeadPrefix(s string) string {
return strings.TrimLeftFunc(s, func(r rune) bool {
return unicode.IsSpace(r) || r == '#'
})
}