Skip to content

Commit

Permalink
Keep trailing slash when baseUrl contains a sub path
Browse files Browse the repository at this point in the history
  • Loading branch information
bep authored and tychoish committed Aug 13, 2017
1 parent e060c0f commit ed43a6e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
16 changes: 13 additions & 3 deletions helpers/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ func (PathBridge) Separator() string {

var pathBridge PathBridge

// SanitizeUrl sanitizes the input URL string.
func SanitizeUrl(in string) string {
s, err := purell.NormalizeURLString(in, purell.FlagsSafe|purell.FlagRemoveTrailingSlash|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator)
func sanitizeUrlWithFlags(in string, f purell.NormalizationFlags) string {
s, err := purell.NormalizeURLString(in, f)
if err != nil {
return in
}
Expand Down Expand Up @@ -85,6 +84,17 @@ func SanitizeUrl(in string) string {
// End temporary kludge

//return s

}

// SanitizeUrl sanitizes the input URL string.
func SanitizeUrl(in string) string {
return sanitizeUrlWithFlags(in, purell.FlagsSafe|purell.FlagRemoveTrailingSlash|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator)
}

// SanitizeUrlKeepTrailingSlash is the same as SanitizeUrl, but will keep any trailing slash.
func SanitizeUrlKeepTrailingSlash(in string) string {
return sanitizeUrlWithFlags(in, purell.FlagsSafe|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator)
}

// Similar to MakePath, but with Unicode handling
Expand Down
32 changes: 30 additions & 2 deletions helpers/url_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package helpers

import (
"testing"

"github.com/stretchr/testify/assert"
"strings"
"testing"
)

func TestUrlize(t *testing.T) {
Expand All @@ -26,6 +26,34 @@ func TestUrlize(t *testing.T) {
}
}

func TestSanitizeUrl(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"http://foo.bar/", "http://foo.bar/"},
{"http://foo.bar/zoo/", "http://foo.bar/zoo"}, // issue #931
}

for _, test := range tests {
o1 := SanitizeUrl(test.input)
o2 := SanitizeUrlKeepTrailingSlash(test.input)

expected2 := test.expected

if strings.HasSuffix(test.input, "/") && !strings.HasSuffix(expected2, "/") {
expected2 += "/"
}

if o1 != test.expected {
t.Errorf("Expected %#v, got %#v\n", test.expected, o1)
}
if o2 != expected2 {
t.Errorf("Expected %#v, got %#v\n", expected2, o2)
}
}
}

func TestMakePermalink(t *testing.T) {
type test struct {
host, link, output string
Expand Down
2 changes: 1 addition & 1 deletion hugolib/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func (s *Site) initializeSiteInfo() {
}

s.Info = SiteInfo{
BaseUrl: template.URL(helpers.SanitizeUrl(viper.GetString("BaseUrl"))),
BaseUrl: template.URL(helpers.SanitizeUrlKeepTrailingSlash(viper.GetString("BaseUrl"))),
Title: viper.GetString("Title"),
Author: viper.GetStringMap("author"),
LanguageCode: viper.GetString("languagecode"),
Expand Down

0 comments on commit ed43a6e

Please sign in to comment.