Skip to content

Commit

Permalink
Fix validation of Page Kind in cascade target map
Browse files Browse the repository at this point in the history
Fixes #8888
  • Loading branch information
jmooring authored and bep committed Feb 10, 2022
1 parent a7d182c commit d1109f5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/content/en/content-management/front-matter.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ path
: A [Glob](https://github.com/gobwas/glob) pattern matching the content path below /content. Expects Unix-styled slashes. Note that this is the virtual path, so it starts at the mount root. The matching support double-asterisks so you can match for patterns like `/blog/*/**` to match anything from the third level and down.

kind
: The Page's Kind, e.g. "section".
: A Glob pattern matching the Page's Kind(s), e.g. "{home,section}".

lang
: A Glob pattern matching the Page's language, e.g. "{en,sv}".
Expand Down
15 changes: 11 additions & 4 deletions resources/page/page_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ import (
"path/filepath"
"strings"

"github.com/pkg/errors"

"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/hugofs/glob"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)

// A PageMatcher can be used to match a Page with Glob patterns.
Expand Down Expand Up @@ -114,8 +113,16 @@ func DecodePageMatcher(m interface{}, v *PageMatcher) error {

v.Kind = strings.ToLower(v.Kind)
if v.Kind != "" {
if _, found := kindMap[v.Kind]; !found {
return errors.Errorf("%q is not a valid Page Kind", v.Kind)
g, _ := glob.GetGlob(v.Kind)
found := false
for _, k := range kindMap {
if g.Match(k) {
found = true
break
}
}
if !found {
return errors.Errorf("%q did not match a valid Page Kind", v.Kind)
}
}

Expand Down
7 changes: 6 additions & 1 deletion resources/page/page_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ func TestPageMatcher(t *testing.T) {

c.Run("Decode", func(c *qt.C) {
var v PageMatcher
c.Assert(DecodePageMatcher(map[string]interface{}{"kind": "foo"}, &v), qt.Not((qt.IsNil)))
c.Assert(DecodePageMatcher(map[string]interface{}{"kind": "foo"}, &v), qt.Not(qt.IsNil))
c.Assert(DecodePageMatcher(map[string]interface{}{"kind": "{foo,bar}"}, &v), qt.Not(qt.IsNil))
c.Assert(DecodePageMatcher(map[string]interface{}{"kind": "taxonomy"}, &v), qt.IsNil)
c.Assert(DecodePageMatcher(map[string]interface{}{"kind": "{taxonomy,foo}"}, &v), qt.IsNil)
c.Assert(DecodePageMatcher(map[string]interface{}{"kind": "{taxonomy,term}"}, &v), qt.IsNil)
c.Assert(DecodePageMatcher(map[string]interface{}{"kind": "*"}, &v), qt.IsNil)
c.Assert(DecodePageMatcher(map[string]interface{}{"kind": "home", "path": filepath.FromSlash("/a/b/**")}, &v), qt.IsNil)
c.Assert(v, qt.Equals, PageMatcher{Kind: "home", Path: "/a/b/**"})
})
Expand Down

0 comments on commit d1109f5

Please sign in to comment.