Skip to content

Commit 1f26420

Browse files
committed
hugolib: Support sub-sections in permalink settings
This enables both the variants below: Current (first level only): ``` "blog": ":section/:title", ``` Nested (all levels): ``` "blog": ":sections/:title", ``` Should ideally been part of Hugo 0.22, but better late than never ... Fixes #3580
1 parent ff54b6b commit 1f26420

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

‎hugolib/permalinks.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package hugolib
1616
import (
1717
"errors"
1818
"fmt"
19+
"path"
1920
"regexp"
2021
"strconv"
2122
"strings"
@@ -182,6 +183,12 @@ func pageToPermalinkSection(p *Page, _ string) (string, error) {
182183
return p.Section(), nil
183184
}
184185

186+
func pageToPermalinkSections(p *Page, _ string) (string, error) {
187+
// TODO(bep) we have some superflous URLize in this file, but let's
188+
// deal with that later.
189+
return path.Join(p.current().sections...), nil
190+
}
191+
185192
func init() {
186193
knownPermalinkAttributes = map[string]pageToPermaAttribute{
187194
"year": pageToPermalinkDate,
@@ -192,6 +199,7 @@ func init() {
192199
"weekdayname": pageToPermalinkDate,
193200
"yearday": pageToPermalinkDate,
194201
"section": pageToPermalinkSection,
202+
"sections": pageToPermalinkSections,
195203
"title": pageToPermalinkTitle,
196204
"slug": pageToPermalinkSlugElseTitle,
197205
"filename": pageToPermalinkFilename,

‎hugolib/site_sections_test.go‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func TestNestedSections(t *testing.T) {
3232
th = testHelper{cfg, fs, t}
3333
)
3434

35+
cfg.Set("permalinks", map[string]string{
36+
"perm a": ":sections/:title",
37+
})
38+
3539
pageTemplate := `---
3640
title: T%d_%d
3741
---
@@ -64,6 +68,15 @@ Content
6468
writeSource(t, fs, filepath.Join("content", "empty3", "b", "c", "d", "_index.md"), fmt.Sprintf(pageTemplate, 41, -1))
6569
writeSource(t, fs, filepath.Join("content", "empty3", "b", "empty3.md"), fmt.Sprintf(pageTemplate, 3, -1))
6670

71+
// Section with permalink config
72+
writeSource(t, fs, filepath.Join("content", "perm a", "link", "_index.md"), fmt.Sprintf(pageTemplate, 9, -1))
73+
for i := 1; i < 4; i++ {
74+
writeSource(t, fs, filepath.Join("content", "perm a", "link", fmt.Sprintf("page_%d.md", i)),
75+
fmt.Sprintf(pageTemplate, 1, i))
76+
}
77+
writeSource(t, fs, filepath.Join("content", "perm a", "link", "regular", fmt.Sprintf("page_%d.md", 5)),
78+
fmt.Sprintf(pageTemplate, 1, 5))
79+
6780
writeSource(t, fs, filepath.Join("content", "l1", "l2", "_index.md"), fmt.Sprintf(pageTemplate, 2, -1))
6881
writeSource(t, fs, filepath.Join("content", "l1", "l2_2", "_index.md"), fmt.Sprintf(pageTemplate, 22, -1))
6982
writeSource(t, fs, filepath.Join("content", "l1", "l2", "l3", "_index.md"), fmt.Sprintf(pageTemplate, 3, -1))
@@ -96,7 +109,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
96109
cfg.Set("paginate", 2)
97110

98111
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
99-
require.Len(t, s.RegularPages, 14)
112+
require.Len(t, s.RegularPages, 18)
100113

101114
tests := []struct {
102115
sections string
@@ -185,6 +198,18 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
185198
assert.Equal("T2_-1", p.Parent().Title)
186199
assert.Len(p.Sections(), 0)
187200
}},
201+
{"perm a,link", func(p *Page) {
202+
assert.Equal("T9_-1", p.Title)
203+
assert.Equal("/perm-a/link/", p.RelPermalink())
204+
assert.Len(p.Pages, 4)
205+
first := p.Pages[0]
206+
assert.Equal("/perm-a/link/t1_1/", first.RelPermalink())
207+
th.assertFileContent("public/perm-a/link/t1_1/index.html", "Single|T1_1")
208+
209+
last := p.Pages[3]
210+
assert.Equal("/perm-a/link/t1_5/", last.RelPermalink())
211+
212+
}},
188213
}
189214

190215
for _, test := range tests {
@@ -203,7 +228,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
203228

204229
assert.NotNil(home)
205230

206-
assert.Len(home.Sections(), 6)
231+
assert.Len(home.Sections(), 7)
207232

208233
rootPage := s.getPage(KindPage, "mypage.md")
209234
assert.NotNil(rootPage)

0 commit comments

Comments
 (0)