diff options
author | bep <bjorn.erik.pedersen@gmail.com> | 2014-12-12 20:28:28 +0100 |
---|---|---|
committer | bep <bjorn.erik.pedersen@gmail.com> | 2015-01-23 14:13:00 +0100 |
commit | 1b42dc572a0475596ffc64f8a9e0dbf8ec823094 (patch) | |
tree | 3be943bbec8906a7c65bbf992224e0e4c15f5073 /helpers | |
parent | 743998306a02a683371caeac477501418894a8f5 (diff) |
Fix RelPermalink() and Urls in menus vs canonifyUrls
canonifyUrls=true, RelPermalink and baseUrl with sub-path did not work.
This fixes that by adding a check for canonifyUrl=trues=true in RelPermalink().
So given
- baseUrl "http://somehost.com/sub/"
- the path "some-path/file.html"
For canonifyUrls=false RelPermalink() returns "/sub/some-path/file.html"
For canonifyUrls=true RelPermalink() returns "/some-path/file.html"
In the last case, the Url will be made absolute and clickable in a later step.
This commit also makes the menu urls defined in site config releative. To make them work with canonifying of urls, the context root is prepended if canonifying is turned off.
Fixes #519
Fixes #711
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/url.go | 19 | ||||
-rw-r--r-- | helpers/url_test.go | 23 |
2 files changed, 42 insertions, 0 deletions
diff --git a/helpers/url.go b/helpers/url.go index 46bc951f4..1b7608178 100644 --- a/helpers/url.go +++ b/helpers/url.go @@ -78,6 +78,25 @@ func MakePermalink(host, plink string) *url.URL { return base } +// 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. +func AddContextRoot(baseUrl, relativePath string) string { + + url, err := url.Parse(baseUrl) + if err != nil { + panic(err) + } + + newPath := path.Join(url.Path, relativePath) + + // path strips traling slash + if strings.HasSuffix(relativePath, "/") { + newPath += "/" + } + return newPath +} + func UrlPrep(ugly bool, in string) string { if ugly { x := Uglify(SanitizeUrl(in)) diff --git a/helpers/url_test.go b/helpers/url_test.go index 1d5c770ea..3df1a05c2 100644 --- a/helpers/url_test.go +++ b/helpers/url_test.go @@ -68,6 +68,29 @@ func TestUrlPrep(t *testing.T) { } +func TestAddContextRoot(t *testing.T) { + tests := []struct { + baseUrl string + url string + expected string + }{ + {"http://example.com/sub/", "/foo", "/sub/foo"}, + {"http://example.com/sub/", "/foo/index.html", "/sub/foo/index.html"}, + {"http://example.com/sub1/sub2", "/foo", "/sub1/sub2/foo"}, + {"http://example.com", "/foo", "/foo"}, + // cannot guess that the context root is already added int the example below + {"http://example.com/sub/", "/sub/foo", "/sub/sub/foo"}, + {"http://example.com/тря", "/трям/", "/тря/трям/"}, + } + + for _, test := range tests { + output := AddContextRoot(test.baseUrl, test.url) + if output != test.expected { + t.Errorf("Expected %#v, got %#v\n", test.expected, output) + } + } +} + func TestPretty(t *testing.T) { assert.Equal(t, PrettifyUrlPath("/section/name.html"), "/section/name/index.html") assert.Equal(t, PrettifyUrlPath("/section/sub/name.html"), "/section/sub/name/index.html") |