From cf91ff39630cead0fad16c16858405b3efbe7994 Mon Sep 17 00:00:00 2001 From: Ngô Ngọc Đức Huy Date: Fri, 4 Aug 2023 10:17:49 +0700 Subject: Implement table of contents --- assets/css/theme.css | 4 ++++ i18n/en.toml | 3 +++ i18n/fr.toml | 3 +++ i18n/vi.toml | 3 +++ layouts/_default/list.html | 4 ++-- layouts/partials/post-navigation-top.html | 3 +++ layouts/partials/post-navigation.html | 2 ++ layouts/partials/root-toc.html | 8 ++++++++ layouts/partials/toc.html | 11 ++++++++--- 9 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 layouts/partials/root-toc.html diff --git a/assets/css/theme.css b/assets/css/theme.css index 1162ae0..22400e9 100644 --- a/assets/css/theme.css +++ b/assets/css/theme.css @@ -54,3 +54,7 @@ a.nav-prev::after { a.nav-next::before { content: "← " } + +.toc-collapse { + text-align: initial; +} diff --git a/i18n/en.toml b/i18n/en.toml index 64ee617..5ec68db 100644 --- a/i18n/en.toml +++ b/i18n/en.toml @@ -14,3 +14,6 @@ other = "approximately {{ .Count }} minutes reading" [skipToContent] other = "Skip to main content" + +[toc] +other = "Table of contents" diff --git a/i18n/fr.toml b/i18n/fr.toml index 42481d0..e649d59 100644 --- a/i18n/fr.toml +++ b/i18n/fr.toml @@ -14,3 +14,6 @@ other = "environ {{ .Count }} minutes de lecture" [skipToContent] other = "Aller à l'article" + +[toc] +other = "Table des matières" diff --git a/i18n/vi.toml b/i18n/vi.toml index 97a6fc5..e5a1751 100644 --- a/i18n/vi.toml +++ b/i18n/vi.toml @@ -12,3 +12,6 @@ other = "đọc hết khoảng {{ .Count }} phút" [skipToContent] other = "Đi đến bài viết" + +[toc] +other = "Mục lục" diff --git a/layouts/_default/list.html b/layouts/_default/list.html index 9a4672e..0f104a4 100644 --- a/layouts/_default/list.html +++ b/layouts/_default/list.html @@ -5,13 +5,13 @@ SPDX-License-Identifier: MIT */}} {{ define "main" }} -
+
{{ .Content }}
{{ if in .Params.categories "book" }} {{ partial "post-navigation-top.html" . }}
- {{ partial "toc.html" . }} + {{ partial "toc.html" (dict "content" . "currentLink" .RelPermalink) }}
{{ else if in .Params.categories "blog" }} {{ partial "feed.html" . }} diff --git a/layouts/partials/post-navigation-top.html b/layouts/partials/post-navigation-top.html index 3bcc39f..a7470c0 100644 --- a/layouts/partials/post-navigation-top.html +++ b/layouts/partials/post-navigation-top.html @@ -1,2 +1,5 @@ +{{ if or (in .Params.categories "book") (in .CurrentSection.Params.categories "book") }} {{ partial "breadcrumbs.html" .}} +{{ partial "root-toc.html" .}} +{{ end}} {{ partial "post-navigation.html" .}} diff --git a/layouts/partials/post-navigation.html b/layouts/partials/post-navigation.html index b88d91f..edd1333 100644 --- a/layouts/partials/post-navigation.html +++ b/layouts/partials/post-navigation.html @@ -1,3 +1,4 @@ +{{ if not .IsSection}} +{{ end }} diff --git a/layouts/partials/root-toc.html b/layouts/partials/root-toc.html new file mode 100644 index 0000000..2792ce9 --- /dev/null +++ b/layouts/partials/root-toc.html @@ -0,0 +1,8 @@ +
+ {{ i18n "toc" }} + {{ $currentLink := .RelPermalink }} + {{ range last 1 (where .Ancestors "Params.categories" "intersect" (slice "book") ) }} + {{ .Title }} + {{ partial "toc.html" (dict "content" . "currentLink" $currentLink) }} + {{ end }} +
diff --git a/layouts/partials/toc.html b/layouts/partials/toc.html index 607e7f0..3b5a822 100644 --- a/layouts/partials/toc.html +++ b/layouts/partials/toc.html @@ -1,4 +1,5 @@ -{{ $chapters := .Page.Pages }} +{{ $chapters := .content.Page.Pages }} +{{ $currentLink := .currentLink}} {{ range .Page.Translations }} {{ $chapters = $chapters | lang.Merge .Pages }} {{ end}} @@ -9,10 +10,14 @@ {{ with .Params.chapterName }} {{ $fullTitle = printf "%s – %s" $fullTitle . }} {{ end }} - {{ trim $fullTitle " " }} + {{ if eq $currentLink .RelPermalink }} + {{ trim $fullTitle " " }} ({{ i18n "current"}}) + {{ else }} + {{ trim $fullTitle " " }} + {{ end }} {{ partial "post-info.html" . }} {{ if .IsSection}} - {{ partial "toc.html" . }} + {{ partial "toc.html" (dict "content" . "currentLink" $currentLink) }} {{ end }} {{ end }} -- cgit 1.4.1