Fix aliases with uglyURLs
authorHelder Pereira <helfper@gmail.com>
Sun, 14 Jun 2020 21:33:00 +0000 (22:33 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 15 Jun 2020 10:19:59 +0000 (12:19 +0200)
hugolib/alias_test.go
hugolib/site_render.go

index 17c66349620e7a86941b9c3fe648e8ff6e20f9d9..a1736e7e8efbfb26bcaac3fdd88487d2ebe3cb52 100644 (file)
@@ -46,12 +46,15 @@ func TestAlias(t *testing.T) {
        c := qt.New(t)
 
        tests := []struct {
-               urlPrefix string
-               settings  map[string]interface{}
+               fileSuffix string
+               urlPrefix  string
+               urlSuffix  string
+               settings   map[string]interface{}
        }{
-               {"http://example.com", map[string]interface{}{"baseURL": "http://example.com"}},
-               {"http://example.com", map[string]interface{}{"baseURL": "http://example.com", "canonifyURLs": true}},
-               {"../..", map[string]interface{}{"relativeURLs": true}},
+               {"/index.html", "http://example.com", "/", map[string]interface{}{"baseURL": "http://example.com"}},
+               {"/index.html", "http://example.com", "/", map[string]interface{}{"baseURL": "http://example.com", "canonifyURLs": true}},
+               {"/index.html", "../..", "/", map[string]interface{}{"relativeURLs": true}},
+               {".html", "", ".html", map[string]interface{}{"uglyURLs": true}},
        }
 
        for _, test := range tests {
@@ -63,10 +66,10 @@ func TestAlias(t *testing.T) {
                c.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 1)
 
                // the real page
-               b.AssertFileContent("public/blog/page/index.html", "For some moments the old man")
+               b.AssertFileContent("public/blog/page"+test.fileSuffix, "For some moments the old man")
                // the alias redirectors
-               b.AssertFileContent("public/foo/bar/index.html", "<meta http-equiv=\"refresh\" content=\"0; url="+test.urlPrefix+"/blog/page/\" />")
-               b.AssertFileContent("public/blog/rel/index.html", "<meta http-equiv=\"refresh\" content=\"0; url="+test.urlPrefix+"/blog/page/\" />")
+               b.AssertFileContent("public/foo/bar"+test.fileSuffix, "<meta http-equiv=\"refresh\" content=\"0; url="+test.urlPrefix+"/blog/page"+test.urlSuffix+"\" />")
+               b.AssertFileContent("public/blog/rel"+test.fileSuffix, "<meta http-equiv=\"refresh\" content=\"0; url="+test.urlPrefix+"/blog/page"+test.urlSuffix+"\" />")
        }
 }
 
index 5377babef557b785c3b9646285a522696d55bee9..1d397dafa96b1073234539b8c9dfc47396bca5f5 100644 (file)
@@ -338,19 +338,18 @@ func (s *Site) renderAliases() error {
                                if isRelative {
                                        // Make alias relative, where "." will be on the
                                        // same directory level as the current page.
-                                       // TODO(bep) ugly URLs doesn't seem to be supported in
-                                       // aliases, I'm not sure why not.
-                                       basePath := of.RelPermalink()
-                                       if strings.HasSuffix(basePath, "/") {
-                                               basePath = path.Join(basePath, "..")
-                                       }
+                                       basePath := path.Join(of.RelPermalink(), "..")
                                        a = path.Join(basePath, a)
 
-                               } else if f.Path != "" {
+                               } else {
                                        // Make sure AMP and similar doesn't clash with regular aliases.
                                        a = path.Join(f.Path, a)
                                }
 
+                               if s.UglyURLs && !strings.HasSuffix(a, ".html") {
+                                       a += ".html"
+                               }
+
                                lang := p.Language().Lang
 
                                if s.h.multihost && !strings.HasPrefix(a, "/"+lang) {