diff --git a/hugolib/page__meta.go b/hugolib/page__meta.go index 3c694ab41..5a27d1913 100644 --- a/hugolib/page__meta.go +++ b/hugolib/page__meta.go @@ -675,7 +675,7 @@ params: params["iscjklanguage"] = pcfg.IsCJKLanguage - if err := pcfg.Validate(false); err != nil { + if err := pcfg.Init(false); err != nil { return err } diff --git a/hugolib/pagesfromdata/pagesfromgotmpl.go b/hugolib/pagesfromdata/pagesfromgotmpl.go index 72909a40b..6b369567b 100644 --- a/hugolib/pagesfromdata/pagesfromgotmpl.go +++ b/hugolib/pagesfromdata/pagesfromgotmpl.go @@ -68,12 +68,9 @@ func (p *pagesFromDataTemplateContext) toPathMap(v any) (string, map[string]any, if err != nil { return "", nil, err } - pathv, ok := m["path"] - if !ok { - return "", nil, fmt.Errorf("path not set") - } - path, err := cast.ToStringE(pathv) - if err != nil || path == "" { + + path, err := cast.ToStringE(m["path"]) + if err != nil { return "", nil, fmt.Errorf("invalid path %q", path) } return path, m, nil @@ -99,7 +96,7 @@ func (p *pagesFromDataTemplateContext) AddPage(v any) (string, error) { return "", err } - if err := pd.Validate(true); err != nil { + if err := pd.Init(true); err != nil { return "", err } @@ -336,5 +333,3 @@ func (p *PagesFromTemplate) Execute(ctx context.Context) (BuildInfo, error) { return bi, nil } - -////////////// diff --git a/hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go b/hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go index db06fb4a4..ffd0c5f5b 100644 --- a/hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go +++ b/hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go @@ -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!|") +} diff --git a/resources/page/pagemeta/page_frontmatter.go b/resources/page/pagemeta/page_frontmatter.go index 9018acb71..5cbef5681 100644 --- a/resources/page/pagemeta/page_frontmatter.go +++ b/resources/page/pagemeta/page_frontmatter.go @@ -149,13 +149,12 @@ var DefaultPageConfig = PageConfig{ Build: DefaultBuildConfig, } -func (p *PageConfig) Validate(pagesFromData bool) error { +func (p *PageConfig) Init(pagesFromData bool) error { if pagesFromData { - if p.Path == "" { - return errors.New("path must be set") - } - if strings.HasPrefix(p.Path, "/") { - return fmt.Errorf("path %q must not start with a /", p.Path) + p.Path = strings.TrimPrefix(p.Path, "/") + + if p.Path == "" && p.Kind != kinds.KindHome { + return fmt.Errorf("empty path is reserved for the home page") } if p.Lang != "" { return errors.New("lang must not be set") @@ -295,9 +294,6 @@ type ResourceConfig struct { } func (rc *ResourceConfig) Validate() error { - if rc.Path == "" { - return errors.New("path must be set") - } if rc.Content.Markup != "" { return errors.New("markup must not be set, use mediaType") }