Rework wikilinks feature

This commit is contained in:
2026-09-17 17:46:36 +02:00
parent b14941b3e0
commit b57e5d454b
12 changed files with 334 additions and 544 deletions
+29 -3
View File
@@ -10,6 +10,7 @@ import (
"io/fs"
"log"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
@@ -105,7 +106,7 @@ func main() {
log.Fatal(err)
}
initMarkdown(root)
initMarkdown()
authKey, err := loadOrCreateAuthKey(root)
if err != nil {
@@ -220,7 +221,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// POSTs must not be redirected — the form action has no trailing
// slash (path.Clean strips it) and the content would be lost.
if !strings.HasSuffix(r.URL.Path, "/") && r.Method != http.MethodPost {
http.Redirect(w, r, r.URL.Path+"/", http.StatusMovedPermanently)
http.Redirect(w, r, slashedURL(r.URL), http.StatusMovedPermanently)
return
}
h.serveDir(w, r, urlPath, fsPath)
@@ -232,14 +233,39 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if info.IsDir() {
if urlPath != "/" {
// Pages must live at a slash-terminated URL: embed destinations are
// relative, so "/Trips/Japan" would resolve them against "/Trips/".
if !strings.HasSuffix(r.URL.Path, "/") && r.Method != http.MethodPost {
http.Redirect(w, r, slashedURL(r.URL), http.StatusMovedPermanently)
return
}
urlPath += "/"
}
h.serveDir(w, r, urlPath, fsPath)
} else {
// ?w= on a file's own URL serves a thumbnail. Embeds use this form so
// their <img src> can stay relative to the page; /_thumb/<abs path>
// stays for listings, which have absolute paths on hand anyway. A file
// with no thumbnailer ignores the parameter and serves normally.
if r.URL.Query().Has("w") && hasThumbnail(fsPath) {
h.serveThumbnail(w, r, fsPath)
return
}
http.ServeFile(w, r, fsPath)
}
}
// slashedURL returns u's path with a trailing slash appended and the query
// string intact — the canonical page form that relative destinations resolve
// against.
func slashedURL(u *url.URL) string {
target := u.Path + "/"
if u.RawQuery != "" {
target += "?" + u.RawQuery
}
return target
}
func (h *handler) serveDir(w http.ResponseWriter, r *http.Request, urlPath, fsPath string) {
_, editMode := r.URL.Query()["edit"]
@@ -373,7 +399,7 @@ func (h *handler) handlePost(w http.ResponseWriter, r *http.Request, urlPath, fs
return
}
if _, ok := query["move"]; ok {
h.handleMove(w, r, urlPath, fsPath, query.Get("move"), query.Has("links"), query.Has("merge"))
h.handleMove(w, r, urlPath, fsPath, query.Get("move"), query.Has("merge"))
return
}
if query.Has("toggle") {