]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Allow creating home pages from content adapters
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 15 Jun 2025 09:19:27 +0000 (11:19 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 7 Jul 2025 16:41:45 +0000 (18:41 +0200)
* Allow "" (empty string) or "/" to represent the home page path.
* Be a little more lenient about path validation.

hugolib/page__meta.go
hugolib/pagesfromdata/pagesfromgotmpl.go
hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go
resources/page/pagemeta/page_frontmatter.go

index 3c694ab41d11af6efee436b998b67c9a595d6bc9..5a27d1913cd58dcd8c994a427f90d1a756a20dfc 100644 (file)
@@ -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
        }
 
index 72909a40b1bb2ce16ead35a7401567d0b141e2e9..6b369567b028f9c1a8bee65724338e101df724d8 100644 (file)
@@ -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
 }
-
-//////////////
index db06fb4a45fd74b00e93963072142014a6173682..ffd0c5f5bf3efab5f425204ba51a5a4a04ac5f8b 100644 (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!|")
+}
index 9018acb713cb91a35ef91b448133f9b105b9efdf..5cbef56816ee1086c3d05b81ddfe79409a2c3e41 100644 (file)
@@ -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")
        }