feat(book): auto-list child pages and add fallback title

Quality of life improvements for creating second brains or knowledge bases with apps like Obsidian.
This commit is contained in:
Geo 2022-06-02 20:00:18 +01:00
commit 45f971c352
5 changed files with 27 additions and 8 deletions

View file

@ -52,8 +52,10 @@
{{- end -}}
{{- /* Add page to index. */ -}}
{{- $index = $index | append (dict "objectID" .File.UniqueID "date" .Date.UTC.Unix "publishdate" .PublishDate "lastmod" .Lastmod.UTC.Unix "expirydate" .ExpiryDate.UTC.Unix "lang" .Lang "permalink" $permalink "relpermalink" $rel_permalink "title" $title "summary" (plainify $desc | htmlUnescape) "content" (.Plain | htmlUnescape | truncate 5000) "authors" $authors "kind" .Kind "type" .Type "section" .Section "tags" .Params.Tags "categories" .Params.Categories) -}}
{{/* Exclude virtual pages which aren't backed by a file */}}
{{ if .File }}
{{- $index = $index | append (dict "objectID" .File.UniqueID "date" .Date.UTC.Unix "publishdate" .PublishDate "lastmod" .Lastmod.UTC.Unix "expirydate" .ExpiryDate.UTC.Unix "lang" .Lang "permalink" $permalink "relpermalink" $rel_permalink "title" $title "summary" (plainify $desc | htmlUnescape) "content" (.Plain | htmlUnescape | truncate 5000) "authors" $authors "kind" .Kind "type" .Type "section" .Section "tags" .Params.Tags "categories" .Params.Categories) -}}
{{ end }}
{{- end -}}
{{- end -}}

View file

@ -59,10 +59,25 @@
{{end}}
<div class="docs-article-container">
<h1>{{ .Title }}</h1>
{{/* Fallback to filename as title (useful for default Obsidian note structure) */}}
<h1>{{ cond (ne .File nil) (.Title | default (title .File.ContentBaseName)) .Title }}</h1>
<div class="article-style">
{{/* Only auto-list page children if _index has no content (or if it isn't a .File?) */}}
{{ if and (not .Content) (gt (len $.Page.Pages) 0) }}
<ul class="list-unstyled">
{{ range $.Page.Pages }}
<li>
<h5><a href="{{.RelPermalink}}">{{.LinkTitle | default (title .File.TranslationBaseName) }}</a></h5>
{{if .Summary}}<p>{{.Summary | plainify | emojify}}</p>{{end}}
</li>
{{end}}
</ul>
{{end}}
{{ .Content }}
</div>
{{ partial "tags.html" . }}

View file

@ -35,10 +35,10 @@
{{ if $first }}
<ul class="nav docs-sidenav">
<li class="{{ if and .File $current_node.File | and (eq .File.UniqueID $current_node.File.UniqueID) }}active{{ end }}"><a href="{{ .RelPermalink }}">{{ safeHTML $icon }}{{ .LinkTitle | default .Title }}</a></li>
<li class="{{ if and .File $current_node.File | and (eq .File.UniqueID $current_node.File.UniqueID) }}active{{ end }}"><a href="{{ .RelPermalink }}">{{ safeHTML $icon }}{{ .LinkTitle }}</a></li>
{{else}}
<div class="docs-toc-item">
<a class="docs-toc-link {{ if and .File $current_node.File | and (eq .File.UniqueID $current_node.File.UniqueID) }} active{{ end }}" href="{{ .RelPermalink }}">{{ safeHTML $icon }}{{ .LinkTitle | default .Title }}</a>
<a class="docs-toc-link {{ if and .File $current_node.File | and (eq .File.UniqueID $current_node.File.UniqueID) }} active{{ end }}" href="{{ .RelPermalink }}">{{ safeHTML $icon }}{{ .LinkTitle }}</a>
{{end}}
{{- $page_count := (add (len .Pages) (len .Sections)) -}}
@ -77,7 +77,7 @@
{{- else -}}
{{- if not .Params.Hidden -}}
<li class="{{ if and .File $current_node.File | and (eq .File.UniqueID $current_node.File.UniqueID) }}active{{ end }}"><a href="{{ .RelPermalink }}">{{ safeHTML $icon }}{{ .LinkTitle | default .Title }}</a></li>
<li class="{{ if and .File $current_node.File | and (eq .File.UniqueID $current_node.File.UniqueID) }}active{{ end }}"><a href="{{ .RelPermalink }}">{{ safeHTML $icon }}{{ .LinkTitle | default (title .File.ContentBaseName) }}</a></li>
{{- end -}}
{{ end -}}
{{- end -}}

View file

@ -1,5 +1,6 @@
{{/* Add link to let visitors edit the page on GitHub. */}}
{{ if .Params.editable | and site.Params.features.repository.url }}
{{/* Perform short-circuit check that page is backed by a file. */}}
{{ if and .File .Params.editable | and site.Params.features.repository.url }}
{{/* Get language subfolder for multilingual sites. */}}
{{/* Limitations: the subfolder param, `<LANG>.contentDir`, is unexposed in the Hugo `site` API so we attempt to workaround this issue. */}}
{{ $content_dir := "content" }}

View file

@ -3,7 +3,8 @@
<ul class="list-unstyled">
{{ range $.Page.Pages }}
<li>
<h5><a href="{{.RelPermalink}}">{{.LinkTitle}}</a></h5>
{{/* Fallback to filename as title (useful for default Obsidian note structure) */}}
<h5><a href="{{.RelPermalink}}">{{ .LinkTitle | default (title .File.TranslationBaseName) }}</a></h5>
{{if $show_summary | and .Summary}}<p>{{.Summary | plainify | emojify}}</p>{{end}}
</li>
{{end}}