diff options
author | n1xx1 <[email protected]> | 2024-08-01 12:14:29 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2024-08-01 12:14:29 +0200 |
commit | 566fe7ba12e7ecabadffa9ec76b319d04063c78b (patch) | |
tree | 227ffdfe6d1cb203f87f2930b9d892adb243805c /hugolib | |
parent | 92573012e83bf8d71a247027d2f7f7f43a9c42b7 (diff) | |
download | hugo-566fe7ba12e7ecabadffa9ec76b319d04063c78b.tar.gz hugo-566fe7ba12e7ecabadffa9ec76b319d04063c78b.zip |
resources/page: Expand parmalinks tokens in `url`
This change allows to use permalink tokens in url front matter fields. This should be useful to target more specific pages instead of using a global permalink configuration. It's expected to be used with cascade.
Fixes #9714
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/page__paths.go | 13 | ||||
-rw-r--r-- | hugolib/page_permalink_test.go | 2 |
2 files changed, 15 insertions, 0 deletions
diff --git a/hugolib/page__paths.go b/hugolib/page__paths.go index d89388f81..6324b5871 100644 --- a/hugolib/page__paths.go +++ b/hugolib/page__paths.go @@ -141,6 +141,19 @@ func createTargetPathDescriptor(p *pageState) (page.TargetPathDescriptor, error) desc.PrefixFilePath = s.getLanguageTargetPathLang(alwaysInSubDir) desc.PrefixLink = s.getLanguagePermalinkLang(alwaysInSubDir) + if desc.URL != "" && strings.IndexByte(desc.URL, ':') >= 0 { + // Attempt to parse and expand an url + opath, err := d.ResourceSpec.Permalinks.ExpandPattern(desc.URL, p) + if err != nil { + return desc, err + } + + if opath != "" { + opath, _ = url.QueryUnescape(opath) + desc.URL = opath + } + } + opath, err := d.ResourceSpec.Permalinks.Expand(p.Section(), p) if err != nil { return desc, err diff --git a/hugolib/page_permalink_test.go b/hugolib/page_permalink_test.go index bc89638d3..d8fd99d79 100644 --- a/hugolib/page_permalink_test.go +++ b/hugolib/page_permalink_test.go @@ -59,6 +59,8 @@ func TestPermalink(t *testing.T) { // test URL overrides {"x/y/z/boofar.md", "", "", "/z/y/q/", false, false, "/z/y/q/", "/z/y/q/"}, + // test URL override with expands + {"x/y/z/boofar.md", "", "test", "/z/:slug/", false, false, "/z/test/", "/z/test/"}, } for i, test := range tests { |