diff options
author | bep <bjorn.erik.pedersen@gmail.com> | 2015-05-11 12:28:44 +0200 |
---|---|---|
committer | bep <bjorn.erik.pedersen@gmail.com> | 2015-05-11 12:28:35 +0200 |
commit | be0cbeee7fb9b6e8af12745971ff80e86e0d3d32 (patch) | |
tree | bc69b9acabe55d40669deb545d1290fdd536c538 /helpers/url.go | |
parent | bec90e085055ef96cbd6a17604cf1020c2c82f24 (diff) |
Add absURL template func
Fixes #1106
Diffstat (limited to 'helpers/url.go')
-rw-r--r-- | helpers/url.go | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/helpers/url.go b/helpers/url.go index c780579d6..502d64a6c 100644 --- a/helpers/url.go +++ b/helpers/url.go @@ -138,13 +138,22 @@ func MakePermalink(host, plink string) *url.URL { base.Path = path.Join(base.Path, p.Path) // path.Join will strip off the last /, so put it back if it was there. - if strings.HasSuffix(p.Path, "/") && !strings.HasSuffix(base.Path, "/") { + hadTrailingSlash := (plink == "" && strings.HasSuffix(host, "/")) || strings.HasSuffix(p.Path, "/") + if hadTrailingSlash && !strings.HasSuffix(base.Path, "/") { base.Path = base.Path + "/" } return base } +// AbsURL creates a absolute URL from the relative path given and the BaseURL set in config. +func AbsURL(path string) string { + if strings.HasPrefix(path, "http") || strings.HasPrefix(path, "//") { + return path + } + return MakePermalink(string(viper.GetString("BaseURL")), path).String() +} + // AddContextRoot adds the context root to an URL if it's not already set. // For relative URL entries on sites with a base url with a context root set (i.e. http://example.com/mysite), // relative URLs must not include the context root if canonifyURLs is enabled. But if it's disabled, it must be set. |