diff options
author | Alan Pearce | 2024-06-28 20:43:14 +0200 |
---|---|---|
committer | Alan Pearce | 2024-06-29 16:50:51 +0200 |
commit | 6c4a3268bc4c528ecff45f50ed5ca6aa1d48500c (patch) | |
tree | 6663946e586d5c0745b984a0a809fc2330ce70d0 /internal/content | |
parent | b4095d108a2646bcf9e7fff64788b10d9bce8da3 (diff) | |
download | website-6c4a3268bc4c528ecff45f50ed5ca6aa1d48500c.tar.lz website-6c4a3268bc4c528ecff45f50ed5ca6aa1d48500c.tar.zst website-6c4a3268bc4c528ecff45f50ed5ca6aa1d48500c.zip |
wip postdate
Diffstat (limited to 'internal/content')
-rw-r--r-- | internal/content/posts.go | 91 |
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) |