Improve sections

This commit is contained in:
2026-04-28 17:43:20 +02:00
parent 1f7cfd637a
commit 73a8b4f78f
6 changed files with 59 additions and 2 deletions
+11 -1
View File
@@ -243,6 +243,7 @@ func (h *handler) handlePost(w http.ResponseWriter, r *http.Request, urlPath, fs
}
content := r.FormValue("content")
indexPath := filepath.Join(fsPath, "index.md")
redirectTarget := urlPath
// If a section index was submitted, splice the edited section back into
// the full file rather than replacing the whole document.
@@ -258,6 +259,15 @@ func (h *handler) handlePost(w http.ResponseWriter, r *http.Request, urlPath, fs
sections[sectionIndex] = []byte(content)
}
content = string(joinSections(sections))
// Section index ≥ 1 is a heading-anchored section. Redirect to its
// anchor so the user lands on the section they just saved, even if
// the heading text changed.
if sectionIndex >= 1 {
ids := headingIDs([]byte(content))
if sectionIndex-1 < len(ids) {
redirectTarget = urlPath + "#" + ids[sectionIndex-1]
}
}
}
if strings.TrimSpace(content) == "" {
@@ -275,7 +285,7 @@ func (h *handler) handlePost(w http.ResponseWriter, r *http.Request, urlPath, fs
return
}
}
http.Redirect(w, r, urlPath, http.StatusSeeOther)
http.Redirect(w, r, redirectTarget, http.StatusSeeOther)
}
// readPageSettings parses a .page-settings file in dir.