content adapter: Add support for menus in AddPage

Fixes #12507
This commit is contained in:
Bjørn Erik Pedersen
2024-05-29 13:23:36 +02:00
parent 98503acd31
commit f8aefd7883
4 changed files with 45 additions and 14 deletions

View File

@@ -62,8 +62,22 @@ func (p *pageMenus) init() {
p.p, 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 var err error
p.pm, err = navigation.PageMenusFromPage(p.p) p.pm, err = navigation.PageMenusFromPage(menus, p.p)
if err != nil { if err != nil {
p.p.s.Log.Errorln(p.p.wrapError(err)) p.p.s.Log.Errorln(p.p.wrapError(err))
} }

View File

@@ -610,3 +610,31 @@ foo
b.AssertFileContent("public/a/index.html", "|xfoo|") b.AssertFileContent("public/a/index.html", "|xfoo|")
b.AssertFileContent("public/b/index.html", "|foo|") // fails 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||",
)
}

View File

@@ -41,20 +41,8 @@ type MenuQueryProvider interface {
IsMenuCurrent(menuID string, inme *MenuEntry) bool IsMenuCurrent(menuID string, inme *MenuEntry) bool
} }
func PageMenusFromPage(p Page) (PageMenus, error) { func PageMenusFromPage(ms any, p Page) (PageMenus, error) {
params := p.Params()
ms, ok := params["menus"]
if !ok {
ms, ok = params["menu"]
}
pm := PageMenus{} pm := PageMenus{}
if !ok {
return nil, nil
}
me := MenuEntry{} me := MenuEntry{}
SetPageValues(&me, p) SetPageValues(&me, p)

View File

@@ -101,6 +101,7 @@ type PageConfig struct {
Cascade []map[string]any Cascade []map[string]any
Sitemap config.SitemapConfig Sitemap config.SitemapConfig
Build BuildConfig Build BuildConfig
Menus []string
// User defined params. // User defined params.
Params maps.Params Params maps.Params