feat(posts): add preview button for posts fc7e63cb
Steve · 2026-10-24 20:56 3 file(s) · +31 −0
apps/posts/handlers_public.go +29 −0
95 95
	a.renderPublic(w, r, v.siteContext, post.DisplayTitle()+" — "+v.BlogTitle, postMeta(v), a.body("post", v, postBody(v)))
96 96
}
97 97
98 +
func (a *App) previewPublicPost(w http.ResponseWriter, r *http.Request) {
99 +
	slug := r.PathValue("slug")
100 +
	post, err := getPostBySlug(a.DB, slug)
101 +
	if err != nil {
102 +
		http.Error(w, "Server error", http.StatusInternalServerError)
103 +
		return
104 +
	}
105 +
	if post == nil {
106 +
		http.Error(w, "Not found", http.StatusNotFound)
107 +
		return
108 +
	}
109 +
	var weather Weather
110 +
	if post.Weather != nil {
111 +
		w, err := formatWeather(*post.Weather)
112 +
		if err != nil {
113 +
			fmt.Printf("Problem formatting weather: %s", err.Error())
114 +
		} else {
115 +
			weather = w
116 +
		}
117 +
	}
118 +
	v := postView{
119 +
		siteContext:     a.site(),
120 +
		Post:            *post,
121 +
		RenderedContent: template.HTML(renderMarkdown(post.Content)),
122 +
		Weather:         weather,
123 +
	}
124 +
	a.renderPublic(w, r, v.siteContext, post.DisplayTitle()+" — "+v.BlogTitle, postMeta(v), a.body("post", v, postBody(v)))
125 +
}
126 +
98 127
func (a *App) publicPage(w http.ResponseWriter, r *http.Request) {
99 128
	slug := r.PathValue("slug")
100 129
	page, err := getPageBySlug(a.DB, slug)
apps/posts/routes.go +1 −0
53 53
	mux.HandleFunc("GET /admin/posts/new", requireSession(a.adminNewPost))
54 54
	mux.HandleFunc("POST /admin/posts", requireSession(a.adminCreatePost))
55 55
	mux.HandleFunc("GET /admin/posts/{id}/edit", requireSession(a.adminEditPost))
56 +
	mux.HandleFunc("GET /admin/posts/{slug}", requireSession(a.previewPublicPost))
56 57
	mux.HandleFunc("POST /admin/posts/{id}", requireSession(a.adminUpdatePost))
57 58
	mux.HandleFunc("POST /admin/posts/{id}/delete", requireSession(a.adminDeletePost))
58 59
	mux.HandleFunc("POST /admin/posts/{id}/publish", requireSession(a.adminTogglePublish))
apps/posts/templates/admin_post_form.html +1 −0
37 37
          <button type="submit" name="action" value="draft">unpublish</button>
38 38
        {{else}}
39 39
          <button type="submit" name="action" value="draft">save draft</button>
40 +
          <a class="btn" href="/admin/posts/{{$p.Slug}}">Preview</a>
40 41
          <button type="submit" name="action" value="publish">publish</button>
41 42
        {{end}}
42 43
      </div>