56 lines
977 B
Go

package app
import (
"os"
"strings"
)
func loadPosts(directory string) map[string]*Post {
entries, err := os.ReadDir(directory)
if err != nil {
panic(err)
}
posts := map[string]*Post{}
for _, entry := range entries {
fileName := entry.Name()
if !strings.HasSuffix(fileName, ".md") {
continue
}
baseName := strings.TrimSuffix(fileName, ".md")
content := load("posts/" + fileName)
post := &Post{
Slug: baseName,
}
if strings.HasPrefix(content, "---\n") {
end := strings.Index(content[4:], "---\n") + 4
frontmatter := content[4:end]
content = content[end+4:]
parseFrontmatter(frontmatter, func(key, value string) {
switch key {
case "title":
post.Title = value
case "tags":
post.Tags = strings.Split(value, " ")
case "created":
post.Created = value
case "published":
post.Published = value == "true"
}
})
}
post.Content = content
posts[baseName] = post
}
return posts
}