resources/page: Revise the new contentbasename permalinks tokens

* Make it work for all pages, including those created from content adapters and not backed by a file.
* Allow the `slug` to win, so the new tokens are:

`:contentbasename`:

1. ContentBaseName

`:slugorcontentbasename`:

1. Slug
2. ContentBaseName

Note that a page will always have a `ContentBaseName`, so no need to fall back to e.g. the title.

Closes #11722
This commit is contained in:
Bjørn Erik Pedersen 2025-02-12 22:36:24 +01:00
parent cb7a4339b7
commit 7b7a0f3624
6 changed files with 145 additions and 60 deletions

View file

@ -22,7 +22,7 @@ import (
"github.com/gohugoio/hugo/common/hashing"
"github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/hugofs/files"
"github.com/gohugoio/hugo/common/hugio"
@ -132,11 +132,17 @@ func (fi *File) p() *paths.Path {
return fi.fim.Meta().PathInfo.Unnormalized()
}
var contentPathParser = &paths.PathParser{
IsContentExt: func(ext string) bool {
return true
},
}
// Used in tests.
func NewFileInfoFrom(path, filename string) *File {
func NewContentFileInfoFrom(path, filename string) *File {
meta := &hugofs.FileMeta{
Filename: filename,
PathInfo: media.DefaultPathParser.Parse("", filepath.ToSlash(path)),
PathInfo: contentPathParser.Parse(files.ComponentFolderContent, filepath.ToSlash(path)),
}
return NewFileInfo(hugofs.NewFileMetaInfo(nil, meta))