From 935ff7f719b0ee9ae521f4bd55cdcb73f6c21234 Mon Sep 17 00:00:00 2001 From: Xin Date: Tue, 19 Mar 2024 22:16:10 +0000 Subject: [PATCH] feat: add title utility to get page or section title (#325) * feat: add title utility to get page or section title * with graceful fallback to using the directory or filename * chore: update link titles in breadcrumb, pager, and sidebar --- layouts/partials/breadcrumb.html | 4 ++-- layouts/partials/components/pager.html | 10 ++++++---- layouts/partials/sidebar.html | 9 +++++---- layouts/partials/utils/title.html | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 layouts/partials/utils/title.html diff --git a/layouts/partials/breadcrumb.html b/layouts/partials/breadcrumb.html index 5b1042ed..f3852754 100644 --- a/layouts/partials/breadcrumb.html +++ b/layouts/partials/breadcrumb.html @@ -2,12 +2,12 @@ {{- range .Ancestors.Reverse }} {{- if not .IsHome }}
- {{ .LinkTitle }} + {{- partial "utils/title" . -}}
{{- partial "utils/icon.html" (dict "name" "chevron-right" "attributes" "class=\"hx-w-3.5 hx-shrink-0 rtl:-hx-rotate-180\"") -}} {{ end -}} {{ end -}}
- {{- .LinkTitle -}} + {{- partial "utils/title" . -}}
diff --git a/layouts/partials/components/pager.html b/layouts/partials/components/pager.html index cae58f5b..1849b208 100644 --- a/layouts/partials/components/pager.html +++ b/layouts/partials/components/pager.html @@ -28,22 +28,24 @@ {{- if or $prev $next -}}
{{- if $prev -}} + {{- $linkTitle := partial "utils/title" $prev -}} {{- partial "utils/icon.html" (dict "name" "chevron-right" "attributes" "class=\"hx-inline hx-h-5 hx-shrink-0 ltr:hx-rotate-180\"") -}} - {{- $prev.LinkTitle -}} + {{- $linkTitle -}} {{- end -}} {{- if $next -}} + {{- $linkTitle := partial "utils/title" $next -}} - {{- $next.LinkTitle -}} + {{- $linkTitle -}} {{- partial "utils/icon.html" (dict "name" "chevron-right" "attributes" "class=\"hx-inline hx-h-5 hx-shrink-0 rtl:-hx-rotate-180\"") -}} {{- end -}} diff --git a/layouts/partials/sidebar.html b/layouts/partials/sidebar.html index 65e154c5..78ebf63a 100644 --- a/layouts/partials/sidebar.html +++ b/layouts/partials/sidebar.html @@ -78,13 +78,14 @@ {{- range $items.ByWeight }} {{- if .Params.sidebar.separator -}}
  • - {{ .LinkTitle }} + {{ partial "utils/title" . }}
  • {{- else -}} {{- $active := eq $pageURL .RelPermalink -}} {{- $shouldOpen := or (.Params.sidebar.open) (.IsAncestor $page) $active | default true }}
  • - {{- template "sidebar-item-link" dict "context" . "active" $active "title" .LinkTitle "link" .RelPermalink -}} + {{- $linkTitle := partial "utils/title" . -}} + {{- template "sidebar-item-link" dict "context" . "active" $active "title" $linkTitle "link" .RelPermalink -}} {{- if and $toc $active -}} {{- template "sidebar-toc" dict "page" . -}} {{- end -}} @@ -98,9 +99,9 @@ {{- range $items.ByWeight }} {{- $active := eq $pageURL .RelPermalink -}} {{- $shouldOpen := or (.Params.sidebar.open) (.IsAncestor $page) $active | default true }} - {{- $title := .LinkTitle | default .File.BaseFileName -}} + {{- $linkTitle := partial "utils/title" . -}}
  • - {{- template "sidebar-item-link" dict "context" . "active" $active "title" $title "link" .RelPermalink -}} + {{- template "sidebar-item-link" dict "context" . "active" $active "title" $linkTitle "link" .RelPermalink -}} {{- if and $toc $active -}} {{ template "sidebar-toc" dict "page" . }} {{- end }} diff --git a/layouts/partials/utils/title.html b/layouts/partials/utils/title.html new file mode 100644 index 00000000..0ab0afae --- /dev/null +++ b/layouts/partials/utils/title.html @@ -0,0 +1,19 @@ +{{/* + This utility is used to retrieve the title of a page or section. + If no title is set, it falls back to using the directory or file name. + + Based on https://github.com/thegeeklab/hugo-geekdoc/blob/v0.44.0/layouts/partials/utils/title.html +*/}} +{{- $title := "" }} + +{{ if .LinkTitle }} + {{ $title = .LinkTitle }} +{{ else if .Title }} + {{ $title = .Title }} +{{ else if and .IsSection .File }} + {{ $title = path.Base .File.Dir | humanize | title }} +{{ else if and .IsPage .File }} + {{ $title = .File.BaseFileName | humanize | title }} +{{ end }} + +{{ return $title -}}