aboutsummaryrefslogtreecommitdiffhomepage
path: root/hugolib/alias.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2019-03-30 17:08:25 +0100
committerBjørn Erik Pedersen <[email protected]>2019-04-02 10:32:47 +0200
commit92baa14fd3f45c0917c5988235cd1a0f8692f171 (patch)
tree130417443701331257f57f3d198eba2a037c079d /hugolib/alias.go
parenta55640de8e3944d3b9f64b15155148a0e35cb31e (diff)
downloadhugo-92baa14fd3f45c0917c5988235cd1a0f8692f171.tar.gz
hugo-92baa14fd3f45c0917c5988235cd1a0f8692f171.zip
hugolib: Allow page-relative aliases
Fixes #5757
Diffstat (limited to 'hugolib/alias.go')
-rw-r--r--hugolib/alias.go21
1 files changed, 9 insertions, 12 deletions
diff --git a/hugolib/alias.go b/hugolib/alias.go
index 599821c0a..972f7b01c 100644
--- a/hugolib/alias.go
+++ b/hugolib/alias.go
@@ -18,6 +18,7 @@ import (
"fmt"
"html/template"
"io"
+ "path"
"path/filepath"
"runtime"
"strings"
@@ -28,8 +29,6 @@ import (
"github.com/gohugoio/hugo/publisher"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/tpl"
-
- "github.com/gohugoio/hugo/helpers"
)
const (
@@ -132,13 +131,14 @@ func (a aliasHandler) targetPathAlias(src string) (string, error) {
return "", fmt.Errorf("alias \"\" is an empty string")
}
- alias := filepath.Clean(src)
- components := strings.Split(alias, helpers.FilePathSeparator)
+ alias := path.Clean(filepath.ToSlash(src))
- if !a.allowRoot && alias == helpers.FilePathSeparator {
+ if !a.allowRoot && alias == "/" {
return "", fmt.Errorf("alias \"%s\" resolves to website root directory", originalAlias)
}
+ components := strings.Split(alias, "/")
+
// Validate against directory traversal
if components[0] == ".." {
return "", fmt.Errorf("alias \"%s\" traverses outside the website root directory", originalAlias)
@@ -182,15 +182,12 @@ func (a aliasHandler) targetPathAlias(src string) (string, error) {
}
// Add the final touch
- alias = strings.TrimPrefix(alias, helpers.FilePathSeparator)
- if strings.HasSuffix(alias, helpers.FilePathSeparator) {
+ alias = strings.TrimPrefix(alias, "/")
+ if strings.HasSuffix(alias, "/") {
alias = alias + "index.html"
} else if !strings.HasSuffix(alias, ".html") {
- alias = alias + helpers.FilePathSeparator + "index.html"
- }
- if originalAlias != alias {
- a.log.INFO.Printf("Alias \"%s\" translated to \"%s\"\n", originalAlias, alias)
+ alias = alias + "/" + "index.html"
}
- return alias, nil
+ return filepath.FromSlash(alias), nil
}