diff options
author | Bjørn Erik Pedersen <[email protected]> | 2024-02-03 13:09:53 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2024-02-03 15:07:59 +0100 |
commit | 058f230a1be1abaf589b5a194ef6ec12d14c4021 (patch) | |
tree | 92d84e4e453c3e66075553445e09d78b3ae92d5d /resources/page | |
parent | a66480f70c1ac734ba5af035e626d29ffcde157d (diff) | |
download | hugo-058f230a1be1abaf589b5a194ef6ec12d14c4021.tar.gz hugo-058f230a1be1abaf589b5a194ef6ec12d14c4021.zip |
Detect now invalid path patterns in cascade
Closes #11977
Diffstat (limited to 'resources/page')
-rw-r--r-- | resources/page/page_matcher.go | 17 | ||||
-rw-r--r-- | resources/page/page_matcher_test.go | 19 |
2 files changed, 31 insertions, 5 deletions
diff --git a/resources/page/page_matcher.go b/resources/page/page_matcher.go index fbdb25d72..466b6fe53 100644 --- a/resources/page/page_matcher.go +++ b/resources/page/page_matcher.go @@ -18,6 +18,7 @@ import ( "path/filepath" "strings" + "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/hugofs/glob" @@ -90,7 +91,14 @@ var disallowedCascadeKeys = map[string]bool{ "lang": true, } -func DecodeCascadeConfig(in any) (*config.ConfigNamespace[[]PageMatcherParamsConfig, map[PageMatcher]maps.Params], error) { +// See issue 11977. +func isGlobWithExtension(s string) bool { + pathParts := strings.Split(s, "/") + last := pathParts[len(pathParts)-1] + return strings.Count(last, ".") > 0 +} + +func DecodeCascadeConfig(logger loggers.Logger, in any) (*config.ConfigNamespace[[]PageMatcherParamsConfig, map[PageMatcher]maps.Params], error) { buildConfig := func(in any) (map[PageMatcher]maps.Params, any, error) { cascade := make(map[PageMatcher]maps.Params) if in == nil { @@ -119,6 +127,9 @@ func DecodeCascadeConfig(in any) (*config.ConfigNamespace[[]PageMatcherParamsCon for _, cfg := range cfgs { m := cfg.Target + if isGlobWithExtension(m.Path) { + logger.Erroridf("cascade-pattern-with-extension", "cascade target path %q looks like a path with an extension; since Hugo v0.123.0 this will not match anything, see https://gohugo.io/methods/page/path/", m.Path) + } c, found := cascade[m] if found { // Merge @@ -139,8 +150,8 @@ func DecodeCascadeConfig(in any) (*config.ConfigNamespace[[]PageMatcherParamsCon } // DecodeCascade decodes in which could be either a map or a slice of maps. -func DecodeCascade(in any) (map[PageMatcher]maps.Params, error) { - conf, err := DecodeCascadeConfig(in) +func DecodeCascade(logger loggers.Logger, in any) (map[PageMatcher]maps.Params, error) { + conf, err := DecodeCascadeConfig(logger, in) if err != nil { return nil, err } diff --git a/resources/page/page_matcher_test.go b/resources/page/page_matcher_test.go index c27a2e9b2..e659eb3b5 100644 --- a/resources/page/page_matcher_test.go +++ b/resources/page/page_matcher_test.go @@ -18,6 +18,7 @@ import ( "testing" "github.com/gohugoio/hugo/common/hugo" + "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/maps" qt "github.com/frankban/quicktest" @@ -128,7 +129,7 @@ func TestDecodeCascadeConfig(t *testing.T) { }, } - got, err := DecodeCascadeConfig(in) + got, err := DecodeCascadeConfig(loggers.NewDefault(), in) c.Assert(err, qt.IsNil) c.Assert(got, qt.IsNotNil) @@ -150,7 +151,7 @@ func TestDecodeCascadeConfig(t *testing.T) { {Params: maps.Params{"b": string("bv")}, Target: PageMatcher{Kind: "page"}}, }) - got, err = DecodeCascadeConfig(nil) + got, err = DecodeCascadeConfig(loggers.NewDefault(), nil) c.Assert(err, qt.IsNil) c.Assert(got, qt.IsNotNil) } @@ -172,3 +173,17 @@ func (c testConfig) Running() bool { func (c testConfig) WorkingDir() string { return c.workingDir } + +func TestIsGlobWithExtension(t *testing.T) { + c := qt.New(t) + + c.Assert(isGlobWithExtension("index.md"), qt.Equals, true) + c.Assert(isGlobWithExtension("foo/index.html"), qt.Equals, true) + c.Assert(isGlobWithExtension("posts/page"), qt.Equals, false) + c.Assert(isGlobWithExtension("pa.th/foo"), qt.Equals, false) + c.Assert(isGlobWithExtension(""), qt.Equals, false) + c.Assert(isGlobWithExtension("*.md?"), qt.Equals, true) + c.Assert(isGlobWithExtension("*.md*"), qt.Equals, true) + c.Assert(isGlobWithExtension("posts/*"), qt.Equals, false) + c.Assert(isGlobWithExtension("*.md"), qt.Equals, true) +} |