shithub: hugo

Download patch

ref: f53145b2748067a9d50ab57efc6423d7568c237c
parent: 8d040c966f777a3fc462d0fb24775cb865519991
author: Bjørn Erik Pedersen <[email protected]>
date: Tue Sep 13 17:37:27 EDT 2016

helpers: Avoid adding language prefix if already present

Fixes #2444

--- a/helpers/url.go
+++ b/helpers/url.go
@@ -169,8 +169,15 @@
 
 	if addLanguage {
 		prefix := getLanguagePrefix()
+		hasPrefix := false
+		// avoid adding language prefix if already present
+		if strings.HasPrefix(in, "/") {
+			hasPrefix = strings.HasPrefix(in[1:], prefix)
+		} else {
+			hasPrefix = strings.HasPrefix(in, prefix)
+		}
 
-		if prefix != "" {
+		if prefix != "" && !hasPrefix {
 			addSlash := in == "" || strings.HasSuffix(in, "/")
 			in = path.Join(prefix, in)
 
@@ -224,7 +231,15 @@
 
 	if addLanguage {
 		prefix := getLanguagePrefix()
-		if prefix != "" {
+		hasPrefix := false
+		// avoid adding language prefix if already present
+		if strings.HasPrefix(in, "/") {
+			hasPrefix = strings.HasPrefix(in[1:], prefix)
+		} else {
+			hasPrefix = strings.HasPrefix(in, prefix)
+		}
+
+		if prefix != "" && !hasPrefix {
 			hadSlash := strings.HasSuffix(u, "/")
 
 			u = path.Join(prefix, u)
--- a/helpers/url_test.go
+++ b/helpers/url_test.go
@@ -69,6 +69,7 @@
 		expected string
 	}{
 		{"/test/foo", "http://base/", "http://base/MULTItest/foo"},
+		{"/" + lang + "/test/foo", "http://base/", "http://base/" + lang + "/test/foo"},
 		{"", "http://base/ace/", "http://base/ace/MULTI"},
 		{"/test/2/foo/", "http://base", "http://base/MULTItest/2/foo/"},
 		{"http://abs", "http://base/", "http://abs"},
@@ -75,6 +76,7 @@
 		{"schema://abs", "http://base/", "schema://abs"},
 		{"//schemaless", "http://base/", "//schemaless"},
 		{"test/2/foo/", "http://base/path", "http://base/path/MULTItest/2/foo/"},
+		{lang + "/test/2/foo/", "http://base/path", "http://base/path/" + lang + "/test/2/foo/"},
 		{"/test/2/foo/", "http://base/path", "http://base/MULTItest/2/foo/"},
 		{"http//foo", "http://base/path", "http://base/path/MULTIhttp/foo"},
 	}
@@ -141,6 +143,8 @@
 		expected string
 	}{
 		{"/test/foo", "http://base/", false, "MULTI/test/foo"},
+		{"/" + lang + "/test/foo", "http://base/", false, "/" + lang + "/test/foo"},
+		{lang + "/test/foo", "http://base/", false, "/" + lang + "/test/foo"},
 		{"test.css", "http://base/sub", false, "/subMULTI/test.css"},
 		{"test.css", "http://base/sub", true, "MULTI/test.css"},
 		{"/test/", "http://base/", false, "MULTI/test/"},