Adjust sidebar tree
This commit is contained in:
@@ -40,18 +40,16 @@ func (h *handler) handleTree(w http.ResponseWriter, r *http.Request, urlPath, fs
|
||||
return
|
||||
}
|
||||
|
||||
entries, err := listTreeEntries(fsPath)
|
||||
foldersOnly := r.URL.Query().Has("foldersOnly")
|
||||
|
||||
entries, err := listTreeEntries(fsPath, foldersOnly)
|
||||
if err != nil {
|
||||
http.Error(w, "read failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// expandTo asks for a nested listing: each folder along the ancestor chain
|
||||
// carries its own children, recursively, down to the target. Used by the
|
||||
// tree sidebar to render the current page's chain in one request. The flat
|
||||
// picker omits expandTo and is unaffected.
|
||||
if expandTo := r.URL.Query().Get("expandTo"); expandTo != "" {
|
||||
expandTreeChain(fsPath, entries, treePathSegments(expandTo))
|
||||
expandTreeChain(fsPath, entries, treePathSegments(expandTo), foldersOnly)
|
||||
}
|
||||
|
||||
resp := treeResponse{Path: canonicalTreePath(urlPath), Entries: entries}
|
||||
@@ -70,8 +68,8 @@ func canonicalTreePath(urlPath string) string {
|
||||
|
||||
// listTreeEntries returns the immediate children of fsPath, filtering hidden
|
||||
// entries and index.md. Folders are listed before files; both groups are
|
||||
// sorted alphabetically.
|
||||
func listTreeEntries(fsPath string) ([]treeEntry, error) {
|
||||
// sorted alphabetically. foldersOnly will not list any individual files at all.
|
||||
func listTreeEntries(fsPath string, foldersOnly bool) ([]treeEntry, error) {
|
||||
raw, err := os.ReadDir(fsPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -85,7 +83,7 @@ func listTreeEntries(fsPath string) ([]treeEntry, error) {
|
||||
if e.IsDir() {
|
||||
folders = append(folders, treeEntry{Name: name, Kind: "folder"})
|
||||
} else {
|
||||
if name == "index.md" {
|
||||
if foldersOnly || name == "index.md" {
|
||||
continue
|
||||
}
|
||||
files = append(files, treeEntry{Name: name, Kind: "file"})
|
||||
@@ -113,7 +111,7 @@ func treePathSegments(p string) []string {
|
||||
// far as it still exists. Segments only ever match real directory names from
|
||||
// listTreeEntries (no "." or ".." entries), so this cannot traverse outside the
|
||||
// listed tree.
|
||||
func expandTreeChain(fsPath string, entries []treeEntry, segs []string) {
|
||||
func expandTreeChain(fsPath string, entries []treeEntry, segs []string, foldersOnly bool) {
|
||||
if len(segs) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -122,12 +120,12 @@ func expandTreeChain(fsPath string, entries []treeEntry, segs []string) {
|
||||
continue
|
||||
}
|
||||
childFs := filepath.Join(fsPath, segs[0])
|
||||
kids, err := listTreeEntries(childFs)
|
||||
kids, err := listTreeEntries(childFs, foldersOnly)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
entries[i].Children = kids
|
||||
expandTreeChain(childFs, entries[i].Children, segs[1:])
|
||||
expandTreeChain(childFs, entries[i].Children, segs[1:], foldersOnly)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user