Update section behavior
This commit is contained in:
@@ -261,6 +261,7 @@ func (h *handler) serveDir(w http.ResponseWriter, r *http.Request, urlPath, fsPa
|
||||
// Determine section index (-1 = whole page).
|
||||
sectionIndex := -1
|
||||
insertBefore := -1
|
||||
sectionSpan := false
|
||||
if editMode {
|
||||
if s := r.URL.Query().Get("section"); s != "" {
|
||||
if n, err := strconv.Atoi(s); err == nil && n >= 0 {
|
||||
@@ -272,6 +273,7 @@ func (h *handler) serveDir(w http.ResponseWriter, r *http.Request, urlPath, fsPa
|
||||
insertBefore = n
|
||||
}
|
||||
}
|
||||
sectionSpan = r.URL.Query().Has("span")
|
||||
}
|
||||
|
||||
var special *specialPage
|
||||
@@ -323,7 +325,11 @@ func (h *handler) serveDir(w http.ResponseWriter, r *http.Request, urlPath, fsPa
|
||||
} else if editMode && sectionIndex >= 0 {
|
||||
sections := splitSections(rawMD)
|
||||
if sectionIndex < len(sections) {
|
||||
rawContent = string(sections[sectionIndex])
|
||||
end := sectionIndex + 1
|
||||
if sectionSpan {
|
||||
end = secionSpanEnd(sections, sectionIndex)
|
||||
}
|
||||
rawContent = string(joinSections(sections[sectionIndex:end]))
|
||||
}
|
||||
} else if editMode && rawContent == "" && urlPath != "/" {
|
||||
rawContent = "# " + pageTitle(urlPath) + "\n\n"
|
||||
@@ -336,6 +342,7 @@ func (h *handler) serveDir(w http.ResponseWriter, r *http.Request, urlPath, fsPa
|
||||
IsRoot: urlPath == "/",
|
||||
SectionIndex: sectionIndex,
|
||||
InsertBefore: insertBefore,
|
||||
Span: sectionSpan,
|
||||
PostURL: urlPath,
|
||||
RawContent: rawContent,
|
||||
Content: rendered,
|
||||
@@ -390,11 +397,7 @@ func (h *handler) handlePost(w http.ResponseWriter, r *http.Request, urlPath, fs
|
||||
indexPath := filepath.Join(fsPath, "index.md")
|
||||
redirectTarget := urlPath
|
||||
|
||||
// insert_before splices a new section into the file *at* index N rather
|
||||
// than replacing index N (used by the diary "create new day" flow).
|
||||
// section replaces the section at index N (used by per-section edits).
|
||||
// Exactly one of insert_before / section should be set; insert_before
|
||||
// wins if both are present.
|
||||
// handle section saving
|
||||
if s := r.FormValue("insert_before"); s != "" {
|
||||
insertIndex, err := strconv.Atoi(s)
|
||||
if err != nil || insertIndex < 0 {
|
||||
@@ -431,8 +434,18 @@ func (h *handler) handlePost(w http.ResponseWriter, r *http.Request, urlPath, fs
|
||||
http.Error(w, "section no longer exists — the page changed since you opened the editor", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
sections[sectionIndex] = []byte(content)
|
||||
content = string(joinSections(sections))
|
||||
|
||||
// handling of section editing that spans until the next heading of the same level
|
||||
end := sectionIndex + 1
|
||||
if r.FormValue("span") != "" {
|
||||
end = secionSpanEnd(sections, sectionIndex)
|
||||
}
|
||||
merged := make([][]byte, 0, len(sections)-(end-sectionIndex)+1)
|
||||
merged = append(merged, sections[:sectionIndex]...)
|
||||
merged = append(merged, []byte(content))
|
||||
merged = append(merged, sections[end:]...)
|
||||
content = string(joinSections(merged))
|
||||
|
||||
// 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.
|
||||
|
||||
Reference in New Issue
Block a user