Skip to content

Commit cb7a433

Browse files
hacdiasbep
authored andcommitted
resources/page: Add :contentbasename and :contentbasenameorslug permalink tokens
See #11722
1 parent 157d370 commit cb7a433

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

docs/content/en/content-management/urls.md

+8
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,14 @@ Use these tokens when defining the URL pattern. You can also use these tokens wh
317317
`:slugorfilename`
318318
: The slug as defined in front matter, else the content's file name without extension, applicable to the `page` page kind.
319319
320+
`:contentbasename`
321+
: The content base name, as defined in [`File.ContentBaseName`], applicable to pages backed by a file.
322+
323+
`:contentbasenameorslug`
324+
: The content base name, else the slug as defined above.
325+
326+
[`File.ContentBaseName`]: /methods/page/file/#contentbasename
327+
320328
For time-related values, you can also use the layout string components defined in Go's [time package]. For example:
321329
322330
[time package]: https://pkg.go.dev/time#pkg-constants

resources/page/permalinks.go

+35-13
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,21 @@ func NewPermalinkExpander(urlize func(uri string) string, patterns map[string]ma
7979
}
8080

8181
p.knownPermalinkAttributes = map[string]pageToPermaAttribute{
82-
"year": p.pageToPermalinkDate,
83-
"month": p.pageToPermalinkDate,
84-
"monthname": p.pageToPermalinkDate,
85-
"day": p.pageToPermalinkDate,
86-
"weekday": p.pageToPermalinkDate,
87-
"weekdayname": p.pageToPermalinkDate,
88-
"yearday": p.pageToPermalinkDate,
89-
"section": p.pageToPermalinkSection,
90-
"sections": p.pageToPermalinkSections,
91-
"title": p.pageToPermalinkTitle,
92-
"slug": p.pageToPermalinkSlugElseTitle,
93-
"slugorfilename": p.pageToPermalinkSlugElseFilename,
94-
"filename": p.pageToPermalinkFilename,
82+
"year": p.pageToPermalinkDate,
83+
"month": p.pageToPermalinkDate,
84+
"monthname": p.pageToPermalinkDate,
85+
"day": p.pageToPermalinkDate,
86+
"weekday": p.pageToPermalinkDate,
87+
"weekdayname": p.pageToPermalinkDate,
88+
"yearday": p.pageToPermalinkDate,
89+
"section": p.pageToPermalinkSection,
90+
"sections": p.pageToPermalinkSections,
91+
"title": p.pageToPermalinkTitle,
92+
"slug": p.pageToPermalinkSlugElseTitle,
93+
"slugorfilename": p.pageToPermalinkSlugElseFilename,
94+
"filename": p.pageToPermalinkFilename,
95+
"contentbasename": p.pageToPermalinkContentBaseName,
96+
"contentbasenameorslug": p.pageToPermalinkContentBaseNameOrSlug,
9597
}
9698

9799
p.expanders = make(map[string]map[string]func(Page) (string, error))
@@ -307,6 +309,26 @@ func (l PermalinkExpander) pageToPermalinkSections(p Page, _ string) (string, er
307309
return p.CurrentSection().SectionsPath(), nil
308310
}
309311

312+
// pageToPermalinkContentBaseName returns the URL-safe form of the content base name.
313+
func (l PermalinkExpander) pageToPermalinkContentBaseName(p Page, _ string) (string, error) {
314+
if p.File() == nil {
315+
return "", nil
316+
}
317+
return l.urlize(p.File().ContentBaseName()), nil
318+
}
319+
320+
// pageToPermalinkContentBaseNameOrSlug returns the URL-safe form of the content base name, or the slug.
321+
func (l PermalinkExpander) pageToPermalinkContentBaseNameOrSlug(p Page, a string) (string, error) {
322+
name, err := l.pageToPermalinkContentBaseName(p, a)
323+
if err != nil {
324+
return "", nil
325+
}
326+
if name != "" {
327+
return name, nil
328+
}
329+
return l.pageToPermalinkSlugElseTitle(p, a)
330+
}
331+
310332
func (l PermalinkExpander) translationBaseName(p Page) string {
311333
if p.File() == nil {
312334
return ""

resources/page/permalinks_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ var testdataPermalinks = []struct {
4646
{"/:sections[0]/:sections[last]/", true, "/a/c/"}, // Sections
4747
{"/\\:filename", true, "/:filename"}, // Escape sequence
4848
{"/special\\::slug/", true, "/special:the-slug/"}, // Escape sequence
49+
{"/:contentbasename/", true, "/index/"}, // Content base name
50+
{"/:contentbasenameorslug/", true, "/index/"}, // Content base name or slug
4951

5052
// Failures
5153
{"/blog/:fred", false, ""},

0 commit comments

Comments
 (0)