Permalink
Browse files

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
  • Loading branch information...
bep committed Jan 12, 2018
1 parent 46db900 commit 8a409894bdb0972e152a2eccc47a2738568e1cfc
Showing with 50 additions and 47 deletions.
  1. +7 −6 hugolib/node_as_page_test.go
  2. +9 −6 hugolib/page.go
  3. +34 −35 hugolib/page_paths.go
@@ -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,19 +602,20 @@ 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]
p := s.RegularPages[0]
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
@@ -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
@@ -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)
} else {
if d.Dir != "" {
pagePath = filepath.Join(pagePath, d.Dir)
}
if d.BaseName != "" {
pagePath = filepath.Join(pagePath, d.BaseName)
}
}
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)
if d.Addends != "" {
pagePath = filepath.Join(pagePath, d.Addends)
} else {
if d.Dir != "" {
pagePath = filepath.Join(pagePath, d.Dir)
}
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.BaseName != "" {
pagePath = filepath.Join(pagePath, d.BaseName)
}
}
if d.LangPrefix != "" {
pagePath = filepath.Join(d.LangPrefix, pagePath)
}
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 d.LangPrefix != "" {
pagePath = filepath.Join(d.LangPrefix, pagePath)
}
} else {
if d.Addends != "" {

0 comments on commit 8a40989

Please sign in to comment.