resource: Make GetByPrefix work for Page resources

Fixes #4264
This commit is contained in:
Bjørn Erik Pedersen 2018-01-12 17:46:50 +01:00
parent 57e10f174e
commit 60c9f3b1c3
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 12 additions and 1 deletions

View file

@ -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]

View file

@ -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
}