Allow creating home pages from content adapters

* Allow "" (empty string) or "/" to represent the home page path.
* Be a little more lenient about path validation.
This commit is contained in:
Bjørn Erik Pedersen
2025-06-15 11:19:27 +02:00
parent 94e2c276a8
commit bba6996e15
4 changed files with 29 additions and 44 deletions

View File

@@ -195,14 +195,7 @@ baseURL = "https://example.com"
b, err := hugolib.TestE(t, files)
b.Assert(err, qt.IsNotNil)
b.Assert(err.Error(), qt.Contains, "_content.gotmpl:1:4")
b.Assert(err.Error(), qt.Contains, "error calling AddPage: path not set")
})
t.Run("AddPage, path starting with slash", func(t *testing.T) {
files := strings.ReplaceAll(filesTemplate, "DICT", `(dict "kind" "page" "title" "p1" "path" "/foo")`)
b, err := hugolib.TestE(t, files)
b.Assert(err, qt.IsNotNil)
b.Assert(err.Error(), qt.Contains, `path "/foo" must not start with a /`)
b.Assert(err.Error(), qt.Contains, "error calling AddPage: empty path is reserved for the home page")
})
t.Run("AddPage, lang set", func(t *testing.T) {
@@ -233,23 +226,6 @@ baseURL = "https://example.com"
})
}
func TestPagesFromGoTmplAddResourceErrors(t *testing.T) {
filesTemplate := `
-- hugo.toml --
disableKinds = ["taxonomy", "term", "rss", "sitemap"]
baseURL = "https://example.com"
-- content/docs/_content.gotmpl --
{{ $.AddResource DICT }}
`
t.Run("missing Path", func(t *testing.T) {
files := strings.ReplaceAll(filesTemplate, "DICT", `(dict "name" "r1")`)
b, err := hugolib.TestE(t, files)
b.Assert(err, qt.IsNotNil)
b.Assert(err.Error(), qt.Contains, "error calling AddResource: path not set")
})
}
func TestPagesFromGoTmplEditGoTmpl(t *testing.T) {
t.Parallel()
b := hugolib.TestRunning(t, filesPagesFromDataTempleBasic)
@@ -915,3 +891,21 @@ Title: {{ .Title }}|Content: {{ .Content }}|
b.AssertFileContent("public/s1/index.html", "Title: baz|")
}
func TestPagesFromGoTmplHome(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ["taxonomy", "term", "rss", "sitemap"]
baseURL = "https://example.com"
-- layouts/all.html --
{{ .Kind }}: {{ .Title }}|
-- content/_content.gotmpl --
{{ $.AddPage (dict "title" "My Home!" "kind" "home" ) }}
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/index.html", "home: My Home!|")
}