diff options
author | bep <bjorn.erik.pedersen@gmail.com> | 2015-05-11 13:59:06 +0200 |
---|---|---|
committer | bep <bjorn.erik.pedersen@gmail.com> | 2015-05-11 13:59:02 +0200 |
commit | bec839e652838a1804bf0dfa3879d69fa9c1f130 (patch) | |
tree | 8cc7ab44316301edc81414555f14584f2dd0740d /helpers/url.go | |
parent | be0cbeee7fb9b6e8af12745971ff80e86e0d3d32 (diff) |
Add relURL template func
Fixes #1126
Diffstat (limited to 'helpers/url.go')
-rw-r--r-- | helpers/url.go | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/helpers/url.go b/helpers/url.go index 502d64a6c..ce36bc184 100644 --- a/helpers/url.go +++ b/helpers/url.go @@ -151,7 +151,36 @@ func AbsURL(path string) string { if strings.HasPrefix(path, "http") || strings.HasPrefix(path, "//") { return path } - return MakePermalink(string(viper.GetString("BaseURL")), path).String() + return MakePermalink(viper.GetString("BaseURL"), path).String() +} + +// RelURL creates a URL relative to the BaseURL root. +// Note: The result URL will not include the context root if canonifyURLs is enabled. +func RelURL(path string) string { + baseURL := viper.GetString("BaseURL") + canonifyURLs := viper.GetBool("canonifyURLs") + if (!strings.HasPrefix(path, baseURL) && strings.HasPrefix(path, "http")) || strings.HasPrefix(path, "//") { + return path + } + + u := path + + if strings.HasPrefix(path, baseURL) { + u = strings.TrimPrefix(u, baseURL) + } + + if !canonifyURLs { + u = AddContextRoot(baseURL, u) + } + if path == "" && !strings.HasSuffix(u, "/") && strings.HasSuffix(baseURL, "/") { + u += "/" + } + + if !strings.HasPrefix(u, "/") { + u = "/" + u + } + + return u } // AddContextRoot adds the context root to an URL if it's not already set. |