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
This commit is contained in:
bep
2014-12-12 20:28:28 +01:00
parent 743998306a
commit 1b42dc572a
8 changed files with 112 additions and 31 deletions

View File

@@ -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))