diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-07-20 12:10:22 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-07-20 17:50:59 +0200 |
commit | d831d2fce8198fb814ea4d3d8c311db5c388d04c (patch) | |
tree | 6aef026a4941e625e4a23a72644bc70a7d98c292 /hugolib | |
parent | 634481ba8cfcd865ba0d8811d8834f6af45663d7 (diff) |
Simplify "active menu" logic for section menus
Fixes #8776
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/menu_test.go | 116 | ||||
-rw-r--r-- | hugolib/page__menus.go | 1 | ||||
-rw-r--r-- | hugolib/site.go | 4 |
3 files changed, 115 insertions, 6 deletions
diff --git a/hugolib/menu_test.go b/hugolib/menu_test.go index c43878090..a647c5bfa 100644 --- a/hugolib/menu_test.go +++ b/hugolib/menu_test.go @@ -33,7 +33,7 @@ menu: ` ) -func TestSectionPagesMenu(t *testing.T) { +func TestMenusSectionPagesMenu(t *testing.T) { t.Parallel() siteConfig := ` @@ -106,7 +106,7 @@ Menu Main: {{ partial "menu.html" (dict "page" . "menu" "main") }}`, } // related issue #7594 -func TestMenuSort(t *testing.T) { +func TestMenusSort(t *testing.T) { b := newTestSitesBuilder(t).WithSimpleConfigFile() b.WithTemplatesAdded("index.html", ` @@ -193,7 +193,7 @@ menu: ) } -func TestMenuFrontMatter(t *testing.T) { +func TestMenusFrontMatter(t *testing.T) { b := newTestSitesBuilder(t).WithSimpleConfigFile() b.WithTemplatesAdded("index.html", ` @@ -243,7 +243,7 @@ menu: } // https://github.com/gohugoio/hugo/issues/5849 -func TestMenuPageMultipleOutputFormats(t *testing.T) { +func TestMenusPageMultipleOutputFormats(t *testing.T) { config := ` baseURL = "https://example.com" @@ -301,7 +301,7 @@ menu: "main" } // https://github.com/gohugoio/hugo/issues/5989 -func TestMenuPageSortByDate(t *testing.T) { +func TestMenusPageSortByDate(t *testing.T) { b := newTestSitesBuilder(t).WithSimpleConfigFile() b.WithContent("blog/a.md", ` @@ -399,3 +399,109 @@ key2: key2_config camelCase: camelCase_config `) } + +func TestMenusShadowMembers(t *testing.T) { + b := newTestSitesBuilder(t).WithConfigFile("toml", ` +[[menus.main]] +identifier = "contact" +pageRef = "contact" +title = "Contact Us" +url = "mailto:noreply@example.com" +weight = 1 +[[menus.main]] +pageRef = "/blog/post3" +title = "My Post 3" +url = "/blog/post3" + +`) + + commonTempl := ` +Main: {{ len .Site.Menus.main }} +{{ range .Site.Menus.main }} +{{ .Title }}|HasMenuCurrent: {{ $.HasMenuCurrent "main" . }}|Page: {{ .Page }} +{{ .Title }}|IsMenuCurrent: {{ $.IsMenuCurrent "main" . }}|Page: {{ .Page }} +{{ end }} +` + + b.WithTemplatesAdded("index.html", commonTempl) + b.WithTemplatesAdded("_default/single.html", commonTempl) + + b.WithContent("_index.md", ` +--- +title: "Home" +menu: + main: + weight: 10 +--- +`) + + b.WithContent("blog/_index.md", ` +--- +title: "Blog" +menu: + main: + weight: 20 +--- +`) + + b.WithContent("blog/post1.md", ` +--- +title: "My Post 1: With No Menu Defined" +--- +`) + + b.WithContent("blog/post2.md", ` +--- +title: "My Post 2: With Menu Defined" +menu: + main: + weight: 30 +--- +`) + + b.WithContent("blog/post3.md", ` +--- +title: "My Post 2: With No Menu Defined" +--- +`) + + b.WithContent("contact.md", ` +--- +title: "Contact: With No Menu Defined" +--- +`) + + b.Build(BuildCfg{}) + + b.AssertFileContent("public/index.html", ` +Main: 5 +Home|HasMenuCurrent: false|Page: Page(/_index.md) +Blog|HasMenuCurrent: false|Page: Page(/blog/_index.md) +My Post 2: With Menu Defined|HasMenuCurrent: false|Page: Page(/blog/post2.md) +My Post 3|HasMenuCurrent: false|Page: Page(/blog/post3.md) +Contact Us|HasMenuCurrent: false|Page: Page(/contact.md) +`) + + b.AssertFileContent("public/blog/post1/index.html", ` +Home|HasMenuCurrent: false|Page: Page(/_index.md) +Blog|HasMenuCurrent: true|Page: Page(/blog/_index.md) +`) + + b.AssertFileContent("public/blog/post2/index.html", ` +Home|HasMenuCurrent: false|Page: Page(/_index.md) +Blog|HasMenuCurrent: true|Page: Page(/blog/_index.md) +Blog|IsMenuCurrent: false|Page: Page(/blog/_index.md) +`) + + b.AssertFileContent("public/blog/post3/index.html", ` +Home|HasMenuCurrent: false|Page: Page(/_index.md) +Blog|HasMenuCurrent: true|Page: Page(/blog/_index.md) +`) + + b.AssertFileContent("public/contact/index.html", ` +Contact Us|HasMenuCurrent: false|Page: Page(/contact.md) +Contact Us|IsMenuCurrent: true|Page: Page(/contact.md) +Blog|HasMenuCurrent: false|Page: Page(/blog/_index.md) +Blog|IsMenuCurrent: false|Page: Page(/blog/_index.md) +`) +} diff --git a/hugolib/page__menus.go b/hugolib/page__menus.go index e64ffa2c9..49d392c2f 100644 --- a/hugolib/page__menus.go +++ b/hugolib/page__menus.go @@ -56,7 +56,6 @@ func (p *pageMenus) menus() navigation.PageMenus { func (p *pageMenus) init() { p.pmInit.Do(func() { p.q = navigation.NewMenuQueryProvider( - p.p.s.Info.sectionPagesMenu, p, p.p.s, p.p, diff --git a/hugolib/site.go b/hugolib/site.go index fe7305b91..e687710bf 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -1452,6 +1452,10 @@ func (s *Site) assembleMenus() { menuConfig := s.getMenusFromConfig() for name, menu := range menuConfig { for _, me := range menu { + if types.IsNil(me.Page) && me.PageRef != "" { + // Try to resolve the page. + me.Page, _ = s.getPageNew(nil, me.PageRef) + } flat[twoD{name, me.KeyName()}] = me } } |