目录

修改文章post_meta信息

目录

Hugo里使用post_meta描述文章的信息,比如发布时间、作者、标签、阅读时长等。这次受这篇文章的启发来修改一下,使其更加美观。

themes/PaperMod/layouts/partials/post_meta.html拷贝到layouts/partials下,并修改:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{{- $scratch := newScratch }}
<!--创建时间-->
{{- if not .Date.IsZero -}}
{{- $scratch.Add "meta" (slice (printf "发表于<span title='%s'>%s</span>" (.Date) (.Date | time.Format (default "2006-01-02" .Site.Params.DateFormat)))) }}
{{- end }}
<!--添加:修改时间-->
{{- if (.Param "ShowLastMod") -}}
{{- $scratch.Add "meta" (slice (printf "修改于<span title='%s'>%s</span>" (.Lastmod) (.Lastmod | time.Format (default "2006-01-02" .Site.Params.DateFormat)))) }}
{{- end }}
<!--阅读时间-->
{{- if (.Param "ShowReadingTime") -}}
{{- $scratch.Add "meta" (slice (i18n "read_time" .ReadingTime | default (printf "%d min" .ReadingTime))) }}
{{- end }}
<!--字数统计-->
{{- if (.Param "ShowWordCount") -}}
{{- $scratch.Add "meta" (slice (i18n "words" .WordCount | default (printf "%d words" .WordCount))) }}
{{- end }}

{{- with (partial "author.html" .) }}
{{- $scratch.Add "meta" (slice .) }}
{{- end }}
<!--分隔符-->
{{- with ($scratch.Get "meta") }}
{{- delimit . "&nbsp;·&nbsp;" -}}
{{- end -}}

config.yaml中:

1
2
3
4
enableGitInfo: false # 关闭gitinfo,否则lastmod会显示为最后commit时间,有些bug
params:
  DateFormat: "2006-01-02" # 日期格式
  ShowLastMod: true # 显示lastmod

然后就可以在文章的front_matter中配置:

1
lastmod: 2022-03-21T21:53:51+08:00

也可以写进archetypes中:

1
lastmod: {{ .Date }}

后面再编辑时手动修改即可。

相关内容