Skip to content

Commit 245928a

Browse files
committed
content adapter: Add support for menus in AddPage
Fixes gohugoio#12507
1 parent 519f41d commit 245928a

File tree

5 files changed

+50
-16
lines changed

5 files changed

+50
-16
lines changed

hugolib/page__menus.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,22 @@ func (p *pageMenus) init() {
6262
p.p,
6363
)
6464

65+
params := p.p.Params()
66+
67+
var menus any
68+
var ok bool
69+
70+
if p.p.m.pageConfig.Menus != nil {
71+
menus = p.p.m.pageConfig.Menus
72+
} else {
73+
menus, ok = params["menus"]
74+
if !ok {
75+
menus = params["menu"]
76+
}
77+
}
78+
6579
var err error
66-
p.pm, err = navigation.PageMenusFromPage(p.p)
80+
p.pm, err = navigation.PageMenusFromPage(menus, p.p)
6781
if err != nil {
6882
p.p.s.Log.Errorln(p.p.wrapError(err))
6983
}

hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,3 +610,31 @@ foo
610610
b.AssertFileContent("public/a/index.html", "|xfoo|")
611611
b.AssertFileContent("public/b/index.html", "|foo|") // fails
612612
}
613+
614+
func TestPagesFromGoTmplMenus(t *testing.T) {
615+
t.Parallel()
616+
617+
files := `
618+
-- hugo.toml --
619+
disableKinds = ['rss','section','sitemap','taxonomy','term']
620+
621+
[menus]
622+
[[menus.main]]
623+
name = "Main"
624+
[[menus.footer]]
625+
name = "Footer"
626+
-- content/_content.gotmpl --
627+
{{ .AddPage (dict "path" "p1" "title" "p1" "menus" "main" ) }}
628+
{{ .AddPage (dict "path" "p2" "title" "p2" "menus" (slice "main" "footer")) }}
629+
-- layouts/index.html --
630+
Main: {{ range index site.Menus.main }}{{ .Name }}|{{ end }}|
631+
Footer: {{ range index site.Menus.footer }}{{ .Name }}|{{ end }}|
632+
633+
`
634+
b := hugolib.Test(t, files)
635+
636+
b.AssertFileContent("public/index.html",
637+
"Main: Main|p1|p2||",
638+
"Footer: Footer|p2||",
639+
)
640+
}

magefile.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,19 @@ func testGoFlags() string {
167167
// Note that we don't run with the extended tag. Currently not supported in 32 bit.
168168
func Test386() error {
169169
env := map[string]string{"GOARCH": "386", "GOFLAGS": testGoFlags()}
170-
return runCmd(env, goexe, "test", "./...")
170+
return runCmd(env, goexe, "test", "-p", "2", "./...")
171171
}
172172

173173
// Run tests
174174
func Test() error {
175175
env := map[string]string{"GOFLAGS": testGoFlags()}
176-
return runCmd(env, goexe, "test", "./...", "-tags", buildTags())
176+
return runCmd(env, goexe, "test", "-p", "2", "./...", "-tags", buildTags())
177177
}
178178

179179
// Run tests with race detector
180180
func TestRace() error {
181181
env := map[string]string{"GOFLAGS": testGoFlags()}
182-
return runCmd(env, goexe, "test", "-race", "./...", "-tags", buildTags())
182+
return runCmd(env, goexe, "test", "-p", "2", "-race", "./...", "-tags", buildTags())
183183
}
184184

185185
// Run gofmt linter

navigation/pagemenus.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,11 @@ type MenuQueryProvider interface {
4141
IsMenuCurrent(menuID string, inme *MenuEntry) bool
4242
}
4343

44-
func PageMenusFromPage(p Page) (PageMenus, error) {
45-
params := p.Params()
46-
47-
ms, ok := params["menus"]
48-
if !ok {
49-
ms, ok = params["menu"]
50-
}
51-
52-
pm := PageMenus{}
53-
54-
if !ok {
44+
func PageMenusFromPage(ms any, p Page) (PageMenus, error) {
45+
if ms == nil {
5546
return nil, nil
5647
}
57-
48+
pm := PageMenus{}
5849
me := MenuEntry{}
5950
SetPageValues(&me, p)
6051

resources/page/pagemeta/page_frontmatter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ type PageConfig struct {
101101
Cascade []map[string]any
102102
Sitemap config.SitemapConfig
103103
Build BuildConfig
104+
Menus []string
104105

105106
// User defined params.
106107
Params maps.Params

0 commit comments

Comments
 (0)