mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-20 14:53:28 +00:00
parent
bec90e0850
commit
be0cbeee7f
3 changed files with 34 additions and 1 deletions
|
@ -138,13 +138,22 @@ func MakePermalink(host, plink string) *url.URL {
|
||||||
base.Path = path.Join(base.Path, p.Path)
|
base.Path = path.Join(base.Path, p.Path)
|
||||||
|
|
||||||
// path.Join will strip off the last /, so put it back if it was there.
|
// 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 + "/"
|
base.Path = base.Path + "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
return base
|
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.
|
// 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),
|
// 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.
|
// relative URLs must not include the context root if canonifyURLs is enabled. But if it's disabled, it must be set.
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package helpers
|
package helpers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/spf13/viper"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -26,6 +27,28 @@ func TestURLize(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAbsURL(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input string
|
||||||
|
baseURL string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{"/test/foo", "http://base/", "http://base/test/foo"},
|
||||||
|
{"", "http://base/ace/", "http://base/ace/"},
|
||||||
|
{"/test/2/foo/", "http://base", "http://base/test/2/foo/"},
|
||||||
|
{"http://abs", "http://base/", "http://abs"},
|
||||||
|
{"//schemaless", "http://base/", "//schemaless"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
viper.Set("BaseURL", test.baseURL)
|
||||||
|
output := AbsURL(test.input)
|
||||||
|
if output != test.expected {
|
||||||
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSanitizeURL(t *testing.T) {
|
func TestSanitizeURL(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
input string
|
input string
|
||||||
|
|
|
@ -1196,6 +1196,7 @@ func init() {
|
||||||
"safeHTML": SafeHTML,
|
"safeHTML": SafeHTML,
|
||||||
"safeCSS": SafeCSS,
|
"safeCSS": SafeCSS,
|
||||||
"safeURL": SafeURL,
|
"safeURL": SafeURL,
|
||||||
|
"absURL": func(a string) template.HTML { return template.HTML(helpers.AbsURL(a)) },
|
||||||
"markdownify": Markdownify,
|
"markdownify": Markdownify,
|
||||||
"first": First,
|
"first": First,
|
||||||
"where": Where,
|
"where": Where,
|
||||||
|
|
Loading…
Add table
Reference in a new issue