mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-21 21:35:28 +02:00
Fix menu URL when multiple permalinkable output formats
In Hugo `0.55` we introduced the `permalinkable` config attribute on Output Format, default enabled for `AMP` and `HTML`. This meant that a Page could have different `RelPermalink` and `Permalink` depending on the rendering format. The menu `URL` did not reflect that fact. Fixes #5849
This commit is contained in:
@@ -14,6 +14,8 @@
|
||||
package navigation
|
||||
|
||||
import (
|
||||
"github.com/gohugoio/hugo/common/types"
|
||||
|
||||
"html/template"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -24,17 +26,29 @@ import (
|
||||
// MenuEntry represents a menu item defined in either Page front matter
|
||||
// or in the site config.
|
||||
type MenuEntry struct {
|
||||
URL string
|
||||
Page Page
|
||||
Name string
|
||||
Menu string
|
||||
Identifier string
|
||||
title string
|
||||
Pre template.HTML
|
||||
Post template.HTML
|
||||
Weight int
|
||||
Parent string
|
||||
Children Menu
|
||||
ConfiguredURL string // The URL value from front matter / config.
|
||||
Page Page
|
||||
Name string
|
||||
Menu string
|
||||
Identifier string
|
||||
title string
|
||||
Pre template.HTML
|
||||
Post template.HTML
|
||||
Weight int
|
||||
Parent string
|
||||
Children Menu
|
||||
}
|
||||
|
||||
func (m *MenuEntry) URL() string {
|
||||
if m.ConfiguredURL != "" {
|
||||
return m.ConfiguredURL
|
||||
}
|
||||
|
||||
if !types.IsNil(m.Page) {
|
||||
return m.Page.RelPermalink()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// A narrow version of page.Page.
|
||||
@@ -72,8 +86,8 @@ func (m *MenuEntry) KeyName() string {
|
||||
func (m *MenuEntry) hopefullyUniqueID() string {
|
||||
if m.Identifier != "" {
|
||||
return m.Identifier
|
||||
} else if m.URL != "" {
|
||||
return m.URL
|
||||
} else if m.URL() != "" {
|
||||
return m.URL()
|
||||
} else {
|
||||
return m.Name
|
||||
}
|
||||
@@ -87,7 +101,8 @@ 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 {
|
||||
return m.URL != "" && inme.URL != "" && m.URL == inme.URL
|
||||
murl, inmeurl := m.URL(), inme.URL()
|
||||
return murl != "" && inmeurl != "" && murl == inmeurl
|
||||
}
|
||||
|
||||
func (m *MenuEntry) MarshallMap(ime map[string]interface{}) {
|
||||
@@ -95,7 +110,7 @@ func (m *MenuEntry) MarshallMap(ime map[string]interface{}) {
|
||||
loki := strings.ToLower(k)
|
||||
switch loki {
|
||||
case "url":
|
||||
m.URL = cast.ToString(v)
|
||||
m.ConfiguredURL = cast.ToString(v)
|
||||
case "weight":
|
||||
m.Weight = cast.ToInt(v)
|
||||
case "name":
|
||||
|
@@ -50,9 +50,7 @@ func PageMenusFromPage(p Page) (PageMenus, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
link := p.RelPermalink()
|
||||
|
||||
me := MenuEntry{Page: p, Name: p.LinkTitle(), Weight: p.Weight(), URL: link}
|
||||
me := MenuEntry{Page: p, Name: p.LinkTitle(), Weight: p.Weight()}
|
||||
|
||||
// Could be the name of the menu to attach it to
|
||||
mname, err := cast.ToStringE(ms)
|
||||
@@ -81,7 +79,7 @@ func PageMenusFromPage(p Page) (PageMenus, error) {
|
||||
}
|
||||
|
||||
for name, menu := range menus {
|
||||
menuEntry := MenuEntry{Page: p, Name: p.LinkTitle(), URL: link, Weight: p.Weight(), Menu: name}
|
||||
menuEntry := MenuEntry{Page: p, Name: p.LinkTitle(), Weight: p.Weight(), Menu: name}
|
||||
if menu != nil {
|
||||
ime, err := cast.ToStringMapE(menu)
|
||||
if err != nil {
|
||||
@@ -153,7 +151,7 @@ func (pm *pageMenus) HasMenuCurrent(menuID string, me *MenuEntry) bool {
|
||||
|
||||
// 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(), URL: pm.p.RelPermalink()}
|
||||
nme := MenuEntry{Page: pm.p, Name: pm.p.LinkTitle()}
|
||||
|
||||
for _, child := range me.Children {
|
||||
if nme.IsSameResource(child) {
|
||||
@@ -183,7 +181,7 @@ func (pm *pageMenus) IsMenuCurrent(menuID string, inme *MenuEntry) bool {
|
||||
|
||||
// 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(), URL: pm.p.RelPermalink()}
|
||||
me := MenuEntry{Page: pm.p, Name: pm.p.LinkTitle()}
|
||||
|
||||
if !me.IsSameResource(inme) {
|
||||
return false
|
||||
|
Reference in New Issue
Block a user