feat: support category page

master v0.0.3
xuthus5 4 months ago
parent 548350ca15
commit 06965a7ead
Signed by: xuthus5
GPG Key ID: A23CF9620CBB55F9
  1. 77
      output.go

@ -115,6 +115,7 @@ func (o *Outter) generate() {
o.generatePage()
o.generateArchives()
o.generateTags()
o.generateCategory()
}
var funcMap = template.FuncMap{
@ -372,7 +373,7 @@ func (o *Outter) generateTags() {
}
funcMap["title"] = func() string {
return instance.Page.Title
return "Tag: " + instance.Page.Title
}
funcMap["page_name"] = func() string {
return o.Config.Title
@ -403,6 +404,80 @@ func (o *Outter) generateTags() {
}
}
func (o *Outter) generateCategory() {
tagsTemplate := template.New("category")
var catBuffer = new(bytes.Buffer)
// 按标签归档
var mCats = make(map[string][]Post)
for _, post := range o.Posts {
newPost := post
newPost.MD = ""
mCats[post.Category] = append(mCats[post.Category], newPost)
}
for cat, posts := range mCats {
// 按时间归档
var m = make(map[string][]Post)
for _, post := range posts {
newPost := post
newPost.MD = ""
m[int2String(post.CreatedAt.Year())] = append(m[int2String(post.CreatedAt.Year())], newPost)
}
var postData []PostData
for year, _posts := range m {
postData = append(postData, PostData{
Key: year,
Posts: _posts,
})
}
sort.Slice(postData, func(i, j int) bool {
return postData[i].Key > postData[j].Key
})
var instance = PageOutter{
Page: Page{
Title: cat,
Link: cat,
},
Config: o.Config,
PostData: postData,
}
funcMap["title"] = func() string {
return "Category: " + instance.Page.Title
}
funcMap["page_name"] = func() string {
return o.Config.Title
}
tagsTemplate.Funcs(funcMap)
tagsTemplate, err := tagsTemplate.Parse(string(o.Theme.TagLayout))
if err != nil {
sfault("generate category page failed: %v", err)
}
// 入buffer
if err := tagsTemplate.Execute(catBuffer, instance); err != nil {
sfault("generate category page failed: %v", err)
}
// 创建tag文件夹
var tagDir = "./dist/category"
if !isExist(tagDir) {
if err := mkdir(tagDir); err != nil {
sfault("create category directory failed: %v", err)
}
}
// buffer写文件
var filename = fmt.Sprintf("%s/%s.html", tagDir, instance.Page.Link)
if err := os.WriteFile(filename, catBuffer.Bytes(), os.ModePerm); err != nil {
sfault("write category content failed: %v", err)
}
catBuffer.Reset()
}
}
func getSource(data interface{}, key string) interface{} {
meta, ok := data.(map[string]interface{})
if !ok {

Loading…
Cancel
Save