mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-30 22:39:58 +02:00
tpl/urls: Add JoinPath template function
See https://pkg.go.dev/net/url#JoinPath Closes #9694
This commit is contained in:
committed by
Bjørn Erik Pedersen
parent
03cb38e6c6
commit
5b3e165bad
@@ -185,3 +185,38 @@ func (ns *Namespace) AbsLangURL(s any) (template.HTML, error) {
|
||||
|
||||
return template.HTML(ns.deps.PathSpec.AbsURL(ss, !ns.multihost)), nil
|
||||
}
|
||||
|
||||
// JoinPath joins the provided elements into a URL string and cleans the result
|
||||
// of any ./ or ../ elements.
|
||||
func (ns *Namespace) JoinPath(elements ...any) (string, error) {
|
||||
|
||||
var selements []string
|
||||
for _, e := range elements {
|
||||
switch v := e.(type) {
|
||||
case []string:
|
||||
for _, e := range v {
|
||||
selements = append(selements, e)
|
||||
}
|
||||
case []any:
|
||||
for _, e := range v {
|
||||
se, err := cast.ToStringE(e)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
selements = append(selements, se)
|
||||
}
|
||||
default:
|
||||
se, err := cast.ToStringE(e)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
selements = append(selements, se)
|
||||
}
|
||||
}
|
||||
|
||||
result, err := url.JoinPath(selements[0], selements[1:]...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user