hugolib: Allow url in front matter for list type pages

This enables some potential foot-shooting, but is needed for some special URL requirements.

Fixes #4263
This commit is contained in:
Bjørn Erik Pedersen 2018-01-12 17:17:10 +01:00
parent 46db900dab
commit 8a409894bd
3 changed files with 49 additions and 46 deletions

View file

@ -591,7 +591,7 @@ func TestNodesWithURLs(t *testing.T) {
writeSource(t, fs, filepath.Join("content", "sect", "_index.md"), `---
title: MySection
url: foo.html
url: /my-section/
---
My Section Content
`)
@ -602,7 +602,8 @@ My Section Content
require.NoError(t, h.Build(BuildCfg{}))
th.assertFileContent(filepath.Join("public", "sect", "index.html"), "My Section")
th.assertFileContent(filepath.Join("public", "my-section", "index.html"), "My Section")
th.assertFileContent(filepath.Join("public", "my-section", "page", "1", "index.html"), `content="0; url=http://bep.is/base/my-section/"`)
s := h.Sites[0]
@ -610,11 +611,11 @@ My Section Content
require.Equal(t, "/base/sect1/regular1/", p.URL())
// Section with front matter and url set (which should not be used)
// Section with front matter and url set
sect := s.getPage(KindSection, "sect")
require.Equal(t, "/base/sect/", sect.URL())
require.Equal(t, "http://bep.is/base/sect/", sect.Permalink())
require.Equal(t, "/base/sect/", sect.RelPermalink())
require.Equal(t, "/base/my-section/", sect.URL())
require.Equal(t, "http://bep.is/base/my-section/", sect.Permalink())
require.Equal(t, "/base/my-section/", sect.RelPermalink())
// Home page without front matter
require.Equal(t, "/base/", s.getPage(KindHome).URL())

View file

@ -223,7 +223,6 @@ type Page struct {
Lastmod time.Time
Sitemap Sitemap
URLPath
permalink string
relPermalink string
@ -1111,6 +1110,7 @@ func (p *Page) update(f interface{}) error {
return fmt.Errorf("Only relative URLs are supported, %v provided", url)
}
p.URLPath.URL = cast.ToString(v)
p.URLPath.frontMatterURL = p.URLPath.URL
p.Params[loki] = p.URLPath.URL
case "type":
p.contentType = cast.ToString(v)
@ -1809,10 +1809,11 @@ func (p *Page) String() string {
}
type URLPath struct {
URL string
Permalink string
Slug string
Section string
URL string
frontMatterURL string
Permalink string
Slug string
Section string
}
// Scratch returns the writable context associated with this Page.
@ -1991,7 +1992,9 @@ func (p *Page) setValuesForKind(s *Site) {
p.URLPath.URL = "/"
case KindPage:
default:
p.URLPath.URL = "/" + path.Join(p.sections...) + "/"
if p.URLPath.URL == "" {
p.URLPath.URL = "/" + path.Join(p.sections...) + "/"
}
}
}

View file

@ -56,7 +56,7 @@ type targetPathDescriptor struct {
// Whether this is a multihost multilingual setup.
IsMultihost bool
// Page.URLPath.URL. Will override any Slug etc. for regular pages.
// URL from front matter if set. Will override any Slug etc.
URL string
// Used to create paginator links.
@ -88,7 +88,7 @@ func (p *Page) initTargetPathDescriptor() error {
Sections: p.sections,
UglyURLs: p.s.Info.uglyURLs(p),
Dir: filepath.ToSlash(p.Source.Dir()),
URL: p.URLPath.URL,
URL: p.URLPath.frontMatterURL,
IsMultihost: p.s.owner.IsMultihost(),
}
@ -189,7 +189,7 @@ func createTargetPath(d targetPathDescriptor) string {
isUgly = true
}
if d.Kind != KindPage && len(d.Sections) > 0 {
if d.Kind != KindPage && d.URL == "" && len(d.Sections) > 0 {
if d.ExpandedPermalink != "" {
pagePath = filepath.Join(pagePath, d.ExpandedPermalink)
} else {
@ -202,43 +202,42 @@ func createTargetPath(d targetPathDescriptor) string {
pagePath = filepath.Join(pagePath, d.Type.Path)
}
if d.Kind == KindPage {
// Always use URL if it's specified
if d.URL != "" {
if d.IsMultihost && d.LangPrefix != "" && !strings.HasPrefix(d.URL, "/"+d.LangPrefix) {
pagePath = filepath.Join(d.LangPrefix, pagePath, d.URL)
} else {
pagePath = filepath.Join(pagePath, d.URL)
}
if strings.HasSuffix(d.URL, "/") || !strings.Contains(d.URL, ".") {
pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
}
if d.Kind != KindHome && d.URL != "" {
if d.IsMultihost && d.LangPrefix != "" && !strings.HasPrefix(d.URL, "/"+d.LangPrefix) {
pagePath = filepath.Join(d.LangPrefix, pagePath, d.URL)
} else {
if d.ExpandedPermalink != "" {
pagePath = filepath.Join(pagePath, d.ExpandedPermalink)
pagePath = filepath.Join(pagePath, d.URL)
}
if d.Addends != "" {
pagePath = filepath.Join(pagePath, d.Addends)
} else if strings.HasSuffix(d.URL, "/") || !strings.Contains(d.URL, ".") {
pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
}
} else if d.Kind == KindPage {
if d.ExpandedPermalink != "" {
pagePath = filepath.Join(pagePath, d.ExpandedPermalink)
} else {
if d.Dir != "" {
pagePath = filepath.Join(pagePath, d.Dir)
}
if d.BaseName != "" {
pagePath = filepath.Join(pagePath, d.BaseName)
}
} else {
if d.Dir != "" {
pagePath = filepath.Join(pagePath, d.Dir)
}
if d.BaseName != "" {
pagePath = filepath.Join(pagePath, d.BaseName)
}
}
if d.Addends != "" {
pagePath = filepath.Join(pagePath, d.Addends)
}
if d.Addends != "" {
pagePath = filepath.Join(pagePath, d.Addends)
}
if isUgly {
pagePath += d.Type.MediaType.Delimiter + d.Type.MediaType.Suffix
} else {
pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
}
if isUgly {
pagePath += d.Type.MediaType.Delimiter + d.Type.MediaType.Suffix
} else {
pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
}
if d.LangPrefix != "" {
pagePath = filepath.Join(d.LangPrefix, pagePath)
}
if d.LangPrefix != "" {
pagePath = filepath.Join(d.LangPrefix, pagePath)
}
} else {
if d.Addends != "" {