about summary refs log tree commit diff stats
path: root/internal/content/posts.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/content/posts.go')
-rw-r--r--internal/content/posts.go91
1 files changed, 55 insertions, 36 deletions
diff --git a/internal/content/posts.go b/internal/content/posts.go
index 5185d06..a4fb4b4 100644
--- a/internal/content/posts.go
+++ b/internal/content/posts.go
@@ -3,7 +3,6 @@ package content
 import (
 	"bytes"
 	"os"
-	"path"
 	"path/filepath"
 	"slices"
 	"strings"
@@ -28,6 +27,11 @@ type PostMatter struct {
 	} `toml:"taxonomies"`
 }
 
+type PostVCSDates struct {
+	Created     time.Time
+	LastUpdated time.Time
+}
+
 type Post struct {
 	Input    string
 	Output   string
@@ -39,6 +43,8 @@ type Post struct {
 
 type Tags mapset.Set[string]
 
+const DatesFilename = "dates.json"
+
 var markdown = goldmark.New(
 	goldmark.WithRendererOptions(
 		htmlrenderer.WithUnsafe(),
@@ -79,47 +85,60 @@ func RenderMarkdown(content []byte) (string, error) {
 	return buf.String(), nil
 }
 
-func ReadPosts(root string, inputDir string, outputDir string) ([]Post, Tags, error) {
+type Config struct {
+	Root      string
+	InputDir  string
+	OutputDir string
+}
+
+func GetPostFiles(root string) ([]string, error) {
+	return filepath.Glob(filepath.Join(root, "*.md"))
+}
+
+func ReadPosts(config *Config) ([]Post, Tags, error) {
 	tags := mapset.NewSet[string]()
 	posts := []Post{}
-	subdir := filepath.Join(root, inputDir)
-	files, err := os.ReadDir(subdir)
+	subdir := filepath.Join(config.Root, config.InputDir)
+	files, err := GetPostFiles(subdir)
 	if err != nil {
 		return nil, nil, errors.WithMessagef(err, "could not read post directory %s", subdir)
 	}
-	outputReplacer := strings.NewReplacer(root, outputDir, ".md", "/index.html")
-	urlReplacer := strings.NewReplacer(root, "", ".md", "/")
-	for _, f := range files {
-		pathFromRoot := filepath.Join(subdir, f.Name())
-		if !f.IsDir() && path.Ext(pathFromRoot) == ".md" {
-			output := outputReplacer.Replace(pathFromRoot)
-			url := urlReplacer.Replace(pathFromRoot)
-			log.Debug("reading post", "post", pathFromRoot)
-			matter, content, err := GetPost(pathFromRoot)
-			if err != nil {
-				return nil, nil, err
-			}
-
-			for _, tag := range matter.Taxonomies.Tags {
-				tags.Add(strings.ToLower(tag))
-			}
-
-			log.Debug("rendering markdown in post", "post", pathFromRoot)
-			html, err := RenderMarkdown(content)
-			if err != nil {
-				return nil, nil, err
-			}
-			post := Post{
-				Input:      pathFromRoot,
-				Output:     output,
-				Basename:   filepath.Base(url),
-				URL:        url,
-				PostMatter: *matter,
-				Content:    html,
-			}
-
-			posts = append(posts, post)
+	outputReplacer := strings.NewReplacer(config.Root, config.OutputDir, ".md", "/index.html")
+	urlReplacer := strings.NewReplacer(config.Root, "", ".md", "/")
+	for _, pathFromRoot := range files {
+		output := outputReplacer.Replace(pathFromRoot)
+		url := urlReplacer.Replace(pathFromRoot)
+		log.Debug("reading post", "post", pathFromRoot)
+		matter, content, err := GetPost(pathFromRoot)
+		if err != nil {
+			return nil, nil, err
 		}
+		// iter, err := repo.FileLog(pathFromRoot)
+		// if err != nil {
+		// 	return nil, nil, err
+		// }
+		// dates := getPostVCSDates(iter)
+		// log.Debug("post dates", "created", dates.Created, "updated", dates.LastUpdated)
+
+		for _, tag := range matter.Taxonomies.Tags {
+			tags.Add(strings.ToLower(tag))
+		}
+
+		log.Debug("rendering markdown in post", "post", pathFromRoot)
+		html, err := RenderMarkdown(content)
+		if err != nil {
+			return nil, nil, err
+		}
+		post := Post{
+			Input:      pathFromRoot,
+			Output:     output,
+			Basename:   filepath.Base(url),
+			URL:        url,
+			PostMatter: *matter,
+			Content:    html,
+		}
+
+		posts = append(posts, post)
 	}
 	slices.SortFunc(posts, func(a, b Post) int {
 		return b.Date.Compare(a.Date)