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"), `--- writeSource(t, fs, filepath.Join("content", "sect", "_index.md"), `---
title: MySection title: MySection
url: foo.html url: /my-section/
--- ---
My Section Content My Section Content
`) `)
@ -602,7 +602,8 @@ My Section Content
require.NoError(t, h.Build(BuildCfg{})) 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] s := h.Sites[0]
@ -610,11 +611,11 @@ My Section Content
require.Equal(t, "/base/sect1/regular1/", p.URL()) 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") sect := s.getPage(KindSection, "sect")
require.Equal(t, "/base/sect/", sect.URL()) require.Equal(t, "/base/my-section/", sect.URL())
require.Equal(t, "http://bep.is/base/sect/", sect.Permalink()) require.Equal(t, "http://bep.is/base/my-section/", sect.Permalink())
require.Equal(t, "/base/sect/", sect.RelPermalink()) require.Equal(t, "/base/my-section/", sect.RelPermalink())
// Home page without front matter // Home page without front matter
require.Equal(t, "/base/", s.getPage(KindHome).URL()) require.Equal(t, "/base/", s.getPage(KindHome).URL())

View file

@ -223,7 +223,6 @@ type Page struct {
Lastmod time.Time Lastmod time.Time
Sitemap Sitemap Sitemap Sitemap
URLPath URLPath
permalink string permalink string
relPermalink 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) return fmt.Errorf("Only relative URLs are supported, %v provided", url)
} }
p.URLPath.URL = cast.ToString(v) p.URLPath.URL = cast.ToString(v)
p.URLPath.frontMatterURL = p.URLPath.URL
p.Params[loki] = p.URLPath.URL p.Params[loki] = p.URLPath.URL
case "type": case "type":
p.contentType = cast.ToString(v) p.contentType = cast.ToString(v)
@ -1809,10 +1809,11 @@ func (p *Page) String() string {
} }
type URLPath struct { type URLPath struct {
URL string URL string
Permalink string frontMatterURL string
Slug string Permalink string
Section string Slug string
Section string
} }
// Scratch returns the writable context associated with this Page. // Scratch returns the writable context associated with this Page.
@ -1991,7 +1992,9 @@ func (p *Page) setValuesForKind(s *Site) {
p.URLPath.URL = "/" p.URLPath.URL = "/"
case KindPage: case KindPage:
default: 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. // Whether this is a multihost multilingual setup.
IsMultihost bool 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 URL string
// Used to create paginator links. // Used to create paginator links.
@ -88,7 +88,7 @@ func (p *Page) initTargetPathDescriptor() error {
Sections: p.sections, Sections: p.sections,
UglyURLs: p.s.Info.uglyURLs(p), UglyURLs: p.s.Info.uglyURLs(p),
Dir: filepath.ToSlash(p.Source.Dir()), Dir: filepath.ToSlash(p.Source.Dir()),
URL: p.URLPath.URL, URL: p.URLPath.frontMatterURL,
IsMultihost: p.s.owner.IsMultihost(), IsMultihost: p.s.owner.IsMultihost(),
} }
@ -189,7 +189,7 @@ func createTargetPath(d targetPathDescriptor) string {
isUgly = true isUgly = true
} }
if d.Kind != KindPage && len(d.Sections) > 0 { if d.Kind != KindPage && d.URL == "" && len(d.Sections) > 0 {
if d.ExpandedPermalink != "" { if d.ExpandedPermalink != "" {
pagePath = filepath.Join(pagePath, d.ExpandedPermalink) pagePath = filepath.Join(pagePath, d.ExpandedPermalink)
} else { } else {
@ -202,43 +202,42 @@ func createTargetPath(d targetPathDescriptor) string {
pagePath = filepath.Join(pagePath, d.Type.Path) pagePath = filepath.Join(pagePath, d.Type.Path)
} }
if d.Kind == KindPage { if d.Kind != KindHome && d.URL != "" {
// Always use URL if it's specified if d.IsMultihost && d.LangPrefix != "" && !strings.HasPrefix(d.URL, "/"+d.LangPrefix) {
if d.URL != "" { pagePath = filepath.Join(d.LangPrefix, pagePath, 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())
}
} else { } else {
if d.ExpandedPermalink != "" { pagePath = filepath.Join(pagePath, d.URL)
pagePath = filepath.Join(pagePath, d.ExpandedPermalink) }
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 { } else {
if d.Dir != "" { if d.Dir != "" {
pagePath = filepath.Join(pagePath, d.Dir) pagePath = filepath.Join(pagePath, d.Dir)
}
if d.BaseName != "" {
pagePath = filepath.Join(pagePath, d.BaseName)
}
} }
if d.BaseName != "" {
pagePath = filepath.Join(pagePath, d.BaseName)
}
}
if d.Addends != "" { if d.Addends != "" {
pagePath = filepath.Join(pagePath, d.Addends) pagePath = filepath.Join(pagePath, d.Addends)
} }
if isUgly { if isUgly {
pagePath += d.Type.MediaType.Delimiter + d.Type.MediaType.Suffix pagePath += d.Type.MediaType.Delimiter + d.Type.MediaType.Suffix
} else { } else {
pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix()) pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
} }
if d.LangPrefix != "" { if d.LangPrefix != "" {
pagePath = filepath.Join(d.LangPrefix, pagePath) pagePath = filepath.Join(d.LangPrefix, pagePath)
}
} }
} else { } else {
if d.Addends != "" { if d.Addends != "" {