shithub: hugo

Download patch

ref: aebfe156fb2f27057e61b2e50c7576e6b06dab58
parent: 907d9e92682ed56a57a2206ae9bd9a985b3e1870
author: ivan-meridianbanc-com <ivan@meridianbanc.com>
date: Wed Dec 2 08:47:01 EST 2020

Fix RelURL and AbsURL when path starts with language


--- a/helpers/url.go
+++ b/helpers/url.go
@@ -181,10 +181,14 @@
 		if prefix != "" {
 			hasPrefix := false
 			// avoid adding language prefix if already present
+			in2 := in
 			if strings.HasPrefix(in, "/") {
-				hasPrefix = strings.HasPrefix(in[1:], prefix)
+				in2 = in[1:]
+			}
+			if in2 == prefix {
+				hasPrefix = true
 			} else {
-				hasPrefix = strings.HasPrefix(in, prefix)
+				hasPrefix = strings.HasPrefix(in2, prefix+"/")
 			}
 
 			if !hasPrefix {
@@ -230,10 +234,14 @@
 		if prefix != "" {
 			hasPrefix := false
 			// avoid adding language prefix if already present
+			in2 := in
 			if strings.HasPrefix(in, "/") {
-				hasPrefix = strings.HasPrefix(in[1:], prefix)
+				in2 = in[1:]
+			}
+			if in2 == prefix {
+				hasPrefix = true
 			} else {
-				hasPrefix = strings.HasPrefix(in, prefix)
+				hasPrefix = strings.HasPrefix(in2, prefix+"/")
 			}
 
 			if !hasPrefix {
--- a/helpers/url_test.go
+++ b/helpers/url_test.go
@@ -84,6 +84,21 @@
 		{"http//foo", "http://base/path", "http://base/path/MULTIhttp/foo"},
 	}
 
+	if multilingual && addLanguage && defaultInSubDir {
+		newTests := []struct {
+			input    string
+			baseURL  string
+			expected string
+		}{
+			{lang + "test", "http://base/", "http://base/" + lang + "/" + lang + "test"},
+			{"/" + lang + "test", "http://base/", "http://base/" + lang + "/" + lang + "test"},
+		}
+
+		for _, test := range newTests {
+			tests = append(tests, test)
+		}
+	}
+
 	for _, test := range tests {
 		v.Set("baseURL", test.baseURL)
 		v.Set("contentDir", "content")
@@ -162,6 +177,22 @@
 		{"", "http://base/ace", false, "/aceMULTI"},
 		{"http://abs", "http://base/", false, "http://abs"},
 		{"//schemaless", "http://base/", false, "//schemaless"},
+	}
+
+	if multilingual && addLanguage && defaultInSubDir {
+		newTests := []struct {
+			input    string
+			baseURL  string
+			canonify bool
+			expected string
+		}{
+			{lang + "test", "http://base/", false, "/" + lang + "/" + lang + "test"},
+			{"/" + lang + "test", "http://base/", false, "/" + lang + "/" + lang + "test"},
+		}
+
+		for _, test := range newTests {
+			tests = append(tests, test)
+		}
 	}
 
 	for i, test := range tests {