mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-21 21:35:28 +02:00
Support subdir in baseurl.
Mainly this was a change to helpers.MakePermalink, but to get the local server to run correctly, we needed to redirect the path of the request from /foo to /. In addition, I added tests for the server's code for fixing up the base url with different config file & CLI options.
This commit is contained in:
@@ -97,3 +97,25 @@ func TestUrlize(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMakePermalink(t *testing.T) {
|
||||
type test struct {
|
||||
host, link, output string
|
||||
}
|
||||
|
||||
data := []test{
|
||||
{"http://abc.com/foo", "post/bar", "http://abc.com/foo/post/bar"},
|
||||
{"http://abc.com/foo/", "post/bar", "http://abc.com/foo/post/bar"},
|
||||
{"http://abc.com", "post/bar", "http://abc.com/post/bar"},
|
||||
{"http://abc.com", "bar", "http://abc.com/bar"},
|
||||
{"http://abc.com/foo/bar", "post/bar", "http://abc.com/foo/bar/post/bar"},
|
||||
{"http://abc.com/foo/bar", "post/bar/", "http://abc.com/foo/bar/post/bar/"},
|
||||
}
|
||||
|
||||
for i, d := range data {
|
||||
output := MakePermalink(d.host, d.link).String()
|
||||
if d.output != output {
|
||||
t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -14,8 +14,10 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/PuerkitoBio/purell"
|
||||
)
|
||||
@@ -57,11 +59,23 @@ func MakePermalink(host, plink string) *url.URL {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
path, err := url.Parse(plink)
|
||||
p, err := url.Parse(plink)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return base.ResolveReference(path)
|
||||
|
||||
if p.Host != "" {
|
||||
panic(fmt.Errorf("Can't make permalink from absolute link %q", plink))
|
||||
}
|
||||
|
||||
base.Path = path.Join(base.Path, p.Path)
|
||||
|
||||
// path.Join will strip off the last /, so put it back if it was there.
|
||||
if strings.HasSuffix(p.Path, "/") && !strings.HasSuffix(base.Path, "/") {
|
||||
base.Path = base.Path + "/"
|
||||
}
|
||||
|
||||
return base
|
||||
}
|
||||
|
||||
func UrlPrep(ugly bool, in string) string {
|
||||
|
Reference in New Issue
Block a user