diff options
author | Alan Pearce | 2024-06-18 20:13:26 +0200 |
---|---|---|
committer | Alan Pearce | 2024-06-18 20:13:26 +0200 |
commit | b27c96688785372787eb5c3c71a32767fab71ac4 (patch) | |
tree | 2e39bc6cd94724953cf917c509fb141d39690cf1 /internal/builder/builder.go | |
parent | 1d247493e05cdc659e46cd3d8a01d5da1e893867 (diff) | |
download | website-b27c96688785372787eb5c3c71a32767fab71ac4.tar.lz website-b27c96688785372787eb5c3c71a32767fab71ac4.tar.zst website-b27c96688785372787eb5c3c71a32767fab71ac4.zip |
split content and sitemap code from builder
Diffstat (limited to 'internal/builder/builder.go')
-rw-r--r-- | internal/builder/builder.go | 35 |
1 files changed, 11 insertions, 24 deletions
diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 7b6af0b..141bd6f 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -11,13 +11,14 @@ import ( "time" "website/internal/config" + "website/internal/content" "website/internal/log" + "website/internal/sitemap" "github.com/a-h/templ" mapset "github.com/deckarep/golang-set/v2" cp "github.com/otiai10/copy" "github.com/pkg/errors" - "github.com/snabb/sitemap" ) type IOConfig struct { @@ -108,12 +109,12 @@ func build(outDir string, config config.Config) (*Result, error) { return nil, errors.WithMessage(err, "could not create post output directory") } log.Debug("reading posts") - posts, tags, err := readPosts("content", "post", publicDir) + posts, tags, err := content.ReadPosts("content", "post", publicDir) if err != nil { return nil, err } - sm := NewSitemap(config) + sitemap := sitemap.New(config) lastMod := time.Now() if len(posts) > 0 { lastMod = posts[0].Date @@ -124,10 +125,7 @@ func build(outDir string, config config.Config) (*Result, error) { return nil, errors.WithMessage(err, "could not create directory for post") } log.Debug("rendering post", "post", post.Basename) - sm.Add(&sitemap.URL{ - Loc: post.URL, - LastMod: &post.Date, - }) + sitemap.AddPath(post.URL, post.Date) if err := renderToFile(postPage(config, post), post.Output); err != nil { return nil, err } @@ -140,13 +138,10 @@ func build(outDir string, config config.Config) (*Result, error) { if err := renderToFile(tagsPage(config, "tags", mapset.Sorted(tags), "/tags"), publicDir, "tags", "index.html"); err != nil { return nil, err } - sm.Add(&sitemap.URL{ - Loc: "/tags/", - LastMod: &lastMod, - }) + sitemap.AddPath("/tags/", lastMod) for _, tag := range tags.ToSlice() { - matchingPosts := []Post{} + matchingPosts := []content.Post{} for _, post := range posts { if slices.Contains(post.Taxonomies.Tags, tag) { matchingPosts = append(matchingPosts, post) @@ -160,10 +155,7 @@ func build(outDir string, config config.Config) (*Result, error) { if err := renderToFile(tagPage(config, tag, matchingPosts, url), publicDir, "tags", tag, "index.html"); err != nil { return nil, err } - sm.Add(&sitemap.URL{ - Loc: url, - LastMod: &matchingPosts[0].Date, - }) + sitemap.AddPath(url, matchingPosts[0].Date) log.Debug("rendering tags feed", "tag", tag) feed, err := renderFeed( @@ -184,10 +176,7 @@ func build(outDir string, config config.Config) (*Result, error) { if err := renderToFile(listPage(config, posts, "/post"), publicDir, "post", "index.html"); err != nil { return nil, err } - sm.Add(&sitemap.URL{ - Loc: "/post/", - LastMod: &lastMod, - }) + sitemap.AddPath("/post/", lastMod) log.Debug("rendering feed") feed, err := renderFeed(config.Title, config, posts, "feed") @@ -223,9 +212,7 @@ func build(outDir string, config config.Config) (*Result, error) { // it would be nice to set LastMod here, but using the latest post // date would be wrong as the homepage has its own content file // without a date, which could be newer - sm.Add(&sitemap.URL{ - Loc: "/", - }) + sitemap.AddPath("/", time.Time{}) h, _ = getHTMLStyleHash(publicDir, "index.html") r.Hashes = append(r.Hashes, h) @@ -235,7 +222,7 @@ func build(outDir string, config config.Config) (*Result, error) { } log.Debug("rendering sitemap") - if err := writerToFile(sm, publicDir, "sitemap.xml"); err != nil { + if err := writerToFile(sitemap, publicDir, "sitemap.xml"); err != nil { return nil, err } |