]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
helpers: Improve schema detection when creating relative URLs
authorJoe Mooring <joe.mooring@veriphor.com>
Sat, 10 Jun 2023 16:26:35 +0000 (09:26 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 12 Jun 2023 13:01:30 +0000 (15:01 +0200)
Fixes #11080

helpers/url.go
helpers/url_test.go

index a4c20c6ad8dc44340b137ee16f0a73cb8853ac2c..bd336545b08475b04be142e1931101235b7aad21 100644 (file)
@@ -152,7 +152,13 @@ func (p *PathSpec) getBaseURLRoot(path string) string {
 func (p *PathSpec) RelURL(in string, addLanguage bool) string {
        baseURL := p.getBaseURLRoot(in)
        canonifyURLs := p.Cfg.CanonifyURLs()
-       if (!strings.HasPrefix(in, baseURL) && strings.HasPrefix(in, "http")) || strings.HasPrefix(in, "//") {
+
+       url, err := url.Parse(in)
+       if err != nil {
+               return in
+       }
+
+       if (!strings.HasPrefix(in, baseURL) && url.IsAbs()) || strings.HasPrefix(in, "//") {
                return in
        }
 
index bb7f6eed747f1cd99de3abaa86756c48ec8d2969..6d496ce8ef6d37263310a7cb72ce9d4a7de4b6ea 100644 (file)
@@ -177,6 +177,10 @@ func doTestRelURL(t testing.TB, defaultInSubDir, addLanguage, multilingual bool,
                {"/foo/bar", "https://example.org/foo/", false, "MULTI/foo/bar"},
                {"foo/bar", "https://example.org/foo/", false, "/fooMULTI/foo/bar"},
 
+               // Issue 11080
+               {"mailto:a@b.com", "http://base/", false, "mailto:a@b.com"},
+               {"ftp://b.com/a.txt", "http://base/", false, "ftp://b.com/a.txt"},
+
                {"/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"},