helpers: Improve the language prefix logic

See #2444
This commit is contained in:
Bjørn Erik Pedersen 2016-09-13 23:44:16 +02:00
parent f53145b274
commit 74ffb45fbe

View file

@ -169,6 +169,7 @@ func AbsURL(in string, addLanguage bool) string {
if addLanguage {
prefix := getLanguagePrefix()
if prefix != "" {
hasPrefix := false
// avoid adding language prefix if already present
if strings.HasPrefix(in, "/") {
@ -177,7 +178,7 @@ func AbsURL(in string, addLanguage bool) string {
hasPrefix = strings.HasPrefix(in, prefix)
}
if prefix != "" && !hasPrefix {
if !hasPrefix {
addSlash := in == "" || strings.HasSuffix(in, "/")
in = path.Join(prefix, in)
@ -186,6 +187,7 @@ func AbsURL(in string, addLanguage bool) string {
}
}
}
}
return MakePermalink(baseURL, in).String()
}
@ -231,6 +233,7 @@ func RelURL(in string, addLanguage bool) string {
if addLanguage {
prefix := getLanguagePrefix()
if prefix != "" {
hasPrefix := false
// avoid adding language prefix if already present
if strings.HasPrefix(in, "/") {
@ -239,7 +242,7 @@ func RelURL(in string, addLanguage bool) string {
hasPrefix = strings.HasPrefix(in, prefix)
}
if prefix != "" && !hasPrefix {
if !hasPrefix {
hadSlash := strings.HasSuffix(u, "/")
u = path.Join(prefix, u)
@ -249,6 +252,7 @@ func RelURL(in string, addLanguage bool) string {
}
}
}
}
if !canonifyURLs {
u = AddContextRoot(baseURL, u)