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

@@ -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
}

View 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
}
//////////////

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!|")
}

View 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")
}