From 4ef81c59bf2782cdc310abf98ff0cb89e8baf98f Mon Sep 17 00:00:00 2001 From: valxntine Date: Tue, 10 Mar 2026 23:59:17 +0000 Subject: [PATCH] simplified some loops, added day2.md to test new footer date functionality --- posts.go | 19 +++++++------------ posts/day2.md | 12 ++++++++++++ templates/footer.templ | 16 +++++++++++++--- templates/footer_templ.go | 27 +++++++++++++++++++++++++-- templates/home.templ | 14 ++++++++++++-- templates/home_templ.go | 14 ++++++++++++-- templates/pages.templ | 2 +- templates/pages_templ.go | 2 +- 8 files changed, 83 insertions(+), 23 deletions(-) create mode 100644 posts/day2.md diff --git a/posts.go b/posts.go index 29ad88f..80d828c 100644 --- a/posts.go +++ b/posts.go @@ -5,6 +5,7 @@ import ( "embed" "fmt" "path/filepath" + "slices" "sort" "strings" "time" @@ -109,8 +110,8 @@ func parseFrontmatter(content string) (PostMeta, string, error) { meta.Excerpt = v case "tags": if v != "" { - tags := strings.Split(v, ",") - for _, t := range tags { + tags := strings.SplitSeq(v, ",") + for t := range tags { meta.Tags = append(meta.Tags, strings.TrimSpace(t)) } } @@ -307,11 +308,8 @@ func GetPostsByTag(tag string, page, per int) []models.Post { var filtered []models.Post for _, post := range posts { - for _, t := range post.Tags { - if t == tag { - filtered = append(filtered, post) - break - } + if slices.Contains(post.Tags, tag) { + filtered = append(filtered, post) } } @@ -336,11 +334,8 @@ func GetTotalPagesByTag(tag string, per int) int { count := 0 for _, post := range posts { - for _, t := range post.Tags { - if t == tag { - count++ - break - } + if slices.Contains(post.Tags, tag) { + count++ } } diff --git a/posts/day2.md b/posts/day2.md new file mode 100644 index 0000000..d0c42ce --- /dev/null +++ b/posts/day2.md @@ -0,0 +1,12 @@ +--- +title: "Day 2...ish?" +date: "2026-03-10" +excerpt: "It's been a while" +draft: false +--- + +It's been a while since I visited the Forge. + +I guess I've just been busy. I think it's time I commit, 1 post a week minimum. + +I'll...see you soon. diff --git a/templates/footer.templ b/templates/footer.templ index c2a5ffa..580ce2b 100644 --- a/templates/footer.templ +++ b/templates/footer.templ @@ -1,11 +1,21 @@ package templates -templ Footer() { +import "time" + +func FormatLastUpdated(t time.Time) string { + updatedAt := "Now-ish" + if !t.IsZero() { + updatedAt = t.Format("January 2, 2006") + } + return updatedAt +} + +templ Footer(lastUpdated time.Time) { } diff --git a/templates/footer_templ.go b/templates/footer_templ.go index 6fa9567..1951882 100644 --- a/templates/footer_templ.go +++ b/templates/footer_templ.go @@ -8,7 +8,17 @@ package templates import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" -func Footer() templ.Component { +import "time" + +func FormatLastUpdated(t time.Time) string { + updatedAt := "Now-ish" + if !t.IsZero() { + updatedAt = t.Format("January 2, 2006") + } + return updatedAt +} + +func Footer(lastUpdated time.Time) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { @@ -29,7 +39,20 @@ func Footer() templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/templates/home.templ b/templates/home.templ index 6fa1672..79b93d6 100644 --- a/templates/home.templ +++ b/templates/home.templ @@ -1,6 +1,16 @@ package templates -import "git.valxntine.dev/valxntine/blog/models" +import ( + "git.valxntine.dev/valxntine/blog/models" + "time" +) + +func GetLastUpdated(p []models.Post) time.Time { + if len(p) == 0 { + return time.Time{} + } + return p[0].PublishedAt +} templ HomePage(data models.Data) { @Layout(data.Title) { @@ -9,6 +19,6 @@ templ HomePage(data models.Data) {
@PostList(data.Posts, data.CurrentPage, data.TotalPages)
- @Footer() + @Footer(GetLastUpdated(data.Posts)) } } diff --git a/templates/home_templ.go b/templates/home_templ.go index 8ce78af..df2ac8f 100644 --- a/templates/home_templ.go +++ b/templates/home_templ.go @@ -8,7 +8,17 @@ package templates import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" -import "git.valxntine.dev/valxntine/blog/models" +import ( + "git.valxntine.dev/valxntine/blog/models" + "time" +) + +func GetLastUpdated(p []models.Post) time.Time { + if len(p) == 0 { + return time.Time{} + } + return p[0].PublishedAt +} func HomePage(data models.Data) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { @@ -67,7 +77,7 @@ func HomePage(data models.Data) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = Footer().Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = Footer(GetLastUpdated(data.Posts)).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/templates/pages.templ b/templates/pages.templ index 22fc627..1a75399 100644 --- a/templates/pages.templ +++ b/templates/pages.templ @@ -9,6 +9,6 @@ templ PostPageFull(post models.Post) {
@PostDetail(post)
- @Footer() + @Footer(post.PublishedAt) } } diff --git a/templates/pages_templ.go b/templates/pages_templ.go index 5ab82dc..a138182 100644 --- a/templates/pages_templ.go +++ b/templates/pages_templ.go @@ -67,7 +67,7 @@ func PostPageFull(post models.Post) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = Footer().Render(ctx, templ_7745c5c3_Buffer) + templ_7745c5c3_Err = Footer(post.PublishedAt).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err }