add read time and toc

This commit is contained in:
geekifan
2025-04-21 17:20:42 +08:00
parent 99f5cf6774
commit 33048ab200
6 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,35 @@
<!-- Calculate the post's reading time, and display the word count in tooltip -->
{{ $content := .content }}
{{ $words := len (split $content " ") }}
<!-- words per minute -->
{{ $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) }}
<!-- return element -->
<span
class="readtime"
data-bs-toggle="tooltip"
data-bs-placement="bottom"
title="{{ $words }} {{ i18n "post.words" (dict "Language" $lang) }}"
>
<em>
{{- $read_time -}}
{{ " " }}
{{- i18n "post.read_time.unit" (dict "Language" $lang) -}}
</em>
{{- if .prompt -}}
{{- $prompt_words := len (split $read_prompt " ") -}}
{{- if le $prompt_words 1 -}}{{ " " }}{{- end -}}
{{ $read_prompt }}
{{- end -}}
</span>

23
layouts/partials/toc.html Normal file
View File

@ -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 "<h2" .Content) (findRE "<h3" .Content) }}
{{ $enable_toc = true }}
{{ end }}
{{ end }}
{{/* Render TOC if enabled */}}
{{ if $enable_toc }}
<div class="toc-border-cover z-3"></div>
<section id="toc-wrapper" class="invisible position-sticky ps-0 pe-4 pb-4">
<h2 class="panel-heading ps-3 pb-2 mb-0">{{ i18n "panel.toc" }}</h2>
<nav id="toc"></nav>
</section>
{{ end }}