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
return "", err
}
- if err := pd.Validate(true); err != nil {
+ if err := pd.Init(true); err != nil {
return "", err
}
return bi, nil
}
-
-//////////////
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) {
})
}
-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)
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!|")
+}
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")
}
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")
}