aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJoe Mooring <[email protected]>2024-10-06 11:02:19 -0700
committerBjørn Erik Pedersen <[email protected]>2024-10-06 20:35:25 +0200
commit3f6830914837f6a0c253cb104ac8b091a2080acc (patch)
tree09439eb3ffcb32bb351e779cc21b77db8ac67e69
parentab03588db95c885e30173fb23c3b69d3ed528201 (diff)
downloadhugo-3f6830914837f6a0c253cb104ac8b091a2080acc.tar.gz
hugo-3f6830914837f6a0c253cb104ac8b091a2080acc.zip
resources/page: Treat null dates as zero dates
Closes #12906
-rw-r--r--resources/page/pagemeta/page_frontmatter.go2
-rw-r--r--resources/page/pagemeta/pagemeta_integration_test.go78
2 files changed, 62 insertions, 18 deletions
diff --git a/resources/page/pagemeta/page_frontmatter.go b/resources/page/pagemeta/page_frontmatter.go
index 686f0d44d..d5d612609 100644
--- a/resources/page/pagemeta/page_frontmatter.go
+++ b/resources/page/pagemeta/page_frontmatter.go
@@ -728,7 +728,7 @@ func (f *frontmatterFieldHandlers) newDateFieldHandler(key string, setter func(d
return func(d *FrontMatterDescriptor) (bool, error) {
v, found := d.PageConfig.Params[key]
- if !found || v == "" {
+ if !found || v == "" || v == nil {
return false, nil
}
diff --git a/resources/page/pagemeta/pagemeta_integration_test.go b/resources/page/pagemeta/pagemeta_integration_test.go
index 9d8f1b92f..d0c550b2e 100644
--- a/resources/page/pagemeta/pagemeta_integration_test.go
+++ b/resources/page/pagemeta/pagemeta_integration_test.go
@@ -52,47 +52,91 @@ func TestDateValidation(t *testing.T) {
-- hugo.toml --
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
-- content/_index.md --
-+++
-date = DATE
-+++
+FRONT_MATTER
-- layouts/index.html --
{{ .Date.UTC.Format "2006-01-02" }}
--
`
errorMsg := `ERROR the "date" front matter field is not a parsable date`
- // Valid (TOML)
- f := strings.ReplaceAll(files, "DATE", "2024-10-01")
+ // TOML: unquoted date/time (valid)
+ f := strings.ReplaceAll(files, "FRONT_MATTER", `
++++
+date = 2024-10-01
++++
+ `)
b := hugolib.Test(t, f)
b.AssertFileContent("public/index.html", "2024-10-01")
- // Valid (string)
- f = strings.ReplaceAll(files, "DATE", `"2024-10-01"`)
+ // TOML: string (valid)
+ f = strings.ReplaceAll(files, "FRONT_MATTER", `
++++
+date = "2024-10-01"
++++
+ `)
b = hugolib.Test(t, f)
b.AssertFileContent("public/index.html", "2024-10-01")
- // Valid (empty string)
- f = strings.ReplaceAll(files, "DATE", `""`)
+ // TOML: empty string (valid)
+ f = strings.ReplaceAll(files, "FRONT_MATTER", `
++++
+date = ""
++++
+ `)
b = hugolib.Test(t, f)
b.AssertFileContent("public/index.html", "0001-01-01")
- // Valid (int)
- f = strings.ReplaceAll(files, "DATE", "0")
+ // TOML: int (valid)
+ f = strings.ReplaceAll(files, "FRONT_MATTER", `
++++
+date = 0
++++
+ `)
b = hugolib.Test(t, f)
b.AssertFileContent("public/index.html", "1970-01-01")
- // Invalid (string)
- f = strings.ReplaceAll(files, "DATE", `"2024-42-42"`)
+ // TOML: string (invalid)
+ f = strings.ReplaceAll(files, "FRONT_MATTER", `
++++
+date = "2024-42-42"
++++
+ `)
b, _ = hugolib.TestE(t, f)
b.AssertLogContains(errorMsg)
- // Invalid (bool)
- f = strings.ReplaceAll(files, "DATE", "true")
+ // TOML: bool (invalid)
+ f = strings.ReplaceAll(files, "FRONT_MATTER", `
++++
+date = true
++++
+ `)
b, _ = hugolib.TestE(t, f)
b.AssertLogContains(errorMsg)
- // Invalid (float)
- f = strings.ReplaceAll(files, "DATE", "6.7")
+ // TOML: float (invalid)
+ f = strings.ReplaceAll(files, "FRONT_MATTER", `
++++
+date = 6.7
++++
+ `)
b, _ = hugolib.TestE(t, f)
b.AssertLogContains(errorMsg)
+
+ // JSON: null (valid)
+ f = strings.ReplaceAll(files, "FRONT_MATTER", `
+{
+ "date": null
+}
+ `)
+ b = hugolib.Test(t, f)
+ b.AssertFileContent("public/index.html", "0001-01-01")
+
+ // YAML: null (valid)
+ f = strings.ReplaceAll(files, "FRONT_MATTER", `
+---
+date:
+---
+ `)
+ b = hugolib.Test(t, f)
+ b.AssertFileContent("public/index.html", "0001-01-01")
}