]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Filter out duplicate content resource files
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 30 Jan 2024 21:26:55 +0000 (22:26 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 31 Jan 2024 09:06:04 +0000 (10:06 +0100)
We do a slight normalisation of the content paths (lower case, replacing " " with "-") and remove andy language identifier before inserting them into the content tree.

This means that, given that that the default content language is `en`:

```
index.md
index.html
Foo Bar.txt
foo-bar.txt
foo-bar.en.txt
Foo-Bar.txt
```

The bundle above will be reduced to one content file with one resource (`foo-bar.txt`).

Before this commit, what version of the `foo-bar.txt` you ended up with was undeterministic. No  we pick the first determined by sort order.

Note that the sort order is stable, but we recommend avoiding situations like the above.

Closes #11946

hugofs/component_fs.go
hugolib/content_map_test.go

index c55f15957295c12bdfe9be0f1a0327b047fe8a6c..62d977ae889fbeb4a86696263635892a20c8251f 100644 (file)
@@ -147,13 +147,13 @@ func (f *componentFsDir) ReadDir(count int) ([]iofs.DirEntry, error) {
        })
 
        if f.fs.opts.Component == files.ComponentFolderContent {
-               // Finally filter out any duplicate content files, e.g. page.md and page.html.
+               // Finally filter out any duplicate content or resource files, e.g. page.md and page.html.
                n := 0
                seen := map[hstrings.Tuple]bool{}
                for _, fi := range fis {
                        fim := fi.(FileMetaInfo)
                        pi := fim.Meta().PathInfo
-                       keep := fim.IsDir() || !pi.IsContent()
+                       keep := fim.IsDir()
 
                        if !keep {
                                baseLang := hstrings.Tuple{First: pi.Base(), Second: fim.Meta().Lang}
index 7843ad2858908f8fb61f8e5414197b8634053990..c666e2a45f1034557698b2741ab86bad9264d441 100644 (file)
@@ -299,3 +299,30 @@ R: {{ with $r }}{{ .Content }}{{ end }}|
 
        b.AssertFileContent("public/index.html", "R: Data.")
 }
+
+// Issue #11946.
+func TestBundleResourcesGetDuplicateSortOrder(t *testing.T) {
+       files := `
+-- hugo.toml --
+baseURL = "https://example.com"
+-- content/bundle/index.md --
+-- content/bundle/data-1.txt --
+data-1.txt
+-- content/bundle/data 1.txt --
+data 1.txt
+-- content/bundle/Data 1.txt --
+Data 1.txt
+-- content/bundle/Data-1.txt --
+Data-1.txt
+-- layouts/index.html --
+{{ $bundle := site.GetPage "bundle" }}
+{{ $r := $bundle.Resources.Get "data-1.txt" }}
+R: {{ with $r }}{{ .Content }}{{ end }}|Len: {{ len $bundle.Resources }}|$
+
+`
+
+       for i := 0; i < 3; i++ {
+               b := Test(t, files)
+               b.AssertFileContent("public/index.html", "R: Data 1.txt|", "Len: 1|")
+       }
+}