Skip to content

Commit a30023f

Browse files
committed
hugolib: Fix section logic for root folders with subfolders
This commit fixes an issue introduced in the recently released Hugo 0.22. This logic did not handle the case with root sections with non-section subfolders very well. Fixes #3586
1 parent 1f26420 commit a30023f

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

‎hugolib/site_sections.go‎

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,25 @@ func (s *Site) assembleSections() Pages {
154154
sect = s.newSectionPage(p.sections[0])
155155
sectionPages[sectionKey] = sect
156156
newPages = append(newPages, sect)
157-
} else if !found {
158-
// We don't know what to do with this section yet.
159-
undecided = append(undecided, p)
157+
found = true
160158
}
161159

162-
pagePath := path.Join(sectionKey, sectPageKey, strconv.Itoa(i))
163-
inPages.Insert([]byte(pagePath), p)
160+
if len(p.sections) > 1 {
161+
// Create the root section if not found.
162+
_, rootFound := sectionPages[p.sections[0]]
163+
if !rootFound {
164+
sect = s.newSectionPage(p.sections[0])
165+
sectionPages[p.sections[0]] = sect
166+
newPages = append(newPages, sect)
167+
}
168+
}
169+
170+
if found {
171+
pagePath := path.Join(sectionKey, sectPageKey, strconv.Itoa(i))
172+
inPages.Insert([]byte(pagePath), p)
173+
} else {
174+
undecided = append(undecided, p)
175+
}
164176
}
165177

166178
// Create any missing sections in the tree.
@@ -181,17 +193,6 @@ func (s *Site) assembleSections() Pages {
181193
}
182194
}
183195

184-
// Create any missing root sections.
185-
for _, p := range undecided {
186-
sectionKey := p.sections[0]
187-
sect, found := sectionPages[sectionKey]
188-
if !found {
189-
sect = s.newSectionPage(sectionKey)
190-
sectionPages[sectionKey] = sect
191-
newPages = append(newPages, sect)
192-
}
193-
}
194-
195196
for k, sect := range sectionPages {
196197
inPages.Insert([]byte(path.Join(k, sectSectKey)), sect)
197198
inSections.Insert([]byte(k), sect)
@@ -200,10 +201,20 @@ func (s *Site) assembleSections() Pages {
200201
var (
201202
currentSection *Page
202203
children Pages
203-
rootPages = inPages.Commit().Root()
204204
rootSections = inSections.Commit().Root()
205205
)
206206

207+
for i, p := range undecided {
208+
// Now we can decide where to put this page into the tree.
209+
sectionKey := path.Join(p.sections...)
210+
_, v, _ := rootSections.LongestPrefix([]byte(sectionKey))
211+
sect := v.(*Page)
212+
pagePath := path.Join(path.Join(sect.sections...), sectSectKey, "u", strconv.Itoa(i))
213+
inPages.Insert([]byte(pagePath), p)
214+
}
215+
216+
var rootPages = inPages.Commit().Root()
217+
207218
rootPages.Walk(func(path []byte, v interface{}) bool {
208219
p := v.(*Page)
209220

‎hugolib/site_sections_test.go‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ Content
5858
fmt.Sprintf(pageTemplate, 1, level1))
5959
}
6060

61+
// Issue #3586
62+
writeSource(t, fs, filepath.Join("content", "post", "0000.md"), fmt.Sprintf(pageTemplate, 1, 2))
63+
writeSource(t, fs, filepath.Join("content", "post", "0000", "0001.md"), fmt.Sprintf(pageTemplate, 1, 3))
64+
writeSource(t, fs, filepath.Join("content", "elsewhere", "0003.md"), fmt.Sprintf(pageTemplate, 1, 4))
65+
6166
// Empty nested section, i.e. no regular content pages.
6267
writeSource(t, fs, filepath.Join("content", "empty1", "b", "c", "_index.md"), fmt.Sprintf(pageTemplate, 33, -1))
6368
// Index content file a the end and in the middle.
@@ -109,12 +114,24 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
109114
cfg.Set("paginate", 2)
110115

111116
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
112-
require.Len(t, s.RegularPages, 18)
117+
require.Len(t, s.RegularPages, 21)
113118

114119
tests := []struct {
115120
sections string
116121
verify func(p *Page)
117122
}{
123+
{"elsewhere", func(p *Page) {
124+
assert.Len(p.Pages, 1)
125+
for _, p := range p.Pages {
126+
assert.Equal([]string{"elsewhere"}, p.sections)
127+
}
128+
}},
129+
{"post", func(p *Page) {
130+
assert.Len(p.Pages, 2)
131+
for _, p := range p.Pages {
132+
assert.Equal("post", p.Section())
133+
}
134+
}},
118135
{"empty1", func(p *Page) {
119136
// > b,c
120137
assert.NotNil(p.s.getPage(KindSection, "empty1", "b"))
@@ -228,7 +245,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
228245

229246
assert.NotNil(home)
230247

231-
assert.Len(home.Sections(), 7)
248+
assert.Len(home.Sections(), 9)
232249

233250
rootPage := s.getPage(KindPage, "mypage.md")
234251
assert.NotNil(rootPage)

0 commit comments

Comments
 (0)