Skip to content

Commit

Permalink
Fall back to original name in Resources.GetMatch/Match
Browse files Browse the repository at this point in the history
Same as we do in .Get.

Fixes #12076
  • Loading branch information
bep committed Feb 20, 2024
1 parent a118cb4 commit 48eec2a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions hugolib/pagebundler_test.go
Expand Up @@ -876,3 +876,20 @@ RegularPages: {{ range .RegularPages }}{{ .RelPermalink }}|File LogicalName: {{
"List: |/mysection|File LogicalName: _index.md|/mysection/|section|Resources: sectiondata.json: Secion data JSON.|sectiondata.txt: Section data TXT.|$",
"RegularPages: /mysection/foo/p2/|File LogicalName: p2.md|/mysection/mybundle/|File LogicalName: index.md|/mysection/p2/|File LogicalName: p2.md|$")
}

func TestBundleResourcesGetMatchOriginalName(t *testing.T) {
files := `
-- hugo.toml --
baseURL = "https://example.com"
-- content/mybundle/index.md --
-- content/mybundle/f1.en.txt --
F1.
-- layouts/_default/single.html --
GetMatch: {{ with .Resources.GetMatch "f1.en.*" }}{{ .Name }}: {{ .Content }}|{{ end }}
Match: {{ range .Resources.Match "f1.en.*" }}{{ .Name }}: {{ .Content }}|{{ end }}
`

b := Test(t, files)

b.AssertFileContent("public/mybundle/index.html", "GetMatch: f1.txt: F1.|", "Match: f1.txt: F1.|")
}
19 changes: 19 additions & 0 deletions resources/resource/resources.go
Expand Up @@ -106,6 +106,15 @@ func (r Resources) GetMatch(pattern any) Resource {
}
}

// Finally, check the original name.
for _, resource := range r {
if nop, ok := resource.(NameOriginalProvider); ok {
if g.Match(paths.NormalizePathStringBasic(nop.NameOriginal())) {
return resource
}
}
}

return nil
}

Expand Down Expand Up @@ -135,6 +144,16 @@ func (r Resources) Match(pattern any) Resources {
matches = append(matches, resource)
}
}
if len(matches) == 0 {
// Fall back to the original name.
for _, resource := range r {
if nop, ok := resource.(NameOriginalProvider); ok {
if g.Match(strings.ToLower(nop.NameOriginal())) {
matches = append(matches, resource)
}
}
}
}
return matches
}

Expand Down

0 comments on commit 48eec2a

Please sign in to comment.