Skip to content

Commit 34a61e4

Browse files
committed
Fix front matter date location when value gets inherited from other dates
Fixes gohugoio#12236
1 parent 4f92f94 commit 34a61e4

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

resources/page/pagemeta/page_frontmatter.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -421,18 +421,22 @@ func (f *frontmatterFieldHandlers) newDateFieldHandler(key string, setter func(d
421421
return false, nil
422422
}
423423

424-
date, err := htime.ToTimeInDefaultLocationE(v, d.Location)
425-
if err != nil {
426-
return false, nil
424+
var date time.Time
425+
if vt, ok := v.(time.Time); ok && vt.Location() == d.Location {
426+
date = vt
427+
} else {
428+
var err error
429+
date, err = htime.ToTimeInDefaultLocationE(v, d.Location)
430+
if err != nil {
431+
return false, nil
432+
}
433+
d.PageConfig.Params[key] = date
427434
}
428435

429436
// We map several date keys to one, so, for example,
430437
// "expirydate", "unpublishdate" will all set .ExpiryDate (first found).
431438
setter(d, date)
432439

433-
// This is the params key as set in front matter.
434-
d.PageConfig.Params[key] = date
435-
436440
return true, nil
437441
}
438442
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2024 The Hugo Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package pagemeta_test
15+
16+
import (
17+
"testing"
18+
19+
"github.com/gohugoio/hugo/hugolib"
20+
)
21+
22+
func TestLastModEq(t *testing.T) {
23+
files := `
24+
-- hugo.toml --
25+
timeZone = "Europe/London"
26+
-- content/p1.md --
27+
---
28+
title: p1
29+
date: 2024-03-13T06:00:00
30+
---
31+
-- layouts/_default/single.html --
32+
Date: {{ .Date }}
33+
Lastmod: {{ .Lastmod }}
34+
Eq: {{ eq .Date .Lastmod }}
35+
36+
`
37+
38+
b := hugolib.Test(t, files)
39+
40+
b.AssertFileContent("public/p1/index.html", `
41+
Date: 2024-03-13 06:00:00 +0000 GMT
42+
Lastmod: 2024-03-13 06:00:00 +0000 GMT
43+
Eq: true
44+
`)
45+
}

0 commit comments

Comments
 (0)