Minify minifiers.MinifyConfig `mapstructure:"-"`
// Permalink configuration.
- Permalinks map[string]string `mapstructure:"-"`
+ Permalinks map[string]map[string]string `mapstructure:"-"`
// Taxonomy configuration.
Taxonomies map[string]string `mapstructure:"-"`
"permalinks": {
key: "permalinks",
decode: func(d decodeWeight, p decodeConfig) error {
- p.c.Permalinks = maps.CleanConfigStringMapString(p.p.GetStringMapString(d.key))
+ p.c.Permalinks = make(map[string]map[string]string)
+
+ p.c.Permalinks["page"] = make(map[string]string)
+ p.c.Permalinks["section"] = make(map[string]string)
+ p.c.Permalinks["taxonomy"] = make(map[string]string)
+ p.c.Permalinks["term"] = make(map[string]string)
+
+ config := maps.CleanConfigStringMap(p.p.GetStringMap(d.key))
+ for k, v := range config {
+ switch v := v.(type) {
+ case string:
+ // [permalinks]
+ // key = '...'
+
+ // To sucessfully be backward compatible, "default" patterns need to be set for both page and term
+ p.c.Permalinks["page"][k] = v;
+ p.c.Permalinks["term"][k] = v;
+
+ case maps.Params:
+ // [permalinks.key]
+ // xyz = ???
+
+ if (k == "page") || (k == "section") || (k == "taxonomy") || (k == "term") {
+ // TODO: warn if we overwrite an already set value
+ for k2, v2 := range v {
+ switch v2 := v2.(type) {
+ case string:
+ p.c.Permalinks[k][k2] = v2
+
+ default:
+ return fmt.Errorf("permalinks configuration invalid: unknown value %q for key %q for kind %q", v2, k2, k)
+ }
+ }
+ } else {
+ return fmt.Errorf("permalinks configuration only allows per-kind configuration 'page', 'section', 'taxonomy' and 'term'; unknown kind: %q", k)
+ }
+
+ default:
+ return fmt.Errorf("permalinks configuration invalid: unknown value %q for key %q", v, k)
+ }
+ }
+
return nil
},
},
ContentClassBranch ContentClass = "branch"
ContentClassFile ContentClass = "zfile" // Sort below
ContentClassContent ContentClass = "zcontent"
+ ContentClassZero ContentClass = "zero" // Special value for zeroFile
)
func (c ContentClass) IsBundle() bool {
"strings"
"github.com/gohugoio/hugo/helpers"
+ "github.com/gohugoio/hugo/hugofs/files"
"github.com/gohugoio/hugo/resources/page"
)
)
d := s.Deps
+ classifier := files.ContentClassZero
if !p.File().IsZero() {
dir = p.File().Dir()
baseName = p.File().TranslationBaseName()
contentBaseName = p.File().ContentBaseName()
+ classifier = p.File().Classifier()
}
- if baseName != contentBaseName {
+ if classifier == files.ContentClassLeaf {
// See https://github.com/gohugoio/hugo/issues/4870
// A leaf bundle
dir = strings.TrimSuffix(dir, contentBaseName+helpers.FilePathSeparator)
desc.PrefixFilePath = s.getLanguageTargetPathLang(alwaysInSubDir)
desc.PrefixLink = s.getLanguagePermalinkLang(alwaysInSubDir)
- // Expand only page.KindPage and page.KindTaxonomy; don't expand other Kinds of Pages
- // like page.KindSection or page.KindTaxonomyTerm because they are "shallower" and
- // the permalink configuration values are likely to be redundant, e.g.
- // naively expanding /category/:slug/ would give /category/categories/ for
- // the "categories" page.KindTaxonomyTerm.
- if p.Kind() == page.KindPage || p.Kind() == page.KindTerm {
- opath, err := d.ResourceSpec.Permalinks.Expand(p.Section(), p)
- if err != nil {
- return desc, err
- }
+ opath, err := d.ResourceSpec.Permalinks.Expand(p.Section(), p)
+ if err != nil {
+ return desc, err
+ }
- if opath != "" {
- opath, _ = url.QueryUnescape(opath)
- desc.ExpandedPermalink = opath
+ if opath != "" {
+ opath, _ = url.QueryUnescape(opath)
+ if strings.HasSuffix(opath, "//") {
+ // When rewriting the _index of the section the permalink config is applied to,
+ // we get douple slashes at the end sometimes; clear them up here
+ opath = strings.TrimSuffix(opath, "/")
}
+ desc.ExpandedPermalink = opath
+
+ if !p.File().IsZero() {
+ s.Log.Debugf("Set expanded permalink path for %s %s to %#v", p.Kind(), p.File().Path(), opath)
+ } else {
+ s.Log.Debugf("Set expanded permalink path for %s in %v to %#v", p.Kind(), desc.Sections, opath)
+ }
}
return desc, nil
methods := c.MethodsFromTypes([]reflect.Type{reflect.TypeOf((*source.File)(nil)).Elem()}, nil)
for _, m := range methods {
- if m.Name == "IsZero" {
+ if m.Name == "IsZero" || m.Name == "Classifier" {
continue
}
fmt.Fprint(&buff, m.DeclarationNamed("zeroFile"))
return true
}
+func (z zeroFile) Classifier() files.ContentClass {
+ z.log.Warnln(".File.Classifier on zero object. Wrap it in if or with: {{ with .File }}{{ .Classifier }}{{ end }}")
+ return files.ContentClassZero
+}
+
%s
`, header, importsString(pkgImports), buff.String())
"errors"
"github.com/gohugoio/hugo/helpers"
+
)
// PermalinkExpander holds permalin mappings per section.
// to be used to replace that tag.
knownPermalinkAttributes map[string]pageToPermaAttribute
- expanders map[string]func(Page) (string, error)
+ expanders map[string]map[string]func(Page) (string, error)
urlize func(uri string) string
}
// NewPermalinkExpander creates a new PermalinkExpander configured by the given
// urlize func.
-func NewPermalinkExpander(urlize func(uri string) string, patterns map[string]string) (PermalinkExpander, error) {
+func NewPermalinkExpander(urlize func(uri string) string, patterns map[string]map[string]string) (PermalinkExpander, error) {
p := PermalinkExpander{urlize: urlize}
p.knownPermalinkAttributes = map[string]pageToPermaAttribute{
"filename": p.pageToPermalinkFilename,
}
- e, err := p.parse(patterns)
- if err != nil {
- return p, err
- }
+ p.expanders = make(map[string]map[string]func(Page) (string, error))
- p.expanders = e
+ for kind, patterns := range patterns {
+ e, err := p.parse(patterns)
+ if err != nil {
+ return p, err
+ }
+ p.expanders[kind] = e
+ }
return p, nil
}
// Expand expands the path in p according to the rules defined for the given key.
// If no rules are found for the given key, an empty string is returned.
func (l PermalinkExpander) Expand(key string, p Page) (string, error) {
- expand, found := l.expanders[key]
+ expanders, found := l.expanders[p.Kind()]
+
+ if !found {
+ return "", nil
+ }
+
+ expand, found := expanders[key]
if !found {
return "", nil
// pageToPermalinkTitle returns the URL-safe form of the title
func (l PermalinkExpander) pageToPermalinkTitle(p Page, _ string) (string, error) {
+ if p.File().TranslationBaseName() == "_index" {
+ return "", nil
+ }
+
return l.urlize(p.Title()), nil
}
// Page bundles; the directory name will hopefully have a better name.
dir := strings.TrimSuffix(p.File().Dir(), helpers.FilePathSeparator)
_, name = filepath.Split(dir)
+ } else if name == "_index" {
+ return "", nil
}
return l.urlize(name), nil
page.date = d
page.section = "blue"
page.slug = "The Slug"
+ page.kind = "page"
for _, item := range testdataPermalinks {
if !item.valid {
name := specNameCleaner.ReplaceAllString(item.spec, "")
c.Run(name, func(c *qt.C) {
- patterns := map[string]string{
- "posts": item.spec,
+ patterns := map[string]map[string]string{
+ "page": {
+ "posts": item.spec,
+ },
}
expander, err := NewPermalinkExpander(urlize, patterns)
c.Assert(err, qt.IsNil)
page.date = d
page.section = "blue"
page.slug = "The Slug"
+ page.kind = "page"
page_slug_fallback := newTestPageWithFile("/page-filename/index.md")
page_slug_fallback.title = "Page Title"
-
- permalinksConfig := map[string]string{
- "posts": "/:slug",
- "blog": "/:section/:year",
- "recipes": "/:slugorfilename",
+ page_slug_fallback.kind = "page"
+
+ permalinksConfig := map[string]map[string]string{
+ "page": {
+ "posts": "/:slug",
+ "blog": "/:section/:year",
+ "recipes": "/:slugorfilename",
+ },
}
expander, err := NewPermalinkExpander(urlize, permalinksConfig)
c.Assert(err, qt.IsNil)
c := qt.New(t)
- permalinksConfig := map[string]string{
- "posts": "/:slug/",
+ permalinksConfig := map[string]map[string]string{
+ "page": {
+ "posts": "/:slug/",
+ },
}
expander, err := NewPermalinkExpander(urlize, permalinksConfig)
go func(i int) {
defer wg.Done()
page := newTestPage()
+ page.kind = "page"
for j := 1; j < 20; j++ {
page.slug = fmt.Sprintf("slug%d", i+j)
expanded, err := expander.Expand("posts", page)
page.title = "Hugo Rocks"
d, _ := time.Parse("2006-01-02", "2019-02-28")
page.date = d
+ page.kind = "page"
- permalinksConfig := map[string]string{
- "posts": "/:year-:month-:title",
+ permalinksConfig := map[string]map[string]string{
+ "page": {
+ "posts": "/:year-:month-:title",
+ },
}
expander, err := NewPermalinkExpander(urlize, permalinksConfig)
if err != nil {
import (
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/hugofs"
+ "github.com/gohugoio/hugo/hugofs/files"
"github.com/gohugoio/hugo/source"
)
return true
}
+func (z zeroFile) Classifier() files.ContentClass {
+ z.log.Warnln(".File.Classifier on zero object. Wrap it in if or with: {{ with .File }}{{ .Classifier }}{{ end }}")
+ return files.ContentClassZero
+}
+
func (z zeroFile) Path() (o0 string) {
z.log.Warnln(".File.Path on zero object. Wrap it in if or with: {{ with .File }}{{ .Path }}{{ end }}")
return
// if file is a leaf bundle.
ContentBaseName() string
+ // Classifier is the ContentClass of the file
+ Classifier() files.ContentClass
+
// UniqueID is the MD5 hash of the file's path and is for most practical applications,
// Hugo content files being one of them, considered to be unique.
UniqueID() string
return fi.contentBaseName
}
+// Classifier is the ContentClass of the file
+func (fi *FileInfo) Classifier() files.ContentClass {
+ return fi.classifier;
+}
+
// Section returns a file's section.
func (fi *FileInfo) Section() string {
fi.init()