Compare commits
4 Commits
509b6b8a8a
...
main
Author | SHA1 | Date | |
---|---|---|---|
ecd53a6741 | |||
4786a21a56 | |||
f1c56bdf8b | |||
2556914486 |
35
hugo.go
35
hugo.go
@ -4,17 +4,23 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PostWithDate struct {
|
||||
Post string
|
||||
Date time.Time
|
||||
}
|
||||
|
||||
func getAllPosts(dir string) ([]string, error) {
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
|
||||
posts := make([]string, 0)
|
||||
posts := make([]PostWithDate, 0)
|
||||
for _, entry := range entries {
|
||||
if filepath.Ext(entry.Name()) != ".md" {
|
||||
continue
|
||||
@ -25,10 +31,31 @@ func getAllPosts(dir string) ([]string, error) {
|
||||
return []string{}, err
|
||||
}
|
||||
|
||||
posts = append(posts, string(content))
|
||||
postInfo, err := getPostInfo(string(content))
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
|
||||
posts = append(posts, PostWithDate{
|
||||
Post: string(content),
|
||||
Date: postInfo.Date,
|
||||
})
|
||||
}
|
||||
|
||||
return posts, nil
|
||||
slices.SortFunc(posts, func(a PostWithDate, b PostWithDate) int {
|
||||
if a.Date.Before(b.Date) {
|
||||
return 1
|
||||
} else {
|
||||
return -1
|
||||
}
|
||||
})
|
||||
|
||||
justPosts := make([]string, len(posts))
|
||||
for i, v := range posts {
|
||||
justPosts[i] = v.Post
|
||||
}
|
||||
|
||||
return justPosts, nil
|
||||
}
|
||||
|
||||
type postInfo struct {
|
||||
@ -61,7 +88,7 @@ func getPostInfo(post string) (postInfo, error) {
|
||||
case "title":
|
||||
info.Title = splitLine[1]
|
||||
case "date":
|
||||
t, err := time.Parse(time.RFC3339, splitLine[1])
|
||||
t, err := time.Parse(time.DateOnly, splitLine[1][1:len(splitLine[1])-1])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
Reference in New Issue
Block a user