Skip to content

Commit

Permalink
feat: add title utility to get page or section title (#325)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
imfing authored Mar 19, 2024
1 parent 29e70c8 commit 935ff7f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
4 changes: 2 additions & 2 deletions layouts/partials/breadcrumb.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
{{- range .Ancestors.Reverse }}
{{- if not .IsHome }}
<div class="hx-whitespace-nowrap hx-transition-colors hx-min-w-[24px] hx-overflow-hidden hx-text-ellipsis hover:hx-text-gray-900 dark:hover:hx-text-gray-100">
<a href="{{ .Permalink }}">{{ .LinkTitle }}</a>
<a href="{{ .Permalink }}">{{- partial "utils/title" . -}}</a>
</div>
{{- partial "utils/icon.html" (dict "name" "chevron-right" "attributes" "class=\"hx-w-3.5 hx-shrink-0 rtl:-hx-rotate-180\"") -}}
{{ end -}}
{{ end -}}
<div class="hx-whitespace-nowrap hx-transition-colors hx-font-medium hx-text-gray-700 contrast-more:hx-font-bold contrast-more:hx-text-current dark:hx-text-gray-100 contrast-more:dark:hx-text-current">
{{- .LinkTitle -}}
{{- partial "utils/title" . -}}
</div>
</div>
10 changes: 6 additions & 4 deletions layouts/partials/components/pager.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,24 @@
{{- if or $prev $next -}}
<div class="hx-mb-8 hx-flex hx-items-center hx-border-t hx-pt-8 dark:hx-border-neutral-800 contrast-more:hx-border-neutral-400 dark:contrast-more:hx-border-neutral-400 print:hx-hidden">
{{- if $prev -}}
{{- $linkTitle := partial "utils/title" $prev -}}
<a
href="{{ $prev.RelPermalink }}"
title="{{ $prev.LinkTitle }}"
title="{{ $linkTitle }}"
class="hx-flex hx-max-w-[50%] hx-items-center hx-gap-1 hx-py-4 hx-text-base hx-font-medium hx-text-gray-600 hx-transition-colors [word-break:break-word] hover:hx-text-primary-600 dark:hx-text-gray-300 md:hx-text-lg ltr:hx-pr-4 rtl:hx-pl-4"
>
{{- 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 -}}
</a>
{{- end -}}
{{- if $next -}}
{{- $linkTitle := partial "utils/title" $next -}}
<a
href="{{ $next.RelPermalink }}"
title="{{ $next.LinkTitle }}"
title="{{ $linkTitle }}"
class="hx-flex hx-max-w-[50%] hx-items-center hx-gap-1 hx-py-4 hx-text-base hx-font-medium hx-text-gray-600 hx-transition-colors [word-break:break-word] hover:hx-text-primary-600 dark:hx-text-gray-300 md:hx-text-lg ltr:hx-ml-auto ltr:hx-pl-4 ltr:hx-text-right rtl:hx-mr-auto rtl:hx-pr-4 rtl:hx-text-left"
>
{{- $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\"") -}}
</a>
{{- end -}}
Expand Down
9 changes: 5 additions & 4 deletions layouts/partials/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@
{{- range $items.ByWeight }}
{{- if .Params.sidebar.separator -}}
<li class="[word-break:break-word] hx-mt-5 hx-mb-2 hx-px-2 hx-py-1.5 hx-text-sm hx-font-semibold hx-text-gray-900 first:hx-mt-0 dark:hx-text-gray-100">
<span class="hx-cursor-default">{{ .LinkTitle }}</span>
<span class="hx-cursor-default">{{ partial "utils/title" . }}</span>
</li>
{{- else -}}
{{- $active := eq $pageURL .RelPermalink -}}
{{- $shouldOpen := or (.Params.sidebar.open) (.IsAncestor $page) $active | default true }}
<li class="{{ if $shouldOpen }}open{{ end }}">
{{- 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 -}}
Expand All @@ -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" . -}}
<li class="hx-flex hx-flex-col {{ if $shouldOpen }}open{{ end }}">
{{- 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 }}
Expand Down
19 changes: 19 additions & 0 deletions layouts/partials/utils/title.html
Original file line number Diff line number Diff line change
@@ -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 -}}

0 comments on commit 935ff7f

Please sign in to comment.