]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
resources/page: Add :contentbasename and :contentbasenameorslug permalink tokens
authorHenrique Dias <mail@hacdias.com>
Wed, 12 Feb 2025 13:13:17 +0000 (14:13 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 17 Feb 2025 08:41:49 +0000 (09:41 +0100)
See #11722

docs/content/en/content-management/urls.md
resources/page/permalinks.go
resources/page/permalinks_test.go

index ab0c7148a428404ab24b86f3744311e36899fca9..62ccfb6669f47892895c82bdc3c218edee36afa2 100644 (file)
@@ -317,6 +317,14 @@ Use these tokens when defining the URL pattern. You can also use these tokens wh
 `:slugorfilename`
 : The slug as defined in front matter, else the content's file name without extension, applicable to the `page` page kind.
 
+`:contentbasename`
+: The content base name, as defined in [`File.ContentBaseName`], applicable to pages backed by a file.
+
+`:contentbasenameorslug`
+: The content base name, else the slug as defined above.
+
+[`File.ContentBaseName`]: /methods/page/file/#contentbasename
+
 For time-related values, you can also use the layout string components defined in Go's [time package]. For example:
 
 [time package]: https://pkg.go.dev/time#pkg-constants
index ece10bb40e50de2f0d16d475d184c20a192c78f3..56dfff46f30882bcaf0fe4165bbd351d74cc96b6 100644 (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 ""
index 808521f42ef2961f8dca658d77f3613145adbdfa..71c5ded65029aaf60dd54ab9d4782b151c94e094 100644 (file)
@@ -46,6 +46,8 @@ var testdataPermalinks = []struct {
        {"/:sections[0]/:sections[last]/", true, "/a/c/"},               // Sections
        {"/\\:filename", true, "/:filename"},                            // Escape sequence
        {"/special\\::slug/", true, "/special:the-slug/"},               // Escape sequence
+       {"/:contentbasename/", true, "/index/"},                         // Content base name
+       {"/:contentbasenameorslug/", true, "/index/"},                   // Content base name or slug
 
        // Failures
        {"/blog/:fred", false, ""},