mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Make per-page url overrides take precedence over section permalink settings
This seems more reasonable to me. I didn't understand why the pUrl comparision was on len > 2; I changed it to 0. Closes #212.
This commit is contained in:
parent
14227351fa
commit
1729628fd3
2 changed files with 23 additions and 14 deletions
|
@ -235,17 +235,19 @@ func (p *Page) permalink() (*url.URL, error) {
|
||||||
var permalink string
|
var permalink string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
if len(pUrl) > 0 {
|
||||||
|
return helpers.MakePermalink(baseUrl, pUrl), nil
|
||||||
|
}
|
||||||
|
|
||||||
if override, ok := p.Site.Permalinks[p.Section]; ok {
|
if override, ok := p.Site.Permalinks[p.Section]; ok {
|
||||||
permalink, err = override.Expand(p)
|
permalink, err = override.Expand(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
//fmt.Printf("have an override for %q in section %s → %s\n", p.Title, p.Section, permalink)
|
// fmt.Printf("have a section override for %q in section %s → %s\n", p.Title, p.Section, permalink)
|
||||||
} else {
|
} else {
|
||||||
if len(pSlug) > 0 {
|
if len(pSlug) > 0 {
|
||||||
permalink = helpers.UrlPrep(p.Site.Config.UglyUrls, path.Join(dir, p.Slug+"."+p.Extension))
|
permalink = helpers.UrlPrep(p.Site.Config.UglyUrls, path.Join(dir, p.Slug+"."+p.Extension))
|
||||||
} else if len(pUrl) > 2 {
|
|
||||||
permalink = pUrl
|
|
||||||
} else {
|
} else {
|
||||||
_, t := path.Split(p.FileName)
|
_, t := path.Split(p.FileName)
|
||||||
permalink = helpers.UrlPrep(p.Site.Config.UglyUrls, path.Join(dir, helpers.ReplaceExtension(strings.TrimSpace(t), p.Extension)))
|
permalink = helpers.UrlPrep(p.Site.Config.UglyUrls, path.Join(dir, helpers.ReplaceExtension(strings.TrimSpace(t), p.Extension)))
|
||||||
|
|
|
@ -11,26 +11,33 @@ func TestPermalink(t *testing.T) {
|
||||||
dir string
|
dir string
|
||||||
base template.URL
|
base template.URL
|
||||||
slug string
|
slug string
|
||||||
|
url string
|
||||||
uglyurls bool
|
uglyurls bool
|
||||||
expectedAbs string
|
expectedAbs string
|
||||||
expectedRel string
|
expectedRel string
|
||||||
}{
|
}{
|
||||||
{"x/y/z/boofar.md", "x/y/z", "", "", false, "/x/y/z/boofar", "/x/y/z/boofar"},
|
{"x/y/z/boofar.md", "x/y/z", "", "", "", false, "/x/y/z/boofar", "/x/y/z/boofar"},
|
||||||
{"x/y/z/boofar.md", "x/y/z/", "", "", false, "/x/y/z/boofar", "/x/y/z/boofar"},
|
{"x/y/z/boofar.md", "x/y/z/", "", "", "", false, "/x/y/z/boofar", "/x/y/z/boofar"},
|
||||||
{"x/y/z/boofar.md", "x/y/z/", "", "boofar", false, "/x/y/z/boofar", "/x/y/z/boofar"},
|
{"x/y/z/boofar.md", "x/y/z/", "", "boofar", "", false, "/x/y/z/boofar", "/x/y/z/boofar"},
|
||||||
{"x/y/z/boofar.md", "x/y/z", "http://barnew/", "", false, "http://barnew/x/y/z/boofar", "/x/y/z/boofar"},
|
{"x/y/z/boofar.md", "x/y/z", "http://barnew/", "", "", false, "http://barnew/x/y/z/boofar", "/x/y/z/boofar"},
|
||||||
{"x/y/z/boofar.md", "x/y/z/", "http://barnew/", "boofar", false, "http://barnew/x/y/z/boofar", "/x/y/z/boofar"},
|
{"x/y/z/boofar.md", "x/y/z/", "http://barnew/", "boofar", "", false, "http://barnew/x/y/z/boofar", "/x/y/z/boofar"},
|
||||||
{"x/y/z/boofar.md", "x/y/z", "", "", true, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
{"x/y/z/boofar.md", "x/y/z", "", "", "", true, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
||||||
{"x/y/z/boofar.md", "x/y/z/", "", "", true, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
{"x/y/z/boofar.md", "x/y/z/", "", "", "", true, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
||||||
{"x/y/z/boofar.md", "x/y/z/", "", "boofar", true, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
{"x/y/z/boofar.md", "x/y/z/", "", "boofar", "", true, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
||||||
{"x/y/z/boofar.md", "x/y/z", "http://barnew/", "", true, "http://barnew/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
{"x/y/z/boofar.md", "x/y/z", "http://barnew/", "", "", true, "http://barnew/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
||||||
{"x/y/z/boofar.md", "x/y/z/", "http://barnew/", "boofar", true, "http://barnew/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
{"x/y/z/boofar.md", "x/y/z/", "http://barnew/", "boofar", "", true, "http://barnew/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
||||||
|
|
||||||
|
// test url overrides
|
||||||
|
{"x/y/z/boofar.md", "x/y/z", "", "", "/z/y/q/", false, "/z/y/q/", "/z/y/q/"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
p := &Page{
|
p := &Page{
|
||||||
Node: Node{
|
Node: Node{
|
||||||
UrlPath: UrlPath{Section: "z"},
|
UrlPath: UrlPath{
|
||||||
|
Section: "z",
|
||||||
|
Url: test.url,
|
||||||
|
},
|
||||||
Site: SiteInfo{
|
Site: SiteInfo{
|
||||||
BaseUrl: test.base,
|
BaseUrl: test.base,
|
||||||
Config: &Config{
|
Config: &Config{
|
||||||
|
|
Loading…
Reference in a new issue