Compare commits
2 Commits
509b6b8a8a
...
f1c56bdf8b
Author | SHA1 | Date | |
---|---|---|---|
f1c56bdf8b | |||
2556914486 |
37
hugo.go
37
hugo.go
@ -2,19 +2,26 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type PostWithDate struct {
|
||||||
|
Post string
|
||||||
|
Date time.Time
|
||||||
|
}
|
||||||
|
|
||||||
func getAllPosts(dir string) ([]string, error) {
|
func getAllPosts(dir string) ([]string, error) {
|
||||||
entries, err := os.ReadDir(dir)
|
entries, err := os.ReadDir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []string{}, err
|
return []string{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
posts := make([]string, 0)
|
posts := make([]PostWithDate, 0)
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
if filepath.Ext(entry.Name()) != ".md" {
|
if filepath.Ext(entry.Name()) != ".md" {
|
||||||
continue
|
continue
|
||||||
@ -25,10 +32,31 @@ func getAllPosts(dir string) ([]string, error) {
|
|||||||
return []string{}, err
|
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 {
|
type postInfo struct {
|
||||||
@ -61,7 +89,8 @@ func getPostInfo(post string) (postInfo, error) {
|
|||||||
case "title":
|
case "title":
|
||||||
info.Title = splitLine[1]
|
info.Title = splitLine[1]
|
||||||
case "date":
|
case "date":
|
||||||
t, err := time.Parse(time.RFC3339, splitLine[1])
|
fmt.Println(splitLine[1])
|
||||||
|
t, err := time.Parse(time.DateOnly, splitLine[1][1:len(splitLine[1])-1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user