From d0601f06c080a8eb8d781fdd0e3338cd7ed42690 Mon Sep 17 00:00:00 2001 From: grm Date: Thu, 17 Sep 2026 16:42:15 +0300 Subject: Add post summaries, featured images and a page outro MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Recreating gizmolab.net as a blog showed three things the design system could not express, all of them ordinary blog features: - Listings can show a summary and a "Read more" link instead of the whole post (theme.list_style). The summary is the source up to ; without the marker a Markdown post is cut at a block boundary after 70 words (never inside a code fence) and an HTML post is shown whole, since cutting hand-written markup blind would leave tags open. Computed at render time (Post.Excerpt/HasMore), nothing new is stored. - A post can carry a featured image (posts.image, a library file that is unset when the file is deleted): a thumbnail floated beside the entry on listings, and on the post page above or below the text, or not at all, as theme.post_image says. The picker is the design page's imagepick; its library panel moves out of design.html into partials/imagelib.html so both forms share it — which also removes a stale pickAction call the design form's click handler would have hit. - A page can have a closing text after its posts (pages.outro_md/html), in the intro's format: the per-page counterpart of the blog-wide "after the posts" module, for a home page that ends in a site map or a widget. The editor partial takes formatof to share another editor's Format row, and its upload field is now _file so two editors fit in one form. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01Sd8UPWrvyYCLj97JexNw3A --- internal/web/handlers_posts.go | 73 ++++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 21 deletions(-) (limited to 'internal/web/handlers_posts.go') diff --git a/internal/web/handlers_posts.go b/internal/web/handlers_posts.go index e284e57..ec56138 100644 --- a/internal/web/handlers_posts.go +++ b/internal/web/handlers_posts.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "github.com/google/uuid" "github.com/gramanas/blogspace/internal/markdown" "github.com/gramanas/blogspace/internal/slug" "github.com/gramanas/blogspace/internal/store" @@ -48,17 +49,39 @@ func (s *Server) loadPost(w http.ResponseWriter, r *http.Request) *store.Post { return p } -func (s *Server) handlePostForm(w http.ResponseWriter, r *http.Request) { - pages, err := blogStore(r).ListPages(r.Context()) +// postFormData is what the post form needs besides the post: the pages to +// file it under, the tags to pick from, and the library images for the +// featured-image picker (the recent ones; the chosen one is added by name). +func (s *Server) postFormData(r *http.Request, p *store.Post) (map[string]any, error) { + bs := blogStore(r) + pages, err := bs.ListPages(r.Context()) if err != nil { - s.serverError(w, err) - return + return nil, err } - allTags, err := blogStore(r).ListTags(r.Context()) + allTags, err := bs.ListTags(r.Context()) if err != nil { - s.serverError(w, err) - return + return nil, err + } + images, imageCount, err := bs.ListFiles(r.Context(), "image", "", recentImages, 0) + if err != nil { + return nil, err + } + names := map[string]string{} + for _, f := range images { + names[f.ID.String()] = f.Filename } + if p.Image != "" && names[p.Image] == "" { + if f, err := bs.FileMeta(r.Context(), uuid.MustParse(p.Image)); err == nil { + names[p.Image] = f.Filename + } else { // gone from the library (or never there): the post has no image + p.Image = "" + } + } + return map[string]any{"post": p, "pages": pages, "allTags": allTags, "tagsText": tagsText(p.Tags), + "images": images, "imageNames": names, "imageCount": imageCount}, nil +} + +func (s *Server) handlePostForm(w http.ResponseWriter, r *http.Request) { p := &store.Post{Published: true, CreatedAt: time.Now()} if r.PathValue("id") != "" { if p = s.loadPost(w, r); p == nil { @@ -69,21 +92,16 @@ 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, "allTags": allTags, "tagsText": tagsText(p.Tags)}) -} - -func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) { - blog := currentBlog(r) - pages, err := blogStore(r).ListPages(r.Context()) - if err != nil { - s.serverError(w, err) - return - } - allTags, err := blogStore(r).ListTags(r.Context()) + d, err := s.postFormData(r, p) if err != nil { s.serverError(w, err) return } + s.render(w, r, "dashboard/post_form.html", d) +} + +func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) { + blog := currentBlog(r) p := &store.Post{CreatedAt: time.Now()} if r.PathValue("id") != "" { if p = s.loadPost(w, r); p == nil { @@ -96,15 +114,22 @@ func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) { p.Format = pick(r.FormValue("format"), store.FormatMarkdown, store.FormatHTML) p.Published = r.FormValue("published") == "on" p.PageID, _ = strconv.ParseInt(r.FormValue("page_id"), 10, 64) + p.Image = imageID(pickImage(p.Image, r.FormValue("image"))) tags, tagsOK := parseTags(strings.Split(r.FormValue("tags"), ",")) p.Tags = tags autoSlug := p.Slug == "" if autoSlug { p.Slug = slug.Make(p.Title) } + d, err := s.postFormData(r, p) + if err != nil { + s.serverError(w, err) + return + } + pages := d["pages"].([]store.Page) fail := func(status int, msg string) { - s.renderStatus(w, r, status, "dashboard/post_form.html", map[string]any{"post": p, "pages": pages, "error": msg, - "allTags": allTags, "tagsText": r.FormValue("tags")}) + d["error"], d["tagsText"] = msg, r.FormValue("tags") + s.renderStatus(w, r, status, "dashboard/post_form.html", d) } if at := strings.TrimSpace(r.FormValue("posted_at")); at != "" { // blank keeps the current date var ok bool @@ -113,12 +138,18 @@ func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) { return } } - img, err := s.readUpload(r, "inline_file", false) + img, err := s.readUpload(r, "body_file", false) if err != nil { fail(http.StatusBadRequest, s.tr(r, "File not added:")+" "+err.Error()) return } p.BodyMD = appendFile(p.BodyMD, img, p.Format) + if f, err := s.readUpload(r, "image_file", true); err != nil { // an upload beats the picker + fail(http.StatusBadRequest, s.tr(r, "Image not added:")+" "+err.Error()) + return + } else if f != nil { + p.Image = f.ID.String() + } pageOK := false for _, pg := range pages { if pg.ID == p.PageID { -- cgit v1.2.3