add read time and toc
This commit is contained in:
@ -26,12 +26,14 @@
|
||||
{{ partialCached "update-list.html" . }}
|
||||
{{ partialCached "trending-tags.html" . }}
|
||||
</div>
|
||||
{{ block "panel" . }}{{ end }}
|
||||
</aside>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<!-- tail -->
|
||||
<div id="tail-wrapper" class="col-12 col-lg-11 col-xl-9 px-md-4">
|
||||
{{ block "tail" . }}{{ end }}
|
||||
{{ partialCached "footer.html" . }}
|
||||
</div>
|
||||
</div>
|
||||
|
@ -82,6 +82,7 @@
|
||||
{{ end }}
|
||||
|
||||
<!-- read time -->
|
||||
{{ partial "read-time.html" (dict "content" .Content "prompt" true "lang" $lang) }}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
@ -154,4 +155,8 @@
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
{{ end }}
|
||||
|
||||
{{ define "panel" }}
|
||||
{{ partial "toc.html" . }}
|
||||
{{ end }}
|
35
layouts/partials/read-time.html
Normal file
35
layouts/partials/read-time.html
Normal 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
23
layouts/partials/toc.html
Normal 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 }}
|
Reference in New Issue
Block a user