2022-12-30 03:20:58 -05:00
|
|
|
// Copyright 2023 The Hugo Authors. All rights reserved.
|
2014-04-23 02:59:19 -04:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-04-23 02:59:19 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-04-23 02:59:19 -04:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-12-30 03:20:58 -05:00
|
|
|
// Package navigation provides the menu functionality.
|
2019-01-02 06:33:26 -05:00
|
|
|
package navigation
|
2014-04-23 02:59:19 -04:00
|
|
|
|
2014-04-24 18:11:08 -04:00
|
|
|
import (
|
2014-05-27 18:34:29 -04:00
|
|
|
"html/template"
|
2014-04-24 18:11:08 -04:00
|
|
|
"sort"
|
|
|
|
|
2020-12-02 07:23:25 -05:00
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
|
|
|
"github.com/gohugoio/hugo/common/types"
|
|
|
|
"github.com/gohugoio/hugo/compare"
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2020-12-02 07:23:25 -05:00
|
|
|
|
2014-04-24 18:11:08 -04:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
)
|
2014-04-23 02:59:19 -04:00
|
|
|
|
2021-05-23 04:42:01 -04:00
|
|
|
var smc = newMenuCache()
|
|
|
|
|
2016-03-22 19:29:39 -04:00
|
|
|
// MenuEntry represents a menu item defined in either Page front matter
|
|
|
|
// or in the site config.
|
2014-04-23 02:59:19 -04:00
|
|
|
type MenuEntry struct {
|
2023-01-04 12:24:36 -05:00
|
|
|
// The menu entry configuration.
|
|
|
|
MenuConfig
|
2022-04-21 04:59:13 -04:00
|
|
|
|
|
|
|
// The menu containing this menu entry.
|
|
|
|
Menu string
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// The URL value from front matter / config.
|
|
|
|
ConfiguredURL string
|
2022-04-21 04:59:13 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// The Page connected to this menu entry.
|
|
|
|
Page Page
|
2022-04-21 04:59:13 -04:00
|
|
|
|
|
|
|
// Child entries.
|
|
|
|
Children Menu
|
2019-04-19 03:18:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MenuEntry) URL() string {
|
|
|
|
|
2021-07-21 05:34:26 -04:00
|
|
|
// Check page first.
|
|
|
|
// In Hugo 0.86.0 we added `pageRef`,
|
|
|
|
// a way to connect menu items in site config to pages.
|
|
|
|
// This means that you now can have both a Page
|
|
|
|
// and a configured URL.
|
|
|
|
// Having the configured URL as a fallback if the Page isn't found
|
|
|
|
// is obviously more useful, especially in multilingual sites.
|
2019-04-19 03:18:12 -04:00
|
|
|
if !types.IsNil(m.Page) {
|
|
|
|
return m.Page.RelPermalink()
|
|
|
|
}
|
|
|
|
|
2021-07-21 05:34:26 -04:00
|
|
|
return m.ConfiguredURL
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// SetPageValues sets the Page and URL values for this menu entry.
|
|
|
|
func SetPageValues(m *MenuEntry, p Page) {
|
|
|
|
m.Page = p
|
|
|
|
if m.MenuConfig.Name == "" {
|
|
|
|
m.MenuConfig.Name = p.LinkTitle()
|
|
|
|
}
|
|
|
|
if m.MenuConfig.Title == "" {
|
|
|
|
m.MenuConfig.Title = p.Title()
|
|
|
|
}
|
|
|
|
if m.MenuConfig.Weight == 0 {
|
|
|
|
m.MenuConfig.Weight = p.Weight()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
// A narrow version of page.Page.
|
|
|
|
type Page interface {
|
|
|
|
LinkTitle() string
|
2023-01-04 12:24:36 -05:00
|
|
|
Title() string
|
2019-01-02 06:33:26 -05:00
|
|
|
RelPermalink() string
|
2021-07-30 04:56:45 -04:00
|
|
|
Path() string
|
2019-01-02 06:33:26 -05:00
|
|
|
Section() string
|
|
|
|
Weight() int
|
|
|
|
IsPage() bool
|
2021-07-20 06:10:22 -04:00
|
|
|
IsSection() bool
|
2022-03-17 17:03:27 -04:00
|
|
|
IsAncestor(other any) (bool, error)
|
2019-11-21 15:59:38 -05:00
|
|
|
Params() maps.Params
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
2016-03-22 19:29:39 -04:00
|
|
|
// Menu is a collection of menu entries.
|
2014-04-23 02:59:19 -04:00
|
|
|
type Menu []*MenuEntry
|
2016-03-22 19:29:39 -04:00
|
|
|
|
|
|
|
// Menus is a dictionary of menus.
|
2019-01-02 06:33:26 -05:00
|
|
|
type Menus map[string]Menu
|
2016-03-22 19:29:39 -04:00
|
|
|
|
|
|
|
// PageMenus is a dictionary of menus defined in the Pages.
|
2014-04-23 02:59:19 -04:00
|
|
|
type PageMenus map[string]*MenuEntry
|
|
|
|
|
2016-03-22 19:29:39 -04:00
|
|
|
// HasChildren returns whether this menu item has any children.
|
|
|
|
func (m *MenuEntry) HasChildren() bool {
|
|
|
|
return m.Children != nil
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 19:29:39 -04:00
|
|
|
// KeyName returns the key used to identify this menu entry.
|
|
|
|
func (m *MenuEntry) KeyName() string {
|
|
|
|
if m.Identifier != "" {
|
|
|
|
return m.Identifier
|
2014-05-14 18:01:13 -04:00
|
|
|
}
|
2016-03-22 19:29:39 -04:00
|
|
|
return m.Name
|
2014-05-14 18:01:13 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 19:29:39 -04:00
|
|
|
func (m *MenuEntry) hopefullyUniqueID() string {
|
|
|
|
if m.Identifier != "" {
|
|
|
|
return m.Identifier
|
2019-04-19 03:18:12 -04:00
|
|
|
} else if m.URL() != "" {
|
|
|
|
return m.URL()
|
2014-05-14 18:01:13 -04:00
|
|
|
} else {
|
2016-03-22 19:29:39 -04:00
|
|
|
return m.Name
|
2014-05-14 18:01:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 23:23:45 -05:00
|
|
|
// isEqual returns whether the two menu entries represents the same menu entry.
|
|
|
|
func (m *MenuEntry) isEqual(inme *MenuEntry) bool {
|
2016-03-22 19:29:39 -04:00
|
|
|
return m.hopefullyUniqueID() == inme.hopefullyUniqueID() && m.Parent == inme.Parent
|
2014-05-14 18:01:13 -04:00
|
|
|
}
|
|
|
|
|
2023-11-07 23:23:45 -05:00
|
|
|
// isSameResource returns whether the two menu entries points to the same
|
2016-03-22 19:29:39 -04:00
|
|
|
// resource (URL).
|
2023-11-07 23:23:45 -05:00
|
|
|
func (m *MenuEntry) isSameResource(inme *MenuEntry) bool {
|
2021-07-20 06:10:22 -04:00
|
|
|
if m.isSamePage(inme.Page) {
|
|
|
|
return m.Page == inme.Page
|
|
|
|
}
|
2019-04-19 03:18:12 -04:00
|
|
|
murl, inmeurl := m.URL(), inme.URL()
|
|
|
|
return murl != "" && inmeurl != "" && murl == inmeurl
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
|
2021-07-20 06:10:22 -04:00
|
|
|
func (m *MenuEntry) isSamePage(p Page) bool {
|
|
|
|
if !types.IsNil(m.Page) && !types.IsNil(p) {
|
|
|
|
return m.Page == p
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// MenuConfig holds the configuration for a menu.
|
|
|
|
type MenuConfig struct {
|
|
|
|
Identifier string
|
|
|
|
Parent string
|
|
|
|
Name string
|
|
|
|
Pre template.HTML
|
|
|
|
Post template.HTML
|
|
|
|
URL string
|
|
|
|
PageRef string
|
|
|
|
Weight int
|
|
|
|
Title string
|
|
|
|
// User defined params.
|
|
|
|
Params maps.Params
|
2014-04-24 18:11:08 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// For internal use.
|
|
|
|
|
2022-04-21 04:59:13 -04:00
|
|
|
// This is for internal use only.
|
2019-01-02 06:33:26 -05:00
|
|
|
func (m Menu) Add(me *MenuEntry) Menu {
|
|
|
|
m = append(m, me)
|
|
|
|
// TODO(bep)
|
2014-04-23 02:59:19 -04:00
|
|
|
m.Sort()
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of a custom sorter for Menu
|
|
|
|
*/
|
|
|
|
|
|
|
|
// A type to implement the sort interface for Menu
|
2016-03-22 19:29:39 -04:00
|
|
|
type menuSorter struct {
|
2014-04-23 02:59:19 -04:00
|
|
|
menu Menu
|
2016-03-22 19:29:39 -04:00
|
|
|
by menuEntryBy
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Closure used in the Sort.Less method.
|
2016-03-22 19:29:39 -04:00
|
|
|
type menuEntryBy func(m1, m2 *MenuEntry) bool
|
2014-04-23 02:59:19 -04:00
|
|
|
|
2016-03-22 19:29:39 -04:00
|
|
|
func (by menuEntryBy) Sort(menu Menu) {
|
|
|
|
ms := &menuSorter{
|
2014-04-23 02:59:19 -04:00
|
|
|
menu: menu,
|
|
|
|
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
|
|
|
|
}
|
2014-09-09 05:59:47 -04:00
|
|
|
sort.Stable(ms)
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
2015-03-07 06:52:29 -05:00
|
|
|
var defaultMenuEntrySort = func(m1, m2 *MenuEntry) bool {
|
2014-04-23 02:59:19 -04:00
|
|
|
if m1.Weight == m2.Weight {
|
2019-08-01 04:19:19 -04:00
|
|
|
c := compare.Strings(m1.Name, m2.Name)
|
|
|
|
if c == 0 {
|
2015-11-09 15:01:36 -05:00
|
|
|
return m1.Identifier < m2.Identifier
|
|
|
|
}
|
2019-08-01 04:19:19 -04:00
|
|
|
return c < 0
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
2017-02-20 03:33:35 -05:00
|
|
|
|
|
|
|
if m2.Weight == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if m1.Weight == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-03-07 06:52:29 -05:00
|
|
|
return m1.Weight < m2.Weight
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 19:29:39 -04:00
|
|
|
func (ms *menuSorter) Len() int { return len(ms.menu) }
|
|
|
|
func (ms *menuSorter) Swap(i, j int) { ms.menu[i], ms.menu[j] = ms.menu[j], ms.menu[i] }
|
2014-04-23 02:59:19 -04:00
|
|
|
|
|
|
|
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
|
2016-03-22 19:29:39 -04:00
|
|
|
func (ms *menuSorter) Less(i, j int) bool { return ms.by(ms.menu[i], ms.menu[j]) }
|
2014-04-23 02:59:19 -04:00
|
|
|
|
2016-03-22 19:29:39 -04:00
|
|
|
// Sort sorts the menu by weight, name and then by identifier.
|
2016-02-07 06:34:43 -05:00
|
|
|
func (m Menu) Sort() Menu {
|
2016-03-22 19:29:39 -04:00
|
|
|
menuEntryBy(defaultMenuEntrySort).Sort(m)
|
2016-02-07 06:34:43 -05:00
|
|
|
return m
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 19:29:39 -04:00
|
|
|
// Limit limits the returned menu to n entries.
|
2015-03-07 06:52:29 -05:00
|
|
|
func (m Menu) Limit(n int) Menu {
|
2016-02-06 16:53:59 -05:00
|
|
|
if len(m) > n {
|
2015-03-07 06:52:29 -05:00
|
|
|
return m[0:n]
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
2015-03-07 06:52:29 -05:00
|
|
|
return m
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 19:29:39 -04:00
|
|
|
// ByWeight sorts the menu by the weight defined in the menu configuration.
|
2015-03-07 06:52:29 -05:00
|
|
|
func (m Menu) ByWeight() Menu {
|
2021-05-23 04:42:01 -04:00
|
|
|
const key = "menuSort.ByWeight"
|
|
|
|
menus, _ := smc.get(key, menuEntryBy(defaultMenuEntrySort).Sort, m)
|
|
|
|
|
|
|
|
return menus
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 19:29:39 -04:00
|
|
|
// ByName sorts the menu by the name defined in the menu configuration.
|
2015-03-07 06:52:29 -05:00
|
|
|
func (m Menu) ByName() Menu {
|
2021-05-23 04:42:01 -04:00
|
|
|
const key = "menuSort.ByName"
|
2014-04-23 02:59:19 -04:00
|
|
|
title := func(m1, m2 *MenuEntry) bool {
|
2019-08-01 04:19:19 -04:00
|
|
|
return compare.LessStrings(m1.Name, m2.Name)
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
2021-05-23 04:42:01 -04:00
|
|
|
menus, _ := smc.get(key, menuEntryBy(title).Sort, m)
|
|
|
|
|
|
|
|
return menus
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 19:29:39 -04:00
|
|
|
// Reverse reverses the order of the menu entries.
|
2014-04-23 02:59:19 -04:00
|
|
|
func (m Menu) Reverse() Menu {
|
2021-05-23 04:42:01 -04:00
|
|
|
const key = "menuSort.Reverse"
|
|
|
|
reverseFunc := func(menu Menu) {
|
|
|
|
for i, j := 0, len(menu)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
menu[i], menu[j] = menu[j], menu[i]
|
|
|
|
}
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
2021-05-23 04:42:01 -04:00
|
|
|
menus, _ := smc.get(key, reverseFunc, m)
|
2014-04-23 02:59:19 -04:00
|
|
|
|
2021-05-23 04:42:01 -04:00
|
|
|
return menus
|
|
|
|
}
|
|
|
|
|
2022-04-21 04:59:13 -04:00
|
|
|
// Clone clones the menu entries.
|
|
|
|
// This is for internal use only.
|
2021-05-23 04:42:01 -04:00
|
|
|
func (m Menu) Clone() Menu {
|
|
|
|
return append(Menu(nil), m...)
|
2014-04-23 02:59:19 -04:00
|
|
|
}
|
2017-12-15 12:49:21 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func DecodeConfig(in any) (*config.ConfigNamespace[map[string]MenuConfig, Menus], error) {
|
|
|
|
buildConfig := func(in any) (Menus, any, error) {
|
|
|
|
ret := Menus{}
|
|
|
|
|
|
|
|
if in == nil {
|
|
|
|
return ret, map[string]any{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
menus, err := maps.ToStringMapE(in)
|
|
|
|
if err != nil {
|
|
|
|
return ret, nil, err
|
|
|
|
}
|
|
|
|
menus = maps.CleanConfigStringMap(menus)
|
|
|
|
|
|
|
|
for name, menu := range menus {
|
|
|
|
m, err := cast.ToSliceE(menu)
|
|
|
|
if err != nil {
|
|
|
|
return ret, nil, err
|
|
|
|
} else {
|
|
|
|
|
|
|
|
for _, entry := range m {
|
|
|
|
var menuConfig MenuConfig
|
|
|
|
if err := mapstructure.WeakDecode(entry, &menuConfig); err != nil {
|
|
|
|
return ret, nil, err
|
|
|
|
}
|
|
|
|
maps.PrepareParams(menuConfig.Params)
|
|
|
|
menuEntry := MenuEntry{
|
|
|
|
Menu: name,
|
|
|
|
MenuConfig: menuConfig,
|
|
|
|
}
|
|
|
|
menuEntry.ConfiguredURL = menuEntry.MenuConfig.URL
|
|
|
|
|
|
|
|
if ret[name] == nil {
|
|
|
|
ret[name] = Menu{}
|
|
|
|
}
|
|
|
|
ret[name] = ret[name].Add(&menuEntry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, menus, nil
|
2017-12-15 12:49:21 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
return config.DecodeNamespace[map[string]MenuConfig](in, buildConfig)
|
2017-12-15 12:49:21 -05:00
|
|
|
}
|