From c65852d88276f36fa111ed6ed1a32c3f81010196 Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Wed, 19 Aug 2026 12:29:36 +0800 Subject: [PATCH] fix(layouts): replace deprecated .Site.BuildDrafts with HUGO_PARAMS_MENU_DRAFT env var Use getenv "HUGO_PARAMS_MENU_DRAFT" to control draft menu item visibility, replacing the deprecated .Site.BuildDrafts (Hugo v0.156.0). (#702) Also refactors menu range loops to use `continue` for reduced nesting. --- hugo.toml | 3 +- layouts/_partials/base/header.html | 196 +++++++++++++++-------------- layouts/_partials/init/index.html | 2 +- 3 files changed, 106 insertions(+), 95 deletions(-) diff --git a/hugo.toml b/hugo.toml index be41e7cd..8bdd1bc6 100644 --- a/hugo.toml +++ b/hugo.toml @@ -99,7 +99,8 @@ weight = 1 [menus.main.params] # Add CSS class to a specific menu item. class = "" -# Whether set as a draft menu item whose function is similar to a draft post/page. +# Whether set as a draft menu item, hidden by default. +# Visible when env var is set: HUGO_PARAMS_MENU_DRAFT=true hugo server draft = false # Add FontAwesome icon to a specific menu item. icon = "fa-solid fa-archive" diff --git a/layouts/_partials/base/header.html b/layouts/_partials/base/header.html index abb1f44b..f02f564c 100644 --- a/layouts/_partials/base/header.html +++ b/layouts/_partials/base/header.html @@ -10,7 +10,13 @@ Called from: layouts/baseof.html. */ -}} -{{- $buildDrafts := .Page.Draft -}} +{{- /* + Whether to show draft menu items in the navigation. + Controlled via env var at build time: HUGO_PARAMS_MENU_DRAFT=true hugo server + Replaces the deprecated .Site.BuildDrafts (Hugo v0.156.0). + See: https://discourse.gohugo.io/t/deprecations-in-v0-156-0/56732/11 +*/ -}} +{{- $menuDraft := getenv "HUGO_PARAMS_MENU_DRAFT" -}} {{- $relLangURL := relLangURL "" -}} {{- /* Desktop header */ -}} @@ -57,51 +63,53 @@ {{- with .Page -}} {{- $url = .RelPermalink -}} {{- end -}} - {{- if (ne .Params.Draft true) | or $buildDrafts | and (ne .Params.Type "mobile") -}} - + {{- if (eq .Params.Type "mobile") | or (and (eq .Params.Draft true) (ne $menuDraft "true")) -}} + {{ continue }} {{- end -}} + {{- end -}} {{- if .Site.Menus.main -}} @@ -220,51 +228,12 @@ {{- with .Page -}} {{- $url = .RelPermalink -}} {{- end -}} - {{- if (ne .Params.Draft true) | or $buildDrafts | and (ne .Params.Type "desktop") -}} - - {{- end -}} + {{ dict "Class" "dropdown-icon fa-solid fa-chevron-right" | partial "plugin/icon.html" }} + + + {{- else -}} + + {{- with .Pre -}}{{- . | safeHTML }} {{ end }} + {{- with .Params.Icon -}}{{ dict "Class" (printf "%v fa-sm" .) | partial "plugin/icon.html" }} {{ end }} + {{- .Name }} + {{- with .Post }} {{ . | safeHTML -}}{{- end -}} + + {{- end -}} + {{- end -}}