From 924028a9be4e468b38f3b7a530253c7c09a0d10e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 10 Mar 2016 10:31:12 +0100 Subject: [PATCH] Fix for page in multiple menus Fixes #1934 --- hugolib/menu_test.go | 32 ++++++++++++++++++++++++++++++++ hugolib/page.go | 15 ++++++++------- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/hugolib/menu_test.go b/hugolib/menu_test.go index dff0cd699..dbfc27ad1 100644 --- a/hugolib/menu_test.go +++ b/hugolib/menu_test.go @@ -344,6 +344,38 @@ func TestMenuURL(t *testing.T) { } +// Issue #1934 +func TestYAMLMenuWithMultipleEntries(t *testing.T) { + viper.Reset() + defer viper.Reset() + + ps1 := []byte(`--- +title: "Yaml 1" +weight: 5 +menu: ["p_one", "p_two"] +--- +Yaml Front Matter with Menu Pages`) + + ps2 := []byte(`--- +title: "Yaml 2" +weight: 5 +menu: + p_three: + p_four: +--- +Yaml Front Matter with Menu Pages`) + + s := setupMenuTests(t, []source.ByteSource{ + {filepath.FromSlash("sect/yaml1.md"), ps1}, + {filepath.FromSlash("sect/yaml2.md"), ps2}}) + + p1 := s.Pages[0] + assert.Len(t, p1.Menus(), 2, "List YAML") + p2 := s.Pages[1] + assert.Len(t, p2.Menus(), 2, "Map YAML") + +} + // issue #719 func TestMenuWithUnicodeURLs(t *testing.T) { diff --git a/hugolib/page.go b/hugolib/page.go index 407b7c6db..97f696f0b 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -759,8 +759,8 @@ func (p *Page) Menus() PageMenus { for _, mname := range mnames { me.Menu = mname p.pageMenus[mname] = &me - return } + return } // Could be a structured menu entry @@ -772,14 +772,15 @@ func (p *Page) Menus() PageMenus { for name, menu := range menus { menuEntry := MenuEntry{Name: p.LinkTitle(), URL: link, Weight: p.Weight, Menu: name} - jww.DEBUG.Printf("found menu: %q, in %q\n", name, p.Title) + if menu != nil { + jww.DEBUG.Printf("found menu: %q, in %q\n", name, p.Title) + ime, err := cast.ToStringMapE(menu) + if err != nil { + jww.ERROR.Printf("unable to process menus for %q: %s", p.Title, err) + } - ime, err := cast.ToStringMapE(menu) - if err != nil { - jww.ERROR.Printf("unable to process menus for %q\n", p.Title) + menuEntry.MarshallMap(ime) } - - menuEntry.MarshallMap(ime) p.pageMenus[name] = &menuEntry } }