Make alias redirect output URL's relative
authorSven Dowideit <SvenDowideit@home.org.au>
Tue, 19 Apr 2016 01:40:15 +0000 (11:40 +1000)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 22 Apr 2016 06:14:19 +0000 (08:14 +0200)
Closes #2093

hugolib/site.go

index 14643fe0aff2574057ef336d5b784b1d9e5c6c25..7537fe8fdea5d72811f34c6ada990ae7739330c9 100644 (file)
@@ -2095,7 +2095,19 @@ func (s *Site) writeDestPage(path string, publisher target.Publisher, reader io.
 }
 
 func (s *Site) writeDestAlias(path string, permalink string) (err error) {
-       jww.DEBUG.Println("creating alias:", path)
+       if viper.GetBool("RelativeURLs") {
+               // convert `permalink` into URI relative to location of `path`
+               baseURL := helpers.SanitizeURLKeepTrailingSlash(viper.GetString("BaseURL"))
+               if strings.HasPrefix(permalink, baseURL) {
+                       permalink = "/" + strings.TrimPrefix(permalink, baseURL)
+               }
+               permalink, err = helpers.GetRelativePath(permalink, path)
+               if err != nil {
+                       jww.ERROR.Println("Failed to make a RelativeURL alias:", path, "redirecting to", permalink)
+               }
+               permalink = filepath.ToSlash(permalink)
+       }
+       jww.DEBUG.Println("creating alias:", path, "redirecting to", permalink)
        return s.aliasTarget().Publish(path, permalink)
 }