]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Emit a warning that can be turned off when overwriting built-in .Params values
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 30 Jan 2024 09:07:28 +0000 (10:07 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 30 Jan 2024 19:12:03 +0000 (20:12 +0100)
Fixes #11941

common/constants/constants.go
hugolib/page__meta.go
hugolib/params_test.go

index e4f5a63a279ac067c5690274fea504564154a5a2..f8f057e053bb06e8afc6f1cc75c13e73e47ce46c 100644 (file)
 
 package constants
 
-// Error IDs.
+// Error/Warning IDs.
 // Do not change these values.
 const (
        // IDs for remote errors in tpl/data.
        ErrRemoteGetJSON = "error-remote-getjson"
        ErrRemoteGetCSV  = "error-remote-getcsv"
+
+       WarnFrontMatterParamsOverrides = "warning-frontmatter-params-overrides"
 )
 
 // Field/method names with special meaning.
index 179622d9b733e7c5e56b1e77ae83a6c3fc308861..e4cb2b83bc3a9effb8f607a51032590b833d21d7 100644 (file)
@@ -31,6 +31,7 @@ import (
 
        "github.com/gohugoio/hugo/source"
 
+       "github.com/gohugoio/hugo/common/constants"
        "github.com/gohugoio/hugo/common/hugo"
        "github.com/gohugoio/hugo/common/maps"
        "github.com/gohugoio/hugo/common/paths"
@@ -621,6 +622,9 @@ func (p *pageState) setMetaPostParams() error {
        }
 
        for k, v := range userParams {
+               if _, found := params[k]; found {
+                       p.s.Log.Warnidf(constants.WarnFrontMatterParamsOverrides, "Hugo front matter key %q is overridden in params section.", k)
+               }
                params[strings.ToLower(k)] = v
        }
 
index f80f14035fa763dc48113928f8ec7652397a8c87..6f890b43b78ec96da46c523b4fa1424bcaaf05c7 100644 (file)
@@ -133,6 +133,28 @@ RegularPages: {{ range site.RegularPages }}{{ .Path }}|{{ .RelPermalink }}|{{ .T
        )
 }
 
+func TestFrontMatterTitleOverrideWarn(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+baseURL = "https://example.org/"
+disableKinds = ["taxonomy", "term"]
+-- content/p1.md --
+---
+title: "My title"
+params:
+  title: "My title from params"
+---
+
+
+`
+
+       b := Test(t, files, TestOptWarn())
+
+       b.AssertLogContains("ARN  Hugo front matter key \"title\" is overridden in params section", "You can suppress this warning")
+}
+
 func TestFrontMatterParamsLangNoCascade(t *testing.T) {
        t.Parallel()