feat: allow disable toc on page frontmatter

refactor: move pager to partial/components

chore: add head-css and favicons

- support favicons and page descriptions
- dynamic title based on page
- add custom head-end hook

feat: add opengraph to head

fix: sidebar not hiding when resizing
This commit is contained in:
Xin 2023-08-09 22:57:43 +01:00
parent ad3c607623
commit 750e566d24
11 changed files with 96 additions and 40 deletions

View File

@ -27,7 +27,7 @@
</div>
{{ partial "components/last-updated.html" . }}
{{ .Scratch.Set "reversePagination" true }}
{{ partial "pager.html" . }}
{{ partial "components/pager.html" . }}
</main>
</article>
</div>

View File

@ -10,7 +10,7 @@
{{ .Content }}
</div>
<div class="mt-16"></div>
{{ partial "pager.html" . }}
{{ partial "components/pager.html" . }}
</main>
</article>
</div>

View File

@ -10,7 +10,7 @@
{{ .Content }}
</div>
{{ partial "components/last-updated.html" . }}
{{ partial "pager.html" . }}
{{ partial "components/pager.html" . }}
</main>
</article>
</div>

View File

View File

@ -0,0 +1,10 @@
<link rel="shortcut icon" href="{{ "favicons/favicon.ico" | relURL }}" />
<link rel="apple-touch-icon" href="{{ "favicons/apple-touch-icon-180x180.png" | relURL }}" sizes="180x180" />
<link rel="icon" type="image/png" href="{{ "favicons/favicon-16x16.png" | relURL }}" sizes="16x16" />
<link rel="icon" type="image/png" href="{{ "favicons/favicon-32x32.png" | relURL }}" sizes="32x32" />
<link rel="icon" type="image/png" href="{{ "favicons/android-36x36.png" | relURL }}" sizes="36x36" />
<link rel="icon" type="image/png" href="{{ "favicons/android-48x48.png" | relURL }}" sizes="48x48" />
<link rel="icon" type="image/png" href="{{ "favicons/android-72x72.png" | relURL }}" sizes="72x72" />
<link rel="icon" type="image/png" href="{{ "favicons/android-96x96.png" | relURL }}" sizes="96x96" />
<link rel="icon" type="image/png" href="{{ "favicons/android-144x144.png" | relURL }}" sizes="144x144" />
<link rel="icon" type="image/png" href="{{ "favicons/android-192x192.png" | relURL }}" sizes="192x192" />

View File

@ -0,0 +1,10 @@
{{ $options := dict "inlineImports" true }}
{{ $styles := resources.Get "css/styles.css" }}
{{ $styles = $styles | resources.PostCSS $options }}
{{ if not hugo.IsProduction }}
<link href="{{ $styles.RelPermalink }}" rel="stylesheet" />
{{ else }}
{{ $styles = $styles | minify | fingerprint | resources.PostProcess }}
<link rel="preload" href="{{ $styles.RelPermalink }}" as="style" />
<link href="{{ $styles.RelPermalink }}" rel="stylesheet" integrity="{{ $styles.Data.integrity }}" />
{{ end }}

View File

@ -1,18 +1,18 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
{{ partialCached "favicons.html" . }}
<title>
{{ .Title }}
{{- if .IsHome -}}
{{ .Site.Title -}}
{{ else -}}
{{ with .Title }}{{ . }} {{ end -}}
{{ .Site.Title -}}
{{ end -}}
</title>
<meta name="description" content="{{ .Description }}" />
{{ $options := dict "inlineImports" true }}
{{ $styles := resources.Get "css/styles.css" }}
{{ $styles = $styles | resources.PostCSS $options }}
{{ if hugo.IsProduction }}
{{ $styles = $styles | minify | fingerprint | resources.PostProcess }}
{{ end }}
<link href="{{ $styles.RelPermalink }}" rel="stylesheet" />
<meta name="description" content="{{ partial "utils/page-description.html" . }}" />
{{ partial "opengraph.html" . }}
{{ partial "head-css.html" . }}
<script>
/* Initialize light/dark mode */
if (localStorage.getItem("color-theme") === "dark" || (!("color-theme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches)) {
@ -21,4 +21,6 @@
document.documentElement.classList.remove("dark");
}
</script>
{{ partial "custom/head-end.html" . }}
</head>

View File

@ -0,0 +1,31 @@
{{/* From https://github.com/gohugoio/gohugoioTheme/blob/master/layouts/partials/opengraph/opengraph.html */}}
<meta property="og:title" content="{{ .Title }}" />
<meta property="og:description" content="{{ with .Description }}{{ . }}{{ else }}{{if .IsPage}}{{ .Summary }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}{{ end }}" />
<meta property="og:type" content="{{ if .IsPage }}article{{ else }}website{{ end }}" />
<meta property="og:url" content="{{ .Permalink }}" />
{{- with $.Params.images -}}
{{- range first 6 . }}<meta property="og:image" content="{{ . | absURL }}" />{{ end -}}
{{- else -}}
{{- $featured := "" }}
{{- with $featured -}}
<meta property="og:image" content="{{ $featured.Permalink }}"/>
{{- else -}}
{{- with $.Site.Params.images }}<meta property="og:image" content="{{ index . 0 | absURL }}"/>{{ end -}}
{{- end -}}
{{- end -}}
{{- if .IsPage }}
{{- $iso8601 := "2006-01-02T15:04:05-07:00" -}}
<meta property="article:section" content="{{ .Section }}" />
{{ with .PublishDate }}<meta property="article:published_time" {{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }} />{{ end }}
{{ with .Lastmod }}<meta property="article:modified_time" {{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }} />{{ end }}
{{- end -}}
{{- with .Params.audio }}<meta property="og:audio" content="{{ . }}" />{{ end }}
{{- with .Params.locale }}<meta property="og:locale" content="{{ . }}" />{{ end }}
{{- with .Site.Params.title }}<meta property="og:site_name" content="{{ . }}" />{{ end }}
{{- with .Params.videos }}{{- range . }}
<meta property="og:video" content="{{ . | absURL }}" />
{{ end }}{{ end }}

View File

@ -1,12 +1,13 @@
{{ $context := .context }}
{{ $disableSidebar := .disableSidebar | default false }}
{{ $sidebarClass := cond $disableSidebar "md:hidden xl:block" "md:sticky" }}
{{ $navRoot := cond (eq site.Home.Type "docs") site.Home $context.FirstSection }}
{{ $navPages := union $navRoot.RegularPages $navRoot.Sections }}
{{ $pageURL := $context.RelPermalink }}
<aside class="sidebar-container flex flex-col print:hidden md:top-16 md:shrink-0 md:w-64 md:sticky md:self-start max-md:[transform:translate3d(0,-100%,0)]">
<aside class="sidebar-container flex flex-col print:hidden md:top-16 md:shrink-0 md:w-64 md:self-start max-md:[transform:translate3d(0,-100%,0)] {{ $sidebarClass }}">
<!-- Search bar on small screen -->
<div class="px-4 pt-4 md:hidden">
{{ partial "search.html" }}

View File

@ -1,35 +1,37 @@
{{/* Table of Contents */}}
{{/* TODO: toc should be able to get disabled through page frontmatter */}}
{{/* TODO: toc bottom part should be able to hide */}}
<div class="order-last hidden w-64 shrink-0 xl:block print:hidden px-4" aria-label="table of contents">
<div class="sticky top-16 overflow-y-auto pr-4 pt-6 text-sm [hyphens:auto] max-h-[calc(100vh-4rem-env(safe-area-inset-bottom))] ltr:-mr-4 rtl:-ml-4">
{{ with .Fragments.Headings }}
<p class="mb-4 font-semibold tracking-tight">On This Page</p>
{{ range . }}
<ul>
{{ with .Headings }}
{{ template "side-toc-subheading" (dict "headings" . "level" 0) }}
{{ end }}
</ul>
{{- $toc := .Params.toc | default true -}}
<nav class="order-last hidden w-64 shrink-0 xl:block print:hidden px-4" aria-label="table of contents">
{{ if $toc }}
<div class="sticky top-16 overflow-y-auto pr-4 pt-6 text-sm [hyphens:auto] max-h-[calc(100vh-4rem-env(safe-area-inset-bottom))] ltr:-mr-4 rtl:-ml-4">
{{ with .Fragments.Headings }}
<p class="mb-4 font-semibold tracking-tight">On This Page {{ $toc }}</p>
{{ range . }}
<ul>
{{ with .Headings }}
{{ template "toc-subheading" (dict "headings" . "level" 0) }}
{{ end }}
</ul>
{{ end }}
{{ end }}
{{ end }}
{{ $borderClass := "mt-8 border-t bg-white pt-8 shadow-[0_-12px_16px_white] dark:bg-dark dark:shadow-[0_-12px_16px_#111]" }}
{{ if not .Fragments.Headings }}
{{ $borderClass = "" }}
{{ end }}
{{ $borderClass := "mt-8 border-t bg-white pt-8 shadow-[0_-12px_16px_white] dark:bg-dark dark:shadow-[0_-12px_16px_#111]" }}
{{ if not .Fragments.Headings }}
{{ $borderClass = "" }}
{{ end }}
<div class="{{ $borderClass }} sticky bottom-0 flex flex-col items-start gap-2 pb-8 dark:border-neutral-800 contrast-more:border-t contrast-more:border-neutral-400 contrast-more:shadow-none contrast-more:dark:border-neutral-400">
<a class="text-xs font-medium text-gray-500 hover:text-gray-900 dark:text-gray-400 dark:hover:text-gray-100 contrast-more:text-gray-800 contrast-more:dark:text-gray-50" href="{{ .Params.editURL }}">Edit this page on GitHub →</a>
<div class="{{ $borderClass }} sticky bottom-0 flex flex-col items-start gap-2 pb-8 dark:border-neutral-800 contrast-more:border-t contrast-more:border-neutral-400 contrast-more:shadow-none contrast-more:dark:border-neutral-400">
<a class="text-xs font-medium text-gray-500 hover:text-gray-900 dark:text-gray-400 dark:hover:text-gray-100 contrast-more:text-gray-800 contrast-more:dark:text-gray-50" href="{{ .Params.editURL }}">Edit this page on GitHub →</a>
</div>
</div>
</div>
</div>
{{ end }}
</nav>
{{/* TOC subheading component.
This is a recursive component that renders a list of headings.
*/}}
{{- define "side-toc-subheading" -}}
{{/* TOC subheadings component. This is a recursive component that renders a list of headings. */}}
{{- define "toc-subheading" -}}
{{ $headings := .headings }}
{{ $level := .level }}
{{ if ge $level 6 }}
@ -48,7 +50,7 @@
</li>
{{ end }}
{{ with .Headings }}
{{ template "side-toc-subheading" (dict "headings" . "level" (add $level 1)) }}
{{ template "toc-subheading" (dict "headings" . "level" (add $level 1)) }}
{{ end }}
{{ end }}