resources/page: Add :contentbasename and :contentbasenameorslug permalink tokens

See #11722
This commit is contained in:
Henrique Dias
2025-02-12 14:13:17 +01:00
committed by Bjørn Erik Pedersen
parent 157d3703c3
commit cb7a4339b7
3 changed files with 45 additions and 13 deletions

View File

@@ -79,19 +79,21 @@ func NewPermalinkExpander(urlize func(uri string) string, patterns map[string]ma
}
p.knownPermalinkAttributes = map[string]pageToPermaAttribute{
"year": p.pageToPermalinkDate,
"month": p.pageToPermalinkDate,
"monthname": p.pageToPermalinkDate,
"day": p.pageToPermalinkDate,
"weekday": p.pageToPermalinkDate,
"weekdayname": p.pageToPermalinkDate,
"yearday": p.pageToPermalinkDate,
"section": p.pageToPermalinkSection,
"sections": p.pageToPermalinkSections,
"title": p.pageToPermalinkTitle,
"slug": p.pageToPermalinkSlugElseTitle,
"slugorfilename": p.pageToPermalinkSlugElseFilename,
"filename": p.pageToPermalinkFilename,
"year": p.pageToPermalinkDate,
"month": p.pageToPermalinkDate,
"monthname": p.pageToPermalinkDate,
"day": p.pageToPermalinkDate,
"weekday": p.pageToPermalinkDate,
"weekdayname": p.pageToPermalinkDate,
"yearday": p.pageToPermalinkDate,
"section": p.pageToPermalinkSection,
"sections": p.pageToPermalinkSections,
"title": p.pageToPermalinkTitle,
"slug": p.pageToPermalinkSlugElseTitle,
"slugorfilename": p.pageToPermalinkSlugElseFilename,
"filename": p.pageToPermalinkFilename,
"contentbasename": p.pageToPermalinkContentBaseName,
"contentbasenameorslug": p.pageToPermalinkContentBaseNameOrSlug,
}
p.expanders = make(map[string]map[string]func(Page) (string, error))
@@ -307,6 +309,26 @@ func (l PermalinkExpander) pageToPermalinkSections(p Page, _ string) (string, er
return p.CurrentSection().SectionsPath(), nil
}
// pageToPermalinkContentBaseName returns the URL-safe form of the content base name.
func (l PermalinkExpander) pageToPermalinkContentBaseName(p Page, _ string) (string, error) {
if p.File() == nil {
return "", nil
}
return l.urlize(p.File().ContentBaseName()), nil
}
// pageToPermalinkContentBaseNameOrSlug returns the URL-safe form of the content base name, or the slug.
func (l PermalinkExpander) pageToPermalinkContentBaseNameOrSlug(p Page, a string) (string, error) {
name, err := l.pageToPermalinkContentBaseName(p, a)
if err != nil {
return "", nil
}
if name != "" {
return name, nil
}
return l.pageToPermalinkSlugElseTitle(p, a)
}
func (l PermalinkExpander) translationBaseName(p Page) string {
if p.File() == nil {
return ""