aboutsummaryrefslogtreecommitdiffstats
path: root/internal/web/handlers_posts.go
diff options
context:
space:
mode:
authorgrm <grm@eyesin.space>2026-09-15 23:12:51 +0300
committergrm <grm@eyesin.space>2026-09-15 23:12:51 +0300
commit2c85ca1a46ddf4652f8fffc53d6bcfbe32b2b4ff (patch)
treeecd5efd574b7d7463e4b2c4361c1994c4f28f561 /internal/web/handlers_posts.go
parent55732bdfc25933bab26220ccd82fe5e172d9e673 (diff)
downloadblogspace-2c85ca1a46ddf4652f8fffc53d6bcfbe32b2b4ff.tar.gz
blogspace-2c85ca1a46ddf4652f8fffc53d6bcfbe32b2b4ff.tar.bz2
blogspace-2c85ca1a46ddf4652f8fffc53d6bcfbe32b2b4ff.zip
Add post tags, with a tag page and two side-column modules
Posts can now carry tags, set on the post form as a checklist of the blog's existing tags plus a comma-separated box for new ones (no JS). A tag is a name and a unique slug, so "Go" and "go" are one tag and Greek tags get readable URLs; tags no post uses any more are deleted. On the blog, tags appear under the post date and link to /tag/<slug>, which lists the published posts from every page, paginated like a page. Two new layout modules show them: a Tags list (with counts, by use) and a Tag cloud (alphabetical, sized by use). Both are only fetched when a visible module needs them. The article loop and pager move to a shared postlist partial; while there, the pager stops adding a trailing slash — /news/?p=2 was a 404 because a {page} wildcard never matches one. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Sd8UPWrvyYCLj97JexNw3A
Diffstat (limited to 'internal/web/handlers_posts.go')
-rw-r--r--internal/web/handlers_posts.go25
1 files changed, 23 insertions, 2 deletions
diff --git a/internal/web/handlers_posts.go b/internal/web/handlers_posts.go
index d38c5f2..16d9cf2 100644
--- a/internal/web/handlers_posts.go
+++ b/internal/web/handlers_posts.go
@@ -54,6 +54,11 @@ func (s *Server) handlePostForm(w http.ResponseWriter, r *http.Request) {
s.serverError(w, err)
return
}
+ allTags, err := blogStore(r).ListTags(r.Context())
+ if err != nil {
+ s.serverError(w, err)
+ return
+ }
p := &store.Post{Published: true, CreatedAt: time.Now()}
if r.PathValue("id") != "" {
if p = s.loadPost(w, r); p == nil {
@@ -64,7 +69,7 @@ func (s *Server) handlePostForm(w http.ResponseWriter, r *http.Request) {
} else if hp, err := blogStore(r).HomePage(r.Context()); err == nil {
p.PageID = hp.ID
}
- s.render(w, r, "dashboard/post_form.html", map[string]any{"post": p, "pages": pages})
+ s.render(w, r, "dashboard/post_form.html", map[string]any{"post": p, "pages": pages, "allTags": allTags, "tagged": tagSet(p.Tags), "newTags": ""})
}
func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) {
@@ -74,6 +79,11 @@ func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) {
s.serverError(w, err)
return
}
+ allTags, err := blogStore(r).ListTags(r.Context())
+ if err != nil {
+ s.serverError(w, err)
+ return
+ }
p := &store.Post{CreatedAt: time.Now()}
if r.PathValue("id") != "" {
if p = s.loadPost(w, r); p == nil {
@@ -85,12 +95,16 @@ func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) {
p.BodyMD = strings.ReplaceAll(r.FormValue("body"), "\r\n", "\n")
p.Published = r.FormValue("published") == "on"
p.PageID, _ = strconv.ParseInt(r.FormValue("page_id"), 10, 64)
+ // The ticked existing tags and the comma-separated new ones are one list.
+ tags, tagsOK := parseTags(append(r.Form["tag"], strings.Split(r.FormValue("new_tags"), ",")...))
+ p.Tags = tags
autoSlug := p.Slug == ""
if autoSlug {
p.Slug = slug.Make(p.Title)
}
fail := func(status int, msg string) {
- s.renderStatus(w, r, status, "dashboard/post_form.html", map[string]any{"post": p, "pages": pages, "error": msg})
+ s.renderStatus(w, r, status, "dashboard/post_form.html", map[string]any{"post": p, "pages": pages, "error": msg,
+ "allTags": allTags, "tagged": tagSet(p.Tags), "newTags": r.FormValue("new_tags")})
}
if at := strings.TrimSpace(r.FormValue("posted_at")); at != "" { // blank keeps the current date
var ok bool
@@ -124,6 +138,9 @@ func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) {
case len(p.BodyMD) > 200_000:
fail(http.StatusBadRequest, s.tr(r, "Post is too long (200 KB max)."))
return
+ case !tagsOK:
+ fail(http.StatusBadRequest, s.tr(r, "Tags: at most 20 per post, 40 characters each."))
+ return
}
p.BodyHTML = markdown.Render(p.BodyMD)
base := p.Slug
@@ -149,6 +166,10 @@ func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) {
s.serverError(w, err)
return
}
+ if err := blogStore(r).SetPostTags(r.Context(), p.ID, tags); err != nil {
+ s.serverError(w, err)
+ return
+ }
redirectOK(w, r, "/b/"+blog.Subdomain+"/posts/"+strconv.FormatInt(p.ID, 10)+"/edit", s.tr(r, "Saved. Refresh your blog to see it."))
}