Skip to content

Commit 9df3736

Browse files
rmetzlerbep
authored andcommitted
Add .Title and .Page to MenuEntry
It uses `title` if configured on the menu entry. If not, it uses the `Page.Title` when possible. Fixes #2784
1 parent 2436941 commit 9df3736

File tree

4 files changed

+62
-16
lines changed

4 files changed

+62
-16
lines changed

hugolib/menu.go

+16
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ import (
2525
// or in the site config.
2626
type MenuEntry struct {
2727
URL string
28+
Page *Page
2829
Name string
2930
Menu string
3031
Identifier string
32+
title string
3133
Pre template.HTML
3234
Post template.HTML
3335
Weight int
@@ -95,6 +97,8 @@ func (m *MenuEntry) marshallMap(ime map[string]interface{}) {
9597
m.Weight = cast.ToInt(v)
9698
case "name":
9799
m.Name = cast.ToString(v)
100+
case "title":
101+
m.title = cast.ToString(v)
98102
case "pre":
99103
m.Pre = template.HTML(cast.ToString(v))
100104
case "post":
@@ -213,3 +217,15 @@ func (m Menu) Reverse() Menu {
213217

214218
return m
215219
}
220+
221+
func (m *MenuEntry) Title() string {
222+
if m.title != "" {
223+
return m.title
224+
}
225+
226+
if m.Page != nil {
227+
return m.Page.LinkTitle()
228+
}
229+
230+
return ""
231+
}

hugolib/menu_old_test.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ const (
8383
[[menu.unicode]]
8484
name = "Unicode Russian"
8585
identifier = "unicode-russian"
86-
url = "/новости-проекта"` // Russian => "news-project"
86+
url = "/новости-проекта" # Russian => "news-project"
87+
[[menu.with_title]]
88+
name="entry with title"
89+
title="a menuentry title"
90+
url="/title"
91+
identifier="titled"`
8792
)
8893

8994
var menuPage1 = []byte(`+++
@@ -388,6 +393,13 @@ func doTestMenuWithUnicodeURLs(t *testing.T, canonifyURLs bool) {
388393
assert.Equal(t, expected, unicodeRussian.URL)
389394
}
390395

396+
func TestMenuWithTitle(t *testing.T) {
397+
s := setupMenuTests(t, menuPageSources)
398+
titled := findTestMenuEntryByID(s, "with_title", "titled")
399+
expected := "a menuentry title"
400+
assert.Equal(t, expected, titled.Title())
401+
}
402+
391403
// Issue #1114
392404
func TestSectionPagesMenu2(t *testing.T) {
393405
t.Parallel()

hugolib/menu_test.go

+29-11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ title: %q
2929
weight: %d
3030
menu:
3131
%s:
32+
title: %s
3233
weight: %d
3334
---
3435
# Doc Menu
@@ -44,11 +45,15 @@ title = "Section Menu"
4445
sectionPagesMenu = "sect"
4546
`
4647

47-
th, h := newTestSitesFromConfig(t, afero.NewMemMapFs(), siteConfig,
48-
"layouts/partials/menu.html", `{{- $p := .page -}}
48+
th, h := newTestSitesFromConfig(
49+
t,
50+
afero.NewMemMapFs(),
51+
siteConfig,
52+
"layouts/partials/menu.html",
53+
`{{- $p := .page -}}
4954
{{- $m := .menu -}}
5055
{{ range (index $p.Site.Menus $m) -}}
51-
{{- .URL }}|{{ .Name }}|{{ .Weight -}}|
56+
{{- .URL }}|{{ .Name }}|{{ .Title }}|{{ .Weight -}}|
5257
{{- if $p.IsMenuCurrent $m . }}IsMenuCurrent{{ else }}-{{ end -}}|
5358
{{- if $p.HasMenuCurrent $m . }}HasMenuCurrent{{ else }}-{{ end -}}|
5459
{{- end -}}
@@ -63,11 +68,11 @@ Menu Main: {{ partial "menu.html" (dict "page" . "menu" "main") }}`,
6368

6469
fs := th.Fs
6570

66-
writeSource(t, fs, "content/sect1/p1.md", fmt.Sprintf(menuPageTemplate, "p1", 1, "main", 40))
67-
writeSource(t, fs, "content/sect1/p2.md", fmt.Sprintf(menuPageTemplate, "p2", 2, "main", 30))
68-
writeSource(t, fs, "content/sect2/p3.md", fmt.Sprintf(menuPageTemplate, "p3", 3, "main", 20))
69-
writeSource(t, fs, "content/sect2/p4.md", fmt.Sprintf(menuPageTemplate, "p4", 4, "main", 10))
70-
writeSource(t, fs, "content/sect3/p5.md", fmt.Sprintf(menuPageTemplate, "p5", 5, "main", 5))
71+
writeSource(t, fs, "content/sect1/p1.md", fmt.Sprintf(menuPageTemplate, "p1", 1, "main", "atitle1", 40))
72+
writeSource(t, fs, "content/sect1/p2.md", fmt.Sprintf(menuPageTemplate, "p2", 2, "main", "atitle2", 30))
73+
writeSource(t, fs, "content/sect2/p3.md", fmt.Sprintf(menuPageTemplate, "p3", 3, "main", "atitle3", 20))
74+
writeSource(t, fs, "content/sect2/p4.md", fmt.Sprintf(menuPageTemplate, "p4", 4, "main", "atitle4", 10))
75+
writeSource(t, fs, "content/sect3/p5.md", fmt.Sprintf(menuPageTemplate, "p5", 5, "main", "atitle5", 5))
7176

7277
writeNewContentFile(t, fs, "Section One", "2017-01-01", "content/sect1/_index.md", 100)
7378
writeNewContentFile(t, fs, "Section Five", "2017-01-01", "content/sect5/_index.md", 10)
@@ -86,11 +91,24 @@ Menu Main: {{ partial "menu.html" (dict "page" . "menu" "main") }}`,
8691
require.Len(t, p1, 1)
8792

8893
th.assertFileContent("public/sect1/p1/index.html", "Single",
89-
"Menu Sect: /sect5/|Section Five|10|-|-|/sect1/|Section One|100|-|HasMenuCurrent|/sect2/|Sect2s|0|-|-|/sect3/|Sect3s|0|-|-|",
90-
"Menu Main: /sect3/p5/|p5|5|-|-|/sect2/p4/|p4|10|-|-|/sect2/p3/|p3|20|-|-|/sect1/p2/|p2|30|-|-|/sect1/p1/|p1|40|IsMenuCurrent|-|",
94+
"Menu Sect: "+
95+
"/sect5/|Section Five||10|-|-|"+
96+
"/sect1/|Section One||100|-|HasMenuCurrent|"+
97+
"/sect2/|Sect2s||0|-|-|"+
98+
"/sect3/|Sect3s||0|-|-|",
99+
"Menu Main: "+
100+
"/sect3/p5/|p5|atitle5|5|-|-|"+
101+
"/sect2/p4/|p4|atitle4|10|-|-|"+
102+
"/sect2/p3/|p3|atitle3|20|-|-|"+
103+
"/sect1/p2/|p2|atitle2|30|-|-|"+
104+
"/sect1/p1/|p1|atitle1|40|IsMenuCurrent|-|",
91105
)
92106

93107
th.assertFileContent("public/sect2/p3/index.html", "Single",
94-
"Menu Sect: /sect5/|Section Five|10|-|-|/sect1/|Section One|100|-|-|/sect2/|Sect2s|0|-|HasMenuCurrent|/sect3/|Sect3s|0|-|-|")
108+
"Menu Sect: "+
109+
"/sect5/|Section Five||10|-|-|"+
110+
"/sect1/|Section One||100|-|-|"+
111+
"/sect2/|Sect2s||0|-|HasMenuCurrent|"+
112+
"/sect3/|Sect3s||0|-|-|")
95113

96114
}

hugolib/page.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ func (p *Page) HasMenuCurrent(menuID string, me *MenuEntry) bool {
12911291

12921292
// The following logic is kept from back when Hugo had both Page and Node types.
12931293
// TODO(bep) consolidate / clean
1294-
nme := MenuEntry{Name: p.Title, URL: p.URL()}
1294+
nme := MenuEntry{Page: p, Name: p.Title, URL: p.URL()}
12951295

12961296
for _, child := range me.Children {
12971297
if nme.IsSameResource(child) {
@@ -1322,7 +1322,7 @@ func (p *Page) IsMenuCurrent(menuID string, inme *MenuEntry) bool {
13221322

13231323
// The following logic is kept from back when Hugo had both Page and Node types.
13241324
// TODO(bep) consolidate / clean
1325-
me := MenuEntry{Name: p.Title, URL: p.URL()}
1325+
me := MenuEntry{Page: p, Name: p.Title, URL: p.URL()}
13261326

13271327
if !me.IsSameResource(inme) {
13281328
return false
@@ -1369,7 +1369,7 @@ func (p *Page) Menus() PageMenus {
13691369
if ms, ok := p.Params["menu"]; ok {
13701370
link := p.RelPermalink()
13711371

1372-
me := MenuEntry{Name: p.LinkTitle(), Weight: p.Weight, URL: link}
1372+
me := MenuEntry{Page: p, Name: p.LinkTitle(), Weight: p.Weight, URL: link}
13731373

13741374
// Could be the name of the menu to attach it to
13751375
mname, err := cast.ToStringE(ms)
@@ -1399,7 +1399,7 @@ func (p *Page) Menus() PageMenus {
13991399
}
14001400

14011401
for name, menu := range menus {
1402-
menuEntry := MenuEntry{Name: p.LinkTitle(), URL: link, Weight: p.Weight, Menu: name}
1402+
menuEntry := MenuEntry{Page: p, Name: p.LinkTitle(), URL: link, Weight: p.Weight, Menu: name}
14031403
if menu != nil {
14041404
p.s.Log.DEBUG.Printf("found menu: %q, in %q\n", name, p.Title)
14051405
ime, err := cast.ToStringMapE(menu)

0 commit comments

Comments
 (0)