From 33048ab20066a95dde0b59f6012299fa170a1598 Mon Sep 17 00:00:00 2001 From: geekifan Date: Mon, 21 Apr 2025 17:20:42 +0800 Subject: [PATCH] add read time and toc --- i18n/en.yaml | 5 +++++ i18n/zh.yaml | 5 +++++ layouts/_default/baseof.html | 2 ++ layouts/_default/single.html | 5 +++++ layouts/partials/read-time.html | 35 +++++++++++++++++++++++++++++++++ layouts/partials/toc.html | 23 ++++++++++++++++++++++ 6 files changed, 75 insertions(+) create mode 100644 layouts/partials/read-time.html create mode 100644 layouts/partials/toc.html diff --git a/i18n/en.yaml b/i18n/en.yaml index bb7de04..dc9bb24 100644 --- a/i18n/en.yaml +++ b/i18n/en.yaml @@ -18,11 +18,16 @@ search: panel: lastmod: "Recently Updated" trending_tags: "Trending Tags" + toc: "Contents" post: posted: "Posted" updated: "Updated" written_by: "By" + read_time: + prompt: "read" + unit: "min" + words: "words" copyright: brief: "All rights reserved" diff --git a/i18n/zh.yaml b/i18n/zh.yaml index 23531dc..6967f67 100644 --- a/i18n/zh.yaml +++ b/i18n/zh.yaml @@ -23,6 +23,11 @@ post: posted: "发布" updated: "更新" written_by: "由" + read_time: + prompt: "阅读" + unit: "分钟" + words: "字" + copyright: brief: "版权所有" verbose: "版权所有" diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 19145d0..20c1cc3 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -26,12 +26,14 @@ {{ partialCached "update-list.html" . }} {{ partialCached "trending-tags.html" . }} + {{ block "panel" . }}{{ end }}
+ {{ block "tail" . }}{{ end }} {{ partialCached "footer.html" . }}
diff --git a/layouts/_default/single.html b/layouts/_default/single.html index d752e12..0fae42b 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -82,6 +82,7 @@ {{ end }} + {{ partial "read-time.html" (dict "content" .Content "prompt" true "lang" $lang) }} @@ -154,4 +155,8 @@ +{{ end }} + +{{ define "panel" }} + {{ partial "toc.html" . }} {{ end }} \ No newline at end of file diff --git a/layouts/partials/read-time.html b/layouts/partials/read-time.html new file mode 100644 index 0000000..507dee0 --- /dev/null +++ b/layouts/partials/read-time.html @@ -0,0 +1,35 @@ + +{{ $content := .content }} +{{ $words := len (split $content " ") }} + + +{{ $wpm := 180 }} +{{ $min_time := 1 }} + +{{ $read_time := div $words $wpm }} + +{{ if lt $read_time 1 }} + {{ $read_time = $min_time }} +{{ end }} + +{{ $lang := .lang | default site.Language.Lang }} +{{ $read_prompt := i18n "post.read_time.prompt" (dict "Language" $lang) }} + + + + + {{- $read_time -}} + {{ " " }} + {{- i18n "post.read_time.unit" (dict "Language" $lang) -}} + + {{- if .prompt -}} + {{- $prompt_words := len (split $read_prompt " ") -}} + {{- if le $prompt_words 1 -}}{{ " " }}{{- end -}} + {{ $read_prompt }} + {{- end -}} + \ No newline at end of file diff --git a/layouts/partials/toc.html b/layouts/partials/toc.html new file mode 100644 index 0000000..b2cbee0 --- /dev/null +++ b/layouts/partials/toc.html @@ -0,0 +1,23 @@ +{{/* + Table of Contents (TOC) partial + Checks if TOC should be enabled, then renders it if needed +*/}} + +{{/* Determine if TOC should be enabled */}} +{{ $enable_toc := false }} +{{ $page_toc := default true .Params.toc }} {{/* 如果 .Params.toc 不存在,默认 true */}} +{{ if and site.Params.toc $page_toc }} + {{ if or (findRE " + +{{ end }} \ No newline at end of file