hugolib: Log warning on relative front matter url with lang
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 7 Apr 2019 08:22:19 +0000 (10:22 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 7 Apr 2019 16:54:18 +0000 (18:54 +0200)
Will do this for one version only, as there may be situations where this is the correct thing.

Also add some more related test cases.

Fixes #5818

hugolib/page__meta.go
hugolib/page_permalink_test.go

index eefecbe4a650fc6ac9826c891d299babadfc99c3..8e61ce138b1eb7a86a23d5884c160af15edbd714 100644 (file)
@@ -21,6 +21,8 @@ import (
        "strings"
        "time"
 
+       "github.com/gohugoio/hugo/common/hugo"
+
        "github.com/gohugoio/hugo/related"
 
        "github.com/gohugoio/hugo/source"
@@ -374,11 +376,22 @@ func (pm *pageMeta) setMetadata(p *pageState, frontmatter map[string]interface{}
                        pm.urlPaths.Slug = strings.Trim(cast.ToString(v), "-")
                        pm.params[loki] = pm.Slug()
                case "url":
-                       if url := cast.ToString(v); strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
-                               return fmt.Errorf("only relative URLs are supported, %v provided", url)
+                       url := cast.ToString(v)
+                       if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
+                               return fmt.Errorf("URLs with protocol (http*) not supported: %q. In page %q", url, p.pathOrTitle())
+                       }
+                       lang := p.s.GetLanguagePrefix()
+                       if lang != "" && !strings.HasPrefix(url, "/") && strings.HasPrefix(url, lang) {
+                               if strings.HasPrefix(hugo.CurrentVersion.String(), "0.55") {
+                                       // We added support for page relative URLs in Hugo 0.55 and
+                                       // this may get its language path added twice.
+                                       // TODO(bep) eventually remove this.
+                                       p.s.Log.WARN.Printf(`Front matter in %q with the url %q with no leading / has what looks like the language prefix added. In Hugo 0.55 we added support for page relative URLs in front matter, no language prefix needed. Check the URL and consider to either add a leading / or remove the language prefix.`, p.pathOrTitle(), url)
+
+                               }
                        }
-                       pm.urlPaths.URL = cast.ToString(v)
-                       pm.params[loki] = pm.urlPaths.URL
+                       pm.urlPaths.URL = url
+                       pm.params[loki] = url
                case "type":
                        pm.contentType = cast.ToString(v)
                        pm.params[loki] = pm.contentType
index 9df10cd53f692d7d544e065b7abcf92cb370a067..526f9578b5bd5ef7e6016bd6c82e5defb3c5c4fa 100644 (file)
@@ -108,7 +108,7 @@ Content
 func TestRelativeURLInFrontMatter(t *testing.T) {
 
        config := `
-
+baseURL = "https://example.com"
 defaultContentLanguage = "en"
 defaultContentLanguageInSubdir = false
 
@@ -132,6 +132,8 @@ Some content.
 
        b := newTestSitesBuilder(t).WithConfigFile("toml", config)
        b.WithContent("content/en/blog/page1.md", fmt.Sprintf(pageTempl, "myblog/p1/"))
+       b.WithContent("content/en/blog/page2.md", fmt.Sprintf(pageTempl, "../../../../../myblog/p2/"))
+       b.WithContent("content/en/blog/page3.md", fmt.Sprintf(pageTempl, "../myblog/../myblog/p3/"))
        b.WithContent("content/en/blog/_index.md", fmt.Sprintf(pageTempl, "this-is-my-english-blog"))
        b.WithContent("content/nn/blog/page1.md", fmt.Sprintf(pageTempl, "myblog/p1/"))
        b.WithContent("content/nn/blog/_index.md", fmt.Sprintf(pageTempl, "this-is-my-blog"))
@@ -139,8 +141,10 @@ Some content.
        b.Build(BuildCfg{})
 
        b.AssertFileContent("public/nn/myblog/p1/index.html", "Single: A page|Hello|nn|RelPermalink: /nn/myblog/p1/|")
-       b.AssertFileContent("public/nn/this-is-my-blog/index.html", "List Page 1|A page|Hello|/nn/this-is-my-blog/|")
-       b.AssertFileContent("public/this-is-my-english-blog/index.html", "List Page 1|A page|Hello|/this-is-my-english-blog/|")
-       b.AssertFileContent("public/myblog/p1/index.html", "Single: A page|Hello|en|RelPermalink: /myblog/p1/|Permalink: /myblog/p1/|")
+       b.AssertFileContent("public/nn/this-is-my-blog/index.html", "List Page 1|A page|Hello|https://example.com/nn/this-is-my-blog/|")
+       b.AssertFileContent("public/this-is-my-english-blog/index.html", "List Page 1|A page|Hello|https://example.com/this-is-my-english-blog/|")
+       b.AssertFileContent("public/myblog/p1/index.html", "Single: A page|Hello|en|RelPermalink: /myblog/p1/|Permalink: https://example.com/myblog/p1/|")
+       b.AssertFileContent("public/myblog/p2/index.html", "Single: A page|Hello|en|RelPermalink: /myblog/p2/|Permalink: https://example.com/myblog/p2/|")
+       b.AssertFileContent("public/myblog/p3/index.html", "Single: A page|Hello|en|RelPermalink: /myblog/p3/|Permalink: https://example.com/myblog/p3/|")
 
 }