aboutsummaryrefslogtreecommitdiffstats
path: root/internal/web
diff options
context:
space:
mode:
Diffstat (limited to 'internal/web')
-rw-r--r--internal/web/handlers_posts.go25
-rw-r--r--internal/web/static/dashboard.css2
-rw-r--r--internal/web/templates/dashboard/post_form.html2
-rw-r--r--internal/web/web_test.go18
4 files changed, 44 insertions, 3 deletions
diff --git a/internal/web/handlers_posts.go b/internal/web/handlers_posts.go
index 88d5c20..8327912 100644
--- a/internal/web/handlers_posts.go
+++ b/internal/web/handlers_posts.go
@@ -5,6 +5,7 @@ import (
"net/http"
"strconv"
"strings"
+ "time"
"github.com/gramanas/blogspace/internal/markdown"
"github.com/gramanas/blogspace/internal/slug"
@@ -46,7 +47,7 @@ func (s *Server) handlePostForm(w http.ResponseWriter, r *http.Request) {
s.serverError(w, err)
return
}
- p := &store.Post{Published: true}
+ p := &store.Post{Published: true, CreatedAt: time.Now()}
if r.PathValue("id") != "" {
if p = s.loadPost(w, r); p == nil {
return
@@ -66,7 +67,7 @@ func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) {
s.serverError(w, err)
return
}
- p := &store.Post{}
+ p := &store.Post{CreatedAt: time.Now()}
if r.PathValue("id") != "" {
if p = s.loadPost(w, r); p == nil {
return
@@ -84,6 +85,13 @@ func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) {
fail := func(status int, msg string) {
s.renderStatus(w, r, status, "dashboard/post_form.html", map[string]any{"post": p, "pages": pages, "error": msg})
}
+ if at := strings.TrimSpace(r.FormValue("posted_at")); at != "" { // blank keeps the current date
+ var ok bool
+ if p.CreatedAt, ok = parsePostDate(at); !ok {
+ fail(http.StatusBadRequest, s.tr(r, "Enter the date as YYYY-MM-DD HH:MM."))
+ return
+ }
+ }
img, err := s.readUpload(r, "inline_image")
if err != nil {
fail(http.StatusBadRequest, s.tr(r, "Image not added:")+" "+err.Error())
@@ -137,6 +145,19 @@ func (s *Server) handlePostSave(w http.ResponseWriter, r *http.Request) {
redirectOK(w, r, "/b/"+blog.Subdomain+"/posts/"+strconv.FormatInt(p.ID, 10)+"/edit", s.tr(r, "Saved. Refresh your blog to see it."))
}
+// parsePostDate reads the post form's date: what a datetime-local input sends,
+// or the same typed by hand where the browser shows a plain text box. The time
+// of day is optional. Times are taken in the server's zone, which is also the
+// zone the blog displays them in.
+func parsePostDate(v string) (time.Time, bool) {
+ for _, layout := range []string{"2006-01-02T15:04", "2006-01-02 15:04", "2006-01-02T15:04:05", "2006-01-02"} {
+ if t, err := time.ParseInLocation(layout, v, time.Local); err == nil {
+ return t, true
+ }
+ }
+ return time.Time{}, false
+}
+
func (s *Server) handlePostDeleteConfirm(w http.ResponseWriter, r *http.Request) {
p := s.loadPost(w, r)
if p == nil {
diff --git a/internal/web/static/dashboard.css b/internal/web/static/dashboard.css
index 7a66ba5..5c766e7 100644
--- a/internal/web/static/dashboard.css
+++ b/internal/web/static/dashboard.css
@@ -59,7 +59,7 @@ p { margin: 0.6em 0; }
label { display: block; margin: 0.8em 0; font-family: "IBM Plex Mono", "SF Mono", Menlo, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 0.8em; text-transform: uppercase; letter-spacing: 0.05em; }
label .muted { text-transform: none; letter-spacing: 0; }
label code { text-transform: none; letter-spacing: 0; }
-input[type=text], input[type=password], input[type=number], input:not([type]), input[type=file], select, textarea {
+input[type=text], input[type=password], input[type=number], input[type=datetime-local], input:not([type]), input[type=file], select, textarea {
width: 100%; max-width: 100%; padding: 0.5em 0.6em; margin-top: 0.25em; border: 2px solid #1d1a17; font: inherit; font-family: -apple-system, "Segoe UI", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 16px; text-transform: none; letter-spacing: 0; background: #fffdf8; color: #1d1a17; }
input:focus, select:focus, textarea:focus, button:focus, .btn:focus { outline: 2px solid #c8541a; outline-offset: 1px; }
input[type=color] { width: 4em; height: 2.3em; padding: 0.1em; margin-top: 0.25em; border: 2px solid #1d1a17; background: #fffdf8; }
diff --git a/internal/web/templates/dashboard/post_form.html b/internal/web/templates/dashboard/post_form.html
index af48215..8069a9a 100644
--- a/internal/web/templates/dashboard/post_form.html
+++ b/internal/web/templates/dashboard/post_form.html
@@ -11,6 +11,8 @@
</label>
<label>{{t "Address"}} <span class="muted">({{t "optional, made from the title"}})</span><br>
<input name="slug" value="{{.Data.post.Slug}}" pattern="[a-z0-9]([a-z0-9-]*[a-z0-9])?" maxlength="80"></label>
+ <label>{{t "Date"}}<br>
+ <input type="datetime-local" name="posted_at" value="{{.Data.post.CreatedAt.Format "2006-01-02T15:04"}}" placeholder="YYYY-MM-DD HH:MM"></label>
</div>
<label for="ed-body">{{t "Content"}} <span class="muted">(Markdown)</span></label>
{{template "editor" (dict "name" "body" "value" .Data.post.BodyMD "rows" 24 "tall" true "upload" (print "/b/" .Blog.Subdomain "/images/upload") "csrf" .CSRF)}}
diff --git a/internal/web/web_test.go b/internal/web/web_test.go
index ae26029..d7de43a 100644
--- a/internal/web/web_test.go
+++ b/internal/web/web_test.go
@@ -220,6 +220,24 @@ func TestValidLinkURL(t *testing.T) {
}
}
+func TestParsePostDate(t *testing.T) {
+ for in, want := range map[string]time.Time{
+ "2026-09-14T08:30": time.Date(2026, 9, 14, 8, 30, 0, 0, time.Local),
+ "2026-09-14 08:30": time.Date(2026, 9, 14, 8, 30, 0, 0, time.Local),
+ "2026-09-14T08:30:15": time.Date(2026, 9, 14, 8, 30, 15, 0, time.Local),
+ "2026-09-14": time.Date(2026, 9, 14, 0, 0, 0, 0, time.Local),
+ } {
+ if got, ok := parsePostDate(in); !ok || !got.Equal(want) {
+ t.Errorf("%q: got %v %v, want %v", in, got, ok, want)
+ }
+ }
+ for _, bad := range []string{"", "14/09/2026", "2026-13-01", "yesterday", "2026-09-14T25:00"} {
+ if _, ok := parsePostDate(bad); ok {
+ t.Errorf("%q should be rejected", bad)
+ }
+ }
+}
+
func TestGroupArchive(t *testing.T) {
d := func(y int, m time.Month, day int) time.Time { return time.Date(y, m, day, 12, 0, 0, 0, time.UTC) }
posts := []store.PostRef{{Title: "d", CreatedAt: d(2026, 9, 3)}, {Title: "c", CreatedAt: d(2026, 9, 1)}, {Title: "b", CreatedAt: d(2026, 2, 1)}, {Title: "a", CreatedAt: d(2025, 12, 31)}}