Permalink
Browse files

resource: Make GetByPrefix work for Page resources

Fixes #4264
  • Loading branch information...
bep committed Jan 12, 2018
1 parent 57e10f1 commit 60c9f3b1c34b69771e25a66906f150f460d73223
Showing with 12 additions and 1 deletion.
  1. +4 −0 hugolib/page_bundler_test.go
  2. +8 −1 resource/resource.go
@@ -111,6 +111,10 @@ func TestPageBundlerSite(t *testing.T) {
assert.Contains(firstPage.Content, "TheContent")
assert.Len(leafBundle1.Resources, 6) // 2 pages 3 images 1 custom mime type
assert.Equal(firstPage, pageResources.GetByPrefix("1"))
assert.Equal(secondPage, pageResources.GetByPrefix("2"))
assert.Nil(pageResources.GetByPrefix("doesnotexist"))
imageResources := leafBundle1.Resources.ByType("image")
assert.Len(imageResources, 3)
image := imageResources[0]
@@ -72,8 +72,15 @@ func (r Resources) ByType(tp string) Resources {
func (r Resources) GetByPrefix(prefix string) Resource {
prefix = strings.ToLower(prefix)
for _, resource := range r {
_, name := filepath.Split(resource.RelPermalink())
var name string
f, ok := resource.(source.File)
if ok {
name = f.BaseFileName()
} else {
_, name = filepath.Split(resource.RelPermalink())
}
name = strings.ToLower(name)
if strings.HasPrefix(name, prefix) {
return resource
}

0 comments on commit 60c9f3b

Please sign in to comment.