Skip to content

Commit

Permalink
content adapter: Add support for menus in AddPage
Browse files Browse the repository at this point in the history
Fixes #12507
  • Loading branch information
bep committed May 30, 2024
1 parent 519f41d commit 245928a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
16 changes: 15 additions & 1 deletion hugolib/page__menus.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,22 @@ func (p *pageMenus) init() {
p.p,
)

params := p.p.Params()

var menus any
var ok bool

if p.p.m.pageConfig.Menus != nil {
menus = p.p.m.pageConfig.Menus
} else {
menus, ok = params["menus"]
if !ok {
menus = params["menu"]
}
}

var err error
p.pm, err = navigation.PageMenusFromPage(p.p)
p.pm, err = navigation.PageMenusFromPage(menus, p.p)
if err != nil {
p.p.s.Log.Errorln(p.p.wrapError(err))
}
Expand Down
28 changes: 28 additions & 0 deletions hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,31 @@ foo
b.AssertFileContent("public/a/index.html", "|xfoo|")
b.AssertFileContent("public/b/index.html", "|foo|") // fails
}

func TestPagesFromGoTmplMenus(t *testing.T) {
t.Parallel()

files := `
-- hugo.toml --
disableKinds = ['rss','section','sitemap','taxonomy','term']
[menus]
[[menus.main]]
name = "Main"
[[menus.footer]]
name = "Footer"
-- content/_content.gotmpl --
{{ .AddPage (dict "path" "p1" "title" "p1" "menus" "main" ) }}
{{ .AddPage (dict "path" "p2" "title" "p2" "menus" (slice "main" "footer")) }}
-- layouts/index.html --
Main: {{ range index site.Menus.main }}{{ .Name }}|{{ end }}|
Footer: {{ range index site.Menus.footer }}{{ .Name }}|{{ end }}|
`
b := hugolib.Test(t, files)

b.AssertFileContent("public/index.html",
"Main: Main|p1|p2||",
"Footer: Footer|p2||",
)
}
6 changes: 3 additions & 3 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,19 @@ func testGoFlags() string {
// Note that we don't run with the extended tag. Currently not supported in 32 bit.
func Test386() error {
env := map[string]string{"GOARCH": "386", "GOFLAGS": testGoFlags()}
return runCmd(env, goexe, "test", "./...")
return runCmd(env, goexe, "test", "-p", "2", "./...")
}

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

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

// Run gofmt linter
Expand Down
15 changes: 3 additions & 12 deletions navigation/pagemenus.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,11 @@ type MenuQueryProvider interface {
IsMenuCurrent(menuID string, inme *MenuEntry) bool
}

func PageMenusFromPage(p Page) (PageMenus, error) {
params := p.Params()

ms, ok := params["menus"]
if !ok {
ms, ok = params["menu"]
}

pm := PageMenus{}

if !ok {
func PageMenusFromPage(ms any, p Page) (PageMenus, error) {
if ms == nil {
return nil, nil
}

pm := PageMenus{}
me := MenuEntry{}
SetPageValues(&me, p)

Expand Down
1 change: 1 addition & 0 deletions resources/page/pagemeta/page_frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type PageConfig struct {
Cascade []map[string]any
Sitemap config.SitemapConfig
Build BuildConfig
Menus []string

// User defined params.
Params maps.Params
Expand Down

0 comments on commit 245928a

Please sign in to comment.