Skip to content

Commit 8a40989

Browse files
committed
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
1 parent 46db900 commit 8a40989

File tree

3 files changed

+50
-47
lines changed

3 files changed

+50
-47
lines changed

‎hugolib/node_as_page_test.go‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ func TestNodesWithURLs(t *testing.T) {
591591

592592
writeSource(t, fs, filepath.Join("content", "sect", "_index.md"), `---
593593
title: MySection
594-
url: foo.html
594+
url: /my-section/
595595
---
596596
My Section Content
597597
`)
@@ -602,19 +602,20 @@ My Section Content
602602

603603
require.NoError(t, h.Build(BuildCfg{}))
604604

605-
th.assertFileContent(filepath.Join("public", "sect", "index.html"), "My Section")
605+
th.assertFileContent(filepath.Join("public", "my-section", "index.html"), "My Section")
606+
th.assertFileContent(filepath.Join("public", "my-section", "page", "1", "index.html"), `content="0; url=http://bep.is/base/my-section/"`)
606607

607608
s := h.Sites[0]
608609

609610
p := s.RegularPages[0]
610611

611612
require.Equal(t, "/base/sect1/regular1/", p.URL())
612613

613-
// Section with front matter and url set (which should not be used)
614+
// Section with front matter and url set
614615
sect := s.getPage(KindSection, "sect")
615-
require.Equal(t, "/base/sect/", sect.URL())
616-
require.Equal(t, "http://bep.is/base/sect/", sect.Permalink())
617-
require.Equal(t, "/base/sect/", sect.RelPermalink())
616+
require.Equal(t, "/base/my-section/", sect.URL())
617+
require.Equal(t, "http://bep.is/base/my-section/", sect.Permalink())
618+
require.Equal(t, "/base/my-section/", sect.RelPermalink())
618619

619620
// Home page without front matter
620621
require.Equal(t, "/base/", s.getPage(KindHome).URL())

‎hugolib/page.go‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ type Page struct {
223223
Lastmod time.Time
224224

225225
Sitemap Sitemap
226-
227226
URLPath
228227
permalink string
229228
relPermalink string
@@ -1111,6 +1110,7 @@ func (p *Page) update(f interface{}) error {
11111110
return fmt.Errorf("Only relative URLs are supported, %v provided", url)
11121111
}
11131112
p.URLPath.URL = cast.ToString(v)
1113+
p.URLPath.frontMatterURL = p.URLPath.URL
11141114
p.Params[loki] = p.URLPath.URL
11151115
case "type":
11161116
p.contentType = cast.ToString(v)
@@ -1809,10 +1809,11 @@ func (p *Page) String() string {
18091809
}
18101810

18111811
type URLPath struct {
1812-
URL string
1813-
Permalink string
1814-
Slug string
1815-
Section string
1812+
URL string
1813+
frontMatterURL string
1814+
Permalink string
1815+
Slug string
1816+
Section string
18161817
}
18171818

18181819
// Scratch returns the writable context associated with this Page.
@@ -1991,7 +1992,9 @@ func (p *Page) setValuesForKind(s *Site) {
19911992
p.URLPath.URL = "/"
19921993
case KindPage:
19931994
default:
1994-
p.URLPath.URL = "/" + path.Join(p.sections...) + "/"
1995+
if p.URLPath.URL == "" {
1996+
p.URLPath.URL = "/" + path.Join(p.sections...) + "/"
1997+
}
19951998
}
19961999
}
19972000

‎hugolib/page_paths.go‎

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type targetPathDescriptor struct {
5656
// Whether this is a multihost multilingual setup.
5757
IsMultihost bool
5858

59-
// Page.URLPath.URL. Will override any Slug etc. for regular pages.
59+
// URL from front matter if set. Will override any Slug etc.
6060
URL string
6161

6262
// Used to create paginator links.
@@ -88,7 +88,7 @@ func (p *Page) initTargetPathDescriptor() error {
8888
Sections: p.sections,
8989
UglyURLs: p.s.Info.uglyURLs(p),
9090
Dir: filepath.ToSlash(p.Source.Dir()),
91-
URL: p.URLPath.URL,
91+
URL: p.URLPath.frontMatterURL,
9292
IsMultihost: p.s.owner.IsMultihost(),
9393
}
9494

@@ -189,7 +189,7 @@ func createTargetPath(d targetPathDescriptor) string {
189189
isUgly = true
190190
}
191191

192-
if d.Kind != KindPage && len(d.Sections) > 0 {
192+
if d.Kind != KindPage && d.URL == "" && len(d.Sections) > 0 {
193193
if d.ExpandedPermalink != "" {
194194
pagePath = filepath.Join(pagePath, d.ExpandedPermalink)
195195
} else {
@@ -202,43 +202,42 @@ func createTargetPath(d targetPathDescriptor) string {
202202
pagePath = filepath.Join(pagePath, d.Type.Path)
203203
}
204204

205-
if d.Kind == KindPage {
206-
// Always use URL if it's specified
207-
if d.URL != "" {
208-
if d.IsMultihost && d.LangPrefix != "" && !strings.HasPrefix(d.URL, "/"+d.LangPrefix) {
209-
pagePath = filepath.Join(d.LangPrefix, pagePath, d.URL)
210-
} else {
211-
pagePath = filepath.Join(pagePath, d.URL)
212-
}
213-
if strings.HasSuffix(d.URL, "/") || !strings.Contains(d.URL, ".") {
214-
pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
215-
}
205+
if d.Kind != KindHome && d.URL != "" {
206+
if d.IsMultihost && d.LangPrefix != "" && !strings.HasPrefix(d.URL, "/"+d.LangPrefix) {
207+
pagePath = filepath.Join(d.LangPrefix, pagePath, d.URL)
216208
} else {
217-
if d.ExpandedPermalink != "" {
218-
pagePath = filepath.Join(pagePath, d.ExpandedPermalink)
219-
220-
} else {
221-
if d.Dir != "" {
222-
pagePath = filepath.Join(pagePath, d.Dir)
223-
}
224-
if d.BaseName != "" {
225-
pagePath = filepath.Join(pagePath, d.BaseName)
226-
}
227-
}
209+
pagePath = filepath.Join(pagePath, d.URL)
210+
}
211+
if d.Addends != "" {
212+
pagePath = filepath.Join(pagePath, d.Addends)
213+
} else if strings.HasSuffix(d.URL, "/") || !strings.Contains(d.URL, ".") {
214+
pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
215+
}
216+
} else if d.Kind == KindPage {
217+
if d.ExpandedPermalink != "" {
218+
pagePath = filepath.Join(pagePath, d.ExpandedPermalink)
228219

229-
if d.Addends != "" {
230-
pagePath = filepath.Join(pagePath, d.Addends)
220+
} else {
221+
if d.Dir != "" {
222+
pagePath = filepath.Join(pagePath, d.Dir)
231223
}
232-
233-
if isUgly {
234-
pagePath += d.Type.MediaType.Delimiter + d.Type.MediaType.Suffix
235-
} else {
236-
pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
224+
if d.BaseName != "" {
225+
pagePath = filepath.Join(pagePath, d.BaseName)
237226
}
227+
}
238228

239-
if d.LangPrefix != "" {
240-
pagePath = filepath.Join(d.LangPrefix, pagePath)
241-
}
229+
if d.Addends != "" {
230+
pagePath = filepath.Join(pagePath, d.Addends)
231+
}
232+
233+
if isUgly {
234+
pagePath += d.Type.MediaType.Delimiter + d.Type.MediaType.Suffix
235+
} else {
236+
pagePath = filepath.Join(pagePath, d.Type.BaseName+d.Type.MediaType.FullSuffix())
237+
}
238+
239+
if d.LangPrefix != "" {
240+
pagePath = filepath.Join(d.LangPrefix, pagePath)
242241
}
243242
} else {
244243
if d.Addends != "" {

0 commit comments

Comments
 (0)