]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fall back to original name in Resources.GetMatch/Match
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 20 Feb 2024 15:08:18 +0000 (16:08 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 20 Feb 2024 15:47:07 +0000 (16:47 +0100)
Same as we do in .Get.

Fixes #12076

hugolib/pagebundler_test.go
resources/resource/resources.go

index 9f5255c747737bb02e0e909f5aa1cf10437b7685..e9686706d0f58c54f243e50c6d904dab77548e76 100644 (file)
@@ -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.|")
+}
index 9f298b7a6c6b769f1855d4cbf8972d458e7dad14..fb5f9a5ee1369420abe538c52e84af3c61fc215c 100644 (file)
@@ -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
 }
 
@@ -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
 }