54 lines
588 B
Go
54 lines
588 B
Go
package server
|
|
|
|
import "strings"
|
|
|
|
type Breadcrumb struct {
|
|
Name string
|
|
Path string
|
|
}
|
|
|
|
func buildBreadcrumbs(
|
|
path string,
|
|
) []Breadcrumb {
|
|
|
|
path =
|
|
strings.TrimSpace(path)
|
|
|
|
if path == "" ||
|
|
path == "/" {
|
|
return nil
|
|
}
|
|
|
|
if !strings.HasPrefix(path, "/") {
|
|
path = "/" + path
|
|
}
|
|
|
|
parts :=
|
|
strings.Split(
|
|
strings.Trim(path, "/"),
|
|
"/",
|
|
)
|
|
|
|
var out []Breadcrumb
|
|
current := ""
|
|
|
|
for _, part := range parts {
|
|
|
|
if part == "" {
|
|
continue
|
|
}
|
|
|
|
current += "/" + part
|
|
|
|
out = append(
|
|
out,
|
|
Breadcrumb{
|
|
Name: part,
|
|
Path: current,
|
|
},
|
|
)
|
|
}
|
|
|
|
return out
|
|
}
|