From b45ab833d5400a1a2ff8725b3f5d12c27c18881c Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Fri, 25 Apr 2025 12:55:03 +0800 Subject: [PATCH] :sparkles: Feat: add `hugo.mainSections` param support (#590) https://gohugo.io/methods/site/mainsections/ --- hugo.toml | 2 ++ layouts/_default/_markup/render-heading.html | 3 ++- layouts/partials/init/global.html | 5 +++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/hugo.toml b/hugo.toml index 07dd1a9f..c8271ef8 100644 --- a/hugo.toml +++ b/hugo.toml @@ -34,6 +34,8 @@ enableRobotsTXT = true enableGitInfo = true # whether to use emoji code enableEmoji = true +# the main sections of a site +mainSections = [] # ------------------------------------------------------------------------------------- # Menu Configuration diff --git a/layouts/_default/_markup/render-heading.html b/layouts/_default/_markup/render-heading.html index 8fa8c52e..667aee6a 100644 --- a/layouts/_default/_markup/render-heading.html +++ b/layouts/_default/_markup/render-heading.html @@ -7,7 +7,8 @@ {{- end -}} {{- $headingConfig := $params.heading -}} {{- /* Only enable in main section pages */ -}} -{{- $onlyMainSection := cond $headingConfig.number.onlyMainSection (eq .Page.Type "posts") true -}} +{{- $intersect := (slice .Page.Section .Page.Type) | intersect site.MainSections -}} +{{- $onlyMainSection := cond $headingConfig.number.onlyMainSection ((gt (len $intersect) 0) | and (eq .Page.Kind "page")) true -}} {{- $ordered := $toc.ordered | and $headingConfig.number.enable | and $onlyMainSection -}} {{- $h1Format := $headingConfig.number.format.h1 | default "{title}" -}} {{- $h2Format := $headingConfig.number.format.h2 | default "{h2} {title}" -}} diff --git a/layouts/partials/init/global.html b/layouts/partials/init/global.html index 14786098..46925cc0 100644 --- a/layouts/partials/init/global.html +++ b/layouts/partials/init/global.html @@ -4,8 +4,9 @@ {{- $homeRelPermalink = strings.TrimSuffix "offline/" $homeRelPermalink -}} {{- .Scratch.Set "homeRelPermalink" $homeRelPermalink -}} -{{- /* Set pages of main section */ -}} -{{- $pages := where .Site.RegularPages "Type" "posts" -}} +{{- /* Set pages of main sections */ -}} +{{- $pages := where .Site.RegularPages "Section" "in" .Site.MainSections -}} +{{- $pages = $pages | union (where .Site.RegularPages "Type" "in" .Site.MainSections) -}} {{- .Scratch.Set "mainSectionPages" $pages -}} {{- /* Select the scope named `public_repo` to generate personal access token */ -}}