Fix path resolution in hugo new
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 8 Nov 2021 10:50:51 +0000 (11:50 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 8 Nov 2021 13:10:46 +0000 (14:10 +0100)
With theme and project with content directories and command on the form `hugo new posts/test.md`.

Fixes #9129

create/content_test.go
hugolib/content_factory_test.go
hugolib/filesystems/basefs.go

index 6f0ed8c86cc3bddc305c3d0cb37b3be60c27fc22..2d68f76102958efe07d05699e27cda374910693e 100644 (file)
@@ -35,7 +35,7 @@ import (
 )
 
 // TODO(bep) clean this up. Export the test site builder in Hugolib or something.
-func TestNewContent(t *testing.T) {
+func TestNewContentFromFile(t *testing.T) {
        cases := []struct {
                name     string
                kind     string
index 50cc783f67d32f75d656bd703adc862a9bbd3a65..dc3b4fc9100c0df6cd5121573c0a3f042b2132bc 100644 (file)
@@ -57,4 +57,22 @@ Hello World.
                b.Assert(buf.String(), qt.Contains, `title: "Mypage"`)
        })
 
+       // Issue #9129
+       c.Run("Content in both project and theme", func(c *qt.C) {
+               b := newTestSitesBuilder(c)
+               b.WithConfigFile("toml", `
+theme = 'ipsum'                
+`)
+
+               themeDir := filepath.Join("themes", "ipsum")
+               b.WithSourceFile("content/posts/foo.txt", `Hello.`)
+               b.WithSourceFile(filepath.Join(themeDir, "content/posts/foo.txt"), `Hello.`)
+               b.CreateSites()
+               cf := NewContentFactory(b.H)
+               abs, err := cf.CreateContentPlaceHolder(filepath.FromSlash("posts/test.md"))
+               b.Assert(err, qt.IsNil)
+               b.Assert(abs, qt.Equals, filepath.FromSlash("content/posts/test.md"))
+
+       })
+
 }
index cfbd295ba5a30a98655ea3ce703429eb8216c47a..939d884598f6731820fde70f3eb5ee1799532594 100644 (file)
@@ -157,11 +157,14 @@ func (b *BaseFs) AbsProjectContentDir(filename string) (string, string) {
        if !isAbs {
                // A filename on the form "posts/mypage.md", put it inside
                // the first content folder, usually <workDir>/content.
-               // The Dirs are ordered with the most important last, so pick that.
+               // Pick the last project dir (which is probably the most important one).
                contentDirs := b.SourceFilesystems.Content.Dirs
-               firstContentDir := contentDirs[len(contentDirs)-1].Meta().Filename
-               return filename, filepath.Join(firstContentDir, filename)
-
+               for i := len(contentDirs) - 1; i >= 0; i-- {
+                       meta := contentDirs[i].Meta()
+                       if meta.Module == "project" {
+                               return filename, filepath.Join(meta.Filename, filename)
+                       }
+               }
        }
 
        return "", ""