Skip to content

Commit

Permalink
hugolib: Support sub-sections in permalink settings
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bep committed Jun 13, 2017
1 parent ff54b6b commit 1f26420
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
8 changes: 8 additions & 0 deletions hugolib/permalinks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package hugolib
import (
"errors"
"fmt"
"path"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -182,6 +183,12 @@ func pageToPermalinkSection(p *Page, _ string) (string, error) {
return p.Section(), nil
}

func pageToPermalinkSections(p *Page, _ string) (string, error) {
// TODO(bep) we have some superflous URLize in this file, but let's
// deal with that later.
return path.Join(p.current().sections...), nil
}

func init() {
knownPermalinkAttributes = map[string]pageToPermaAttribute{
"year": pageToPermalinkDate,
Expand All @@ -192,6 +199,7 @@ func init() {
"weekdayname": pageToPermalinkDate,
"yearday": pageToPermalinkDate,
"section": pageToPermalinkSection,
"sections": pageToPermalinkSections,
"title": pageToPermalinkTitle,
"slug": pageToPermalinkSlugElseTitle,
"filename": pageToPermalinkFilename,
Expand Down
29 changes: 27 additions & 2 deletions hugolib/site_sections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func TestNestedSections(t *testing.T) {
th = testHelper{cfg, fs, t}
)

cfg.Set("permalinks", map[string]string{
"perm a": ":sections/:title",
})

pageTemplate := `---
title: T%d_%d
---
Expand Down Expand Up @@ -64,6 +68,15 @@ Content
writeSource(t, fs, filepath.Join("content", "empty3", "b", "c", "d", "_index.md"), fmt.Sprintf(pageTemplate, 41, -1))
writeSource(t, fs, filepath.Join("content", "empty3", "b", "empty3.md"), fmt.Sprintf(pageTemplate, 3, -1))

// Section with permalink config
writeSource(t, fs, filepath.Join("content", "perm a", "link", "_index.md"), fmt.Sprintf(pageTemplate, 9, -1))
for i := 1; i < 4; i++ {
writeSource(t, fs, filepath.Join("content", "perm a", "link", fmt.Sprintf("page_%d.md", i)),
fmt.Sprintf(pageTemplate, 1, i))
}
writeSource(t, fs, filepath.Join("content", "perm a", "link", "regular", fmt.Sprintf("page_%d.md", 5)),
fmt.Sprintf(pageTemplate, 1, 5))

writeSource(t, fs, filepath.Join("content", "l1", "l2", "_index.md"), fmt.Sprintf(pageTemplate, 2, -1))
writeSource(t, fs, filepath.Join("content", "l1", "l2_2", "_index.md"), fmt.Sprintf(pageTemplate, 22, -1))
writeSource(t, fs, filepath.Join("content", "l1", "l2", "l3", "_index.md"), fmt.Sprintf(pageTemplate, 3, -1))
Expand Down Expand Up @@ -96,7 +109,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
cfg.Set("paginate", 2)

s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
require.Len(t, s.RegularPages, 14)
require.Len(t, s.RegularPages, 18)

tests := []struct {
sections string
Expand Down Expand Up @@ -185,6 +198,18 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
assert.Equal("T2_-1", p.Parent().Title)
assert.Len(p.Sections(), 0)
}},
{"perm a,link", func(p *Page) {
assert.Equal("T9_-1", p.Title)
assert.Equal("/perm-a/link/", p.RelPermalink())
assert.Len(p.Pages, 4)
first := p.Pages[0]
assert.Equal("/perm-a/link/t1_1/", first.RelPermalink())
th.assertFileContent("public/perm-a/link/t1_1/index.html", "Single|T1_1")

last := p.Pages[3]
assert.Equal("/perm-a/link/t1_5/", last.RelPermalink())

}},
}

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

assert.NotNil(home)

assert.Len(home.Sections(), 6)
assert.Len(home.Sections(), 7)

rootPage := s.getPage(KindPage, "mypage.md")
assert.NotNil(rootPage)
Expand Down

0 comments on commit 1f26420

Please sign in to comment.