Add config.cascade

This commit adds support for using the `cascade` keyword in your configuration file(s), e.g. `config.toml`.

Note that

* Every feature of `cascade` is available, e.g. `_target` to target specific page sets.
* Pages, e.g. the home page, can overwrite the cascade defined in config.

Fixes #8741
This commit is contained in:
Bjørn Erik Pedersen
2021-07-09 11:52:03 +02:00
parent 30eea3915b
commit 5cb52c2315
6 changed files with 129 additions and 32 deletions

View File

@@ -19,6 +19,7 @@ import (
"github.com/pkg/errors"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/hugofs/glob"
"github.com/mitchellh/mapstructure"
)
@@ -70,6 +71,42 @@ func (m PageMatcher) Matches(p Page) bool {
return true
}
// DecodeCascade decodes in which could be eiter a map or a slice of maps.
func DecodeCascade(in interface{}) (map[PageMatcher]maps.Params, error) {
m, err := maps.ToSliceStringMap(in)
if err != nil {
return map[PageMatcher]maps.Params{
{}: maps.ToStringMap(in),
}, nil
}
cascade := make(map[PageMatcher]maps.Params)
for _, vv := range m {
var m PageMatcher
if mv, found := vv["_target"]; found {
err := DecodePageMatcher(mv, &m)
if err != nil {
return nil, err
}
}
c, found := cascade[m]
if found {
// Merge
for k, v := range vv {
if _, found := c[k]; !found {
c[k] = v
}
}
} else {
cascade[m] = vv
}
}
return cascade, nil
}
// DecodePageMatcher decodes m into v.
func DecodePageMatcher(m interface{}, v *PageMatcher) error {
if err := mapstructure.WeakDecode(m, v); err != nil {