ref: a30023f5cbafd06034807255181a5b7b17f3c25f
parent: 1f26420d392a5ab4c7b7fe1911c0268b45d01ab8
author: Bjørn Erik Pedersen <[email protected]>
date: Tue Jun 13 07:26:17 EDT 2017
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
--- a/hugolib/site_sections.go
+++ b/hugolib/site_sections.go
@@ -154,13 +154,25 @@
sect = s.newSectionPage(p.sections[0])
sectionPages[sectionKey] = sect
newPages = append(newPages, sect)
- } else if !found {
- // We don't know what to do with this section yet.
- undecided = append(undecided, p)
+ found = true
}
- pagePath := path.Join(sectionKey, sectPageKey, strconv.Itoa(i))
- inPages.Insert([]byte(pagePath), p)
+ if len(p.sections) > 1 {
+ // Create the root section if not found.
+ _, rootFound := sectionPages[p.sections[0]]
+ if !rootFound {
+ sect = s.newSectionPage(p.sections[0])
+ sectionPages[p.sections[0]] = sect
+ newPages = append(newPages, sect)
+ }
+ }
+
+ if found {
+ pagePath := path.Join(sectionKey, sectPageKey, strconv.Itoa(i))
+ inPages.Insert([]byte(pagePath), p)
+ } else {
+ undecided = append(undecided, p)
+ }
}
// Create any missing sections in the tree.
@@ -181,17 +193,6 @@
}
}
- // Create any missing root sections.
- for _, p := range undecided {
- sectionKey := p.sections[0]
- sect, found := sectionPages[sectionKey]
- if !found {
- sect = s.newSectionPage(sectionKey)
- sectionPages[sectionKey] = sect
- newPages = append(newPages, sect)
- }
- }
-
for k, sect := range sectionPages {
inPages.Insert([]byte(path.Join(k, sectSectKey)), sect)
inSections.Insert([]byte(k), sect)
@@ -200,9 +201,19 @@
var (
currentSection *Page
children Pages
- rootPages = inPages.Commit().Root()
rootSections = inSections.Commit().Root()
)
+
+ for i, p := range undecided {
+ // Now we can decide where to put this page into the tree.
+ sectionKey := path.Join(p.sections...)
+ _, v, _ := rootSections.LongestPrefix([]byte(sectionKey))
+ sect := v.(*Page)
+ pagePath := path.Join(path.Join(sect.sections...), sectSectKey, "u", strconv.Itoa(i))
+ inPages.Insert([]byte(pagePath), p)
+ }
+
+ var rootPages = inPages.Commit().Root()
rootPages.Walk(func(path []byte, v interface{}) bool {
p := v.(*Page)
--- a/hugolib/site_sections_test.go
+++ b/hugolib/site_sections_test.go
@@ -58,6 +58,11 @@
fmt.Sprintf(pageTemplate, 1, level1))
}
+ // Issue #3586
+ writeSource(t, fs, filepath.Join("content", "post", "0000.md"), fmt.Sprintf(pageTemplate, 1, 2))
+ writeSource(t, fs, filepath.Join("content", "post", "0000", "0001.md"), fmt.Sprintf(pageTemplate, 1, 3))
+ writeSource(t, fs, filepath.Join("content", "elsewhere", "0003.md"), fmt.Sprintf(pageTemplate, 1, 4))
+
// Empty nested section, i.e. no regular content pages.
writeSource(t, fs, filepath.Join("content", "empty1", "b", "c", "_index.md"), fmt.Sprintf(pageTemplate, 33, -1))
// Index content file a the end and in the middle.
@@ -109,12 +114,24 @@
cfg.Set("paginate", 2)
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
- require.Len(t, s.RegularPages, 18)
+ require.Len(t, s.RegularPages, 21)
tests := []struct {
sections string
verify func(p *Page)
}{
+ {"elsewhere", func(p *Page) {
+ assert.Len(p.Pages, 1)
+ for _, p := range p.Pages {
+ assert.Equal([]string{"elsewhere"}, p.sections)
+ }
+ }},
+ {"post", func(p *Page) {
+ assert.Len(p.Pages, 2)
+ for _, p := range p.Pages {
+ assert.Equal("post", p.Section())
+ }
+ }},
{"empty1", func(p *Page) {
// > b,c
assert.NotNil(p.s.getPage(KindSection, "empty1", "b"))
@@ -228,7 +245,7 @@
assert.NotNil(home)
- assert.Len(home.Sections(), 7)
+ assert.Len(home.Sections(), 9)
rootPage := s.getPage(KindPage, "mypage.md")
assert.NotNil(rootPage)