Simplify "active menu" logic for section menus

Fixes #8776
This commit is contained in:
Bjørn Erik Pedersen 2021-07-20 12:10:22 +02:00
parent 634481ba8c
commit d831d2fce8
7 changed files with 151 additions and 34 deletions

View file

@ -32,6 +32,7 @@ var smc = newMenuCache()
type MenuEntry struct {
ConfiguredURL string // The URL value from front matter / config.
Page Page
PageRef string // The path to the page, only relevant for site config.
Name string
Menu string
Identifier string
@ -63,6 +64,8 @@ type Page interface {
Section() string
Weight() int
IsPage() bool
IsSection() bool
IsAncestor(other interface{}) (bool, error)
Params() maps.Params
}
@ -106,16 +109,28 @@ func (m *MenuEntry) IsEqual(inme *MenuEntry) bool {
// IsSameResource returns whether the two menu entries points to the same
// resource (URL).
func (m *MenuEntry) IsSameResource(inme *MenuEntry) bool {
if m.isSamePage(inme.Page) {
return m.Page == inme.Page
}
murl, inmeurl := m.URL(), inme.URL()
return murl != "" && inmeurl != "" && murl == inmeurl
}
func (m *MenuEntry) isSamePage(p Page) bool {
if !types.IsNil(m.Page) && !types.IsNil(p) {
return m.Page == p
}
return false
}
func (m *MenuEntry) MarshallMap(ime map[string]interface{}) {
for k, v := range ime {
loki := strings.ToLower(k)
switch loki {
case "url":
m.ConfiguredURL = cast.ToString(v)
case "pageref":
m.PageRef = cast.ToString(v)
case "weight":
m.Weight = cast.ToInt(v)
case "name":

View file

@ -15,6 +15,7 @@ package navigation
import (
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/types"
"github.com/pkg/errors"
"github.com/spf13/cast"
@ -97,31 +98,25 @@ func PageMenusFromPage(p Page) (PageMenus, error) {
}
func NewMenuQueryProvider(
setionPagesMenu string,
pagem PageMenusGetter,
sitem MenusGetter,
p Page) MenuQueryProvider {
return &pageMenus{
p: p,
pagem: pagem,
sitem: sitem,
setionPagesMenu: setionPagesMenu,
p: p,
pagem: pagem,
sitem: sitem,
}
}
type pageMenus struct {
pagem PageMenusGetter
sitem MenusGetter
setionPagesMenu string
p Page
pagem PageMenusGetter
sitem MenusGetter
p Page
}
func (pm *pageMenus) HasMenuCurrent(menuID string, me *MenuEntry) bool {
// page is labeled as "shadow-member" of the menu with the same identifier as the section
if pm.setionPagesMenu != "" {
section := pm.p.Section()
if section != "" && pm.setionPagesMenu == menuID && section == me.Identifier {
if !types.IsNil(me.Page) && me.Page.IsSection() {
if ok, _ := me.Page.IsAncestor(pm.p); ok {
return true
}
}
@ -143,18 +138,15 @@ func (pm *pageMenus) HasMenuCurrent(menuID string, me *MenuEntry) bool {
}
}
if pm.p == nil || pm.p.IsPage() {
if pm.p == nil {
return false
}
// The following logic is kept from back when Hugo had both Page and Node types.
// TODO(bep) consolidate / clean
nme := MenuEntry{Page: pm.p, Name: pm.p.LinkTitle()}
for _, child := range me.Children {
if nme.IsSameResource(child) {
if child.isSamePage(pm.p) {
return true
}
if pm.HasMenuCurrent(menuID, child) {
return true
}
@ -172,20 +164,16 @@ func (pm *pageMenus) IsMenuCurrent(menuID string, inme *MenuEntry) bool {
}
}
if pm.p == nil || pm.p.IsPage() {
if pm.p == nil {
return false
}
// The following logic is kept from back when Hugo had both Page and Node types.
// TODO(bep) consolidate / clean
me := MenuEntry{Page: pm.p, Name: pm.p.LinkTitle()}
if !me.IsSameResource(inme) {
if !inme.isSamePage(pm.p) {
return false
}
// this resource may be included in several menus
// search for it to make sure that it is in the menu with the given menuId
// This resource may be included in several menus.
// Search for it to make sure that it is in the menu with the given menuId.
if menu, ok := pm.sitem.Menus()[menuID]; ok {
for _, menuEntry := range menu {
if menuEntry.IsSameResource(inme) {