mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-06 22:40:31 -05:00
browse: Correct links when site defined with a path (fixes #1561)
This commit is contained in:
parent
7d15435361
commit
3ca419e2cf
2 changed files with 18 additions and 14 deletions
|
@ -37,7 +37,7 @@ type Browse struct {
|
||||||
|
|
||||||
// Config is a configuration for browsing in a particular path.
|
// Config is a configuration for browsing in a particular path.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
PathScope string
|
PathScope string // the base path the URL must match to enable browsing
|
||||||
Fs staticfiles.FileServer
|
Fs staticfiles.FileServer
|
||||||
Variables interface{}
|
Variables interface{}
|
||||||
Template *template.Template
|
Template *template.Template
|
||||||
|
@ -78,10 +78,15 @@ type Listing struct {
|
||||||
httpserver.Context
|
httpserver.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// BreadcrumbMap returns l.Path where every element is a map
|
// Crumb represents part of a breadcrumb menu.
|
||||||
// of URLs and path segment names.
|
type Crumb struct {
|
||||||
func (l Listing) BreadcrumbMap() map[string]string {
|
Link, Text string
|
||||||
result := map[string]string{}
|
}
|
||||||
|
|
||||||
|
// Breadcrumbs returns l.Path where every element maps
|
||||||
|
// the link to the text to display.
|
||||||
|
func (l Listing) Breadcrumbs() []Crumb {
|
||||||
|
var result []Crumb
|
||||||
|
|
||||||
if len(l.Path) == 0 {
|
if len(l.Path) == 0 {
|
||||||
return result
|
return result
|
||||||
|
@ -94,13 +99,12 @@ func (l Listing) BreadcrumbMap() map[string]string {
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := strings.Split(lpath, "/")
|
parts := strings.Split(lpath, "/")
|
||||||
for i, part := range parts {
|
for i := range parts {
|
||||||
if i == 0 && part == "" {
|
txt := parts[i]
|
||||||
// Leading slash (root)
|
if i == 0 && parts[i] == "" {
|
||||||
result["/"] = "/"
|
txt = "/"
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
result[strings.Join(parts[:i+1], "/")] = part
|
result = append(result, Crumb{Link: strings.Repeat("../", len(parts)-i-1), Text: txt})
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
@ -247,12 +251,12 @@ func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string, config
|
||||||
fileCount++
|
fileCount++
|
||||||
}
|
}
|
||||||
|
|
||||||
url := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
|
|
||||||
|
|
||||||
if config.Fs.IsHidden(f) {
|
if config.Fs.IsHidden(f) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
url := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
|
||||||
|
|
||||||
fileinfos = append(fileinfos, FileInfo{
|
fileinfos = append(fileinfos, FileInfo{
|
||||||
IsDir: f.IsDir(),
|
IsDir: f.IsDir(),
|
||||||
Name: f.Name(),
|
Name: f.Name(),
|
||||||
|
|
|
@ -323,7 +323,7 @@ footer {
|
||||||
|
|
||||||
<header>
|
<header>
|
||||||
<h1>
|
<h1>
|
||||||
{{range $url, $name := .BreadcrumbMap}}<a href="{{html $url}}">{{html $name}}</a>{{if ne $url "/"}}/{{end}}{{end}}
|
{{range $i, $crumb := .Breadcrumbs}}<a href="{{html $crumb.Link}}">{{html $crumb.Text}}</a>{{if ne $i 0}}/{{end}}{{end}}
|
||||||
</h1>
|
</h1>
|
||||||
</header>
|
</header>
|
||||||
<main>
|
<main>
|
||||||
|
|
Loading…
Reference in a new issue