Skip to content

Commit 57e10f1

Browse files
committed
Support uglyURLs per section
Fixes #4256
1 parent db85e83 commit 57e10f1

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

‎hugolib/page_paths.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (p *Page) initTargetPathDescriptor() error {
8686
PathSpec: p.s.PathSpec,
8787
Kind: p.Kind,
8888
Sections: p.sections,
89-
UglyURLs: p.s.Info.uglyURLs,
89+
UglyURLs: p.s.Info.uglyURLs(p),
9090
Dir: filepath.ToSlash(p.Source.Dir()),
9191
URL: p.URLPath.URL,
9292
IsMultihost: p.s.owner.IsMultihost(),

‎hugolib/site.go‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ type SiteInfo struct {
357357
BuildDrafts bool
358358
canonifyURLs bool
359359
relativeURLs bool
360-
uglyURLs bool
360+
uglyURLs func(p *Page) bool
361361
preserveTaxonomyNames bool
362362
Data *map[string]interface{}
363363

@@ -413,6 +413,9 @@ func newSiteInfo(cfg siteBuilderCfg) SiteInfo {
413413
multilingual: newMultiLingualForLanguage(cfg.language),
414414
PageCollections: cfg.pageCollections,
415415
Params: make(map[string]interface{}),
416+
uglyURLs: func(p *Page) bool {
417+
return false
418+
},
416419
}
417420
}
418421

@@ -1035,6 +1038,24 @@ func (s *Site) initializeSiteInfo() {
10351038
multilingual = s.owner.multilingual
10361039
}
10371040

1041+
var uglyURLs = func(p *Page) bool {
1042+
return false
1043+
}
1044+
1045+
v := s.Cfg.Get("uglyURLs")
1046+
if v != nil {
1047+
if vv, ok := v.(bool); ok {
1048+
uglyURLs = func(p *Page) bool {
1049+
return vv
1050+
}
1051+
} else {
1052+
m := cast.ToStringMapBool(v)
1053+
uglyURLs = func(p *Page) bool {
1054+
return m[p.Section()]
1055+
}
1056+
}
1057+
}
1058+
10381059
s.Info = SiteInfo{
10391060
Title: lang.GetString("title"),
10401061
Author: lang.GetStringMap("author"),
@@ -1052,7 +1073,7 @@ func (s *Site) initializeSiteInfo() {
10521073
BuildDrafts: s.Cfg.GetBool("buildDrafts"),
10531074
canonifyURLs: s.Cfg.GetBool("canonifyURLs"),
10541075
relativeURLs: s.Cfg.GetBool("relativeURLs"),
1055-
uglyURLs: s.Cfg.GetBool("uglyURLs"),
1076+
uglyURLs: uglyURLs,
10561077
preserveTaxonomyNames: lang.GetBool("preserveTaxonomyNames"),
10571078
PageCollections: s.PageCollections,
10581079
Menus: &s.Menus,

‎hugolib/site_url_test.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,40 @@ func TestPageCount(t *testing.T) {
8787
}
8888
}
8989
}
90+
91+
func TestUglyURLsPerSection(t *testing.T) {
92+
t.Parallel()
93+
94+
assert := require.New(t)
95+
96+
const dt = `---
97+
title: Do not go gentle into that good night
98+
---
99+
100+
Wild men who caught and sang the sun in flight,
101+
And learn, too late, they grieved it on its way,
102+
Do not go gentle into that good night.
103+
104+
`
105+
106+
cfg, fs := newTestCfg()
107+
108+
cfg.Set("uglyURLs", map[string]bool{
109+
"sect2": true,
110+
})
111+
112+
writeSource(t, fs, filepath.Join("content", "sect1", "p1.md"), dt)
113+
writeSource(t, fs, filepath.Join("content", "sect2", "p2.md"), dt)
114+
115+
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})
116+
117+
assert.Len(s.RegularPages, 2)
118+
119+
notUgly := s.getPage(KindPage, "sect1/p1.md")
120+
assert.NotNil(notUgly)
121+
assert.Equal("/sect1/p1/", notUgly.RelPermalink())
122+
123+
ugly := s.getPage(KindPage, "sect2/p2.md")
124+
assert.NotNil(ugly)
125+
assert.Equal("/sect2/p2.html", ugly.RelPermalink())
126+
}

0 commit comments

Comments
 (0)