From 8496e59994b5e346f7739f9074515beeb87c8b79 Mon Sep 17 00:00:00 2001 From: Julian Colmenares Date: Thu, 21 Nov 2024 13:43:57 -0600 Subject: [PATCH] Dynamically assign header sizes to articles based on their hierarchy within sections --- layouts/partials/article/title.html | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/layouts/partials/article/title.html b/layouts/partials/article/title.html index 0e72a537..45b183cd 100644 --- a/layouts/partials/article/title.html +++ b/layouts/partials/article/title.html @@ -2,15 +2,34 @@ {{ $articleLink := ($.Scratch.Get "articleLink") }} {{ if (eq .Parent.Title .Title) }} -{{/* No title - For when the main title is the same as the first article */}} +{{/* No title - For when the main title is the same as the first article */}} {{ else }}
- {{ if .Data.Pages }} -

{{ .Title }}

- {{ else }} -

{{ .Title }}

+ + {{/* Inline depth calculation using range to iterate up to max depth */}} + {{ $depth := 0 }} + {{ $parent := .Parent }} + {{ $maxDepth := 10 }} + + {{/* Use a range to simulate a loop up to maxDepth */}} + {{ range seq 1 $maxDepth }} + {{ if and $parent (ne $parent.Title $.Site.Title) }} + {{ $depth = add $depth 1 }} + {{ $parent = $parent.Parent }} + {{ else }} + {{ break }} + {{ end }} {{ end }} + {{/* Set heading level based on depth, capping at 6 */}} + {{ $headingLevel := add $depth 1 }} + {{ if gt $headingLevel 6 }} + {{ $headingLevel = 6 }} + {{ end }} + + {{/* Output the heading tag based on calculated level */}} + {{ .Title }} + {{ partial "common/permalink" (dict "copyTitle" "article" "Page" . "slug" $slug "permalink" $articleLink) }}
{{ end }}