Skip to content

Commit c439325

Browse files
anthonyfokspf13
authored andcommitted
Adapt to relative path bug fix in purell
Temporary workaround for the bug fix and resulting behavioral change in purell.NormalizeURLString(): a leading '/' was inadvertently to relative links, but no longer, see #878. I think the real solution is to allow Hugo to make relative URL with relative path, e.g. "../../post/hello-again/", as wished by users in issues #157, #622, etc., without forcing relative URLs to begin with '/'. Once the fixes are in, let's remove this kludge and restore SanitizeUrl() to the way it was. Fixes #878
1 parent 41c0e82 commit c439325

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

helpers/url.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2013 Steve Francia <[email protected]>.
1+
// Copyright © 2013-2015 Steve Francia <[email protected]>.
22
//
33
// Licensed under the Simple Public License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -54,11 +54,37 @@ var pathBridge PathBridge
5454

5555
// SanitizeUrl sanitizes the input URL string.
5656
func SanitizeUrl(in string) string {
57-
url, err := purell.NormalizeURLString(in, purell.FlagsSafe|purell.FlagRemoveTrailingSlash|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator)
57+
s, err := purell.NormalizeURLString(in, purell.FlagsSafe|purell.FlagRemoveTrailingSlash|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator)
5858
if err != nil {
5959
return in
6060
}
61-
return url
61+
62+
// Temporary workaround for the bug fix and resulting
63+
// behavioral change in purell.NormalizeURLString():
64+
// a leading '/' was inadvertently to relative links,
65+
// but no longer, see #878.
66+
//
67+
// I think the real solution is to allow Hugo to
68+
// make relative URL with relative path,
69+
// e.g. "../../post/hello-again/", as wished by users
70+
// in issues #157, #622, etc., without forcing
71+
// relative URLs to begin with '/'.
72+
// Once the fixes are in, let's remove this kludge
73+
// and restore SanitizeUrl() to the way it was.
74+
// -- @anthonyfok, 2015-02-16
75+
//
76+
// Begin temporary kludge
77+
u, err := url.Parse(s)
78+
if err != nil {
79+
panic(err)
80+
}
81+
if !strings.HasPrefix(u.Path, "/") {
82+
u.Path = "/" + u.Path
83+
}
84+
return u.String()
85+
// End temporary kludge
86+
87+
//return s
6288
}
6389

6490
// Similar to MakePath, but with Unicode handling

0 commit comments

Comments
 (0)