Add persitent URLs for year/month/day in the diary

This commit is contained in:
2026-05-08 18:15:09 +02:00
parent 34e2a8972a
commit e060faa39f
3 changed files with 97 additions and 1 deletions
+26 -1
View File
@@ -37,8 +37,19 @@ type specialPage struct {
// pageTypeHandler is implemented by each special folder type (diary, gallery, …).
// handle returns nil when the handler does not apply to the given path.
// redirect returns ok=true with an absolute URL when the request should be
// short-circuited with a 302 redirect (e.g. persistent date links in a diary).
// defaultHeading returns ok=true with custom pre-fill heading text for the
// editor when no index.md exists yet (e.g. German long-form date for a diary
// day). Handlers that don't need a hook should return the zero value.
//
// When adding a new hook, prefer a sibling method here over folding logic
// into main.go or render.go. If this list grows much beyond three, consider
// collapsing into a single overrides struct returned per request.
type pageTypeHandler interface {
handle(root, fsPath, urlPath string) *specialPage
redirect(root, fsPath, urlPath string) (target string, ok bool)
defaultHeading(root, fsPath, urlPath string) (heading string, ok bool)
}
// pageTypeHandlers is the registry. Each type registers itself via init().
@@ -204,6 +215,13 @@ func (h *handler) serveDir(w http.ResponseWriter, r *http.Request, urlPath, fsPa
return
}
for _, ph := range pageTypeHandlers {
if target, ok := ph.redirect(h.root, fsPath, urlPath); ok {
http.Redirect(w, r, target, http.StatusFound)
return
}
}
indexPath := filepath.Join(fsPath, "index.md")
rawMD, _ := os.ReadFile(indexPath)
@@ -255,7 +273,14 @@ func (h *handler) serveDir(w http.ResponseWriter, r *http.Request, urlPath, fsPa
rawContent = string(sections[sectionIndex])
}
} else if editMode && rawContent == "" && urlPath != "/" {
rawContent = "# " + pageTitle(urlPath) + "\n\n"
heading := pageTitle(urlPath)
for _, ph := range pageTypeHandlers {
if custom, ok := ph.defaultHeading(h.root, fsPath, urlPath); ok {
heading = custom
break
}
}
rawContent = "# " + heading + "\n\n"
}
data := pageData{