我自己平时使用 hugo 的一些可能会遗忘的部分, 做个记录

partials

 1{{- if (.Param "ShowBreadCrumbs") -}}
 2<div class="breadcrumbs">
 3    {{- $url := replace .Parent.Permalink (printf "%s" site.Home.Permalink) "" }}
 4    {{- $lang_url := strings.TrimPrefix (printf "%s/" .Lang) $url -}}
 5
 6    <a href="{{ "" | absLangURL }}">{{ i18n "home" | default "Home" }}</a>
 7    {{- $scratch := newScratch }}
 8    {{- range $index, $element := split $lang_url "/" }}
 9    <!--
10    这种方式可以打印来进行调试
11    element 的值为每一层的路径, 比如说如果该文章的 URL 为 "/posts/turorials/hugo/"
12    那么 element 会分别为: "posts", "turorials", "hugo", 以及最后一个 ""
13    {{- printf "index: %v, $element: %v" $index $element -}}
14    -->
15
16    <!--
17    累加路径
18    每次循环, scratch.path 的值就会往后累加地址, 比如一开始为
19    "{host}/posts", 下一次就为
20    "{host}/posts/tech", 再下一次就为
21    "{host}/posts/tech/shell"
22    -->
23    {{- $scratch.Add "path" (printf "%s/" $element )}}
24    {{- $bc_pg := site.GetPage ($scratch.Get "path") -}}
25
26    <!-- 
27    检查页面存在并且路径长度大于 0
28    值得注意的是, 如果路径中有中文, 认为页面不存在
29    并且页面存在指的是目录下存在 _index.md
30    $bc_pg.Name 是 _index.md 中的 Title
31    -->
32    {{- if (and ($bc_pg) (gt (len . ) 0))}}
33    {{- print "&nbsp;»&nbsp;" | safeHTML -}}<a href="{{ $bc_pg.Permalink }}">{{ $bc_pg.Name }}</a>
34    {{- end }}
35
36    {{- end -}}
37</div>
38{{- end -}}

home_info

这里是用来在首页渲染一些 home 才有的内容

list.html:

50{{- $paginator := .Paginate $pages }}
51
52<!-- 渲染 home_info -->
53{{- if and .IsHome site.Params.homeInfoParams (eq $paginator.PageNumber 1) }}
54{{- partial "home_info.html" . }}
55{{- end }}
56
57<!-- 这里就确定下面的文章列表的 class 类型应该是 post-entry/first-entry/post-entry tag-entry -->
58{{- $term := .Data.Term }}
59{{- range $index, $page := $paginator.Pages }}
60
61{{- $class := "post-entry" }}
62
63{{- $user_preferred := or site.Params.disableSpecial1stPost site.Params.homeInfoParams }}
64<!--
65{{- printf "disableSpecial1stPost: %v, homeInfoParams: %v" site.Params.disableSpecial1stPost site.Params.homeInfoParams }}
66{{- printf "is_home: %t, $paginator.PageNumber: %d, $index: %d, $user_preferred: %v"
67  $.IsHome $paginator.PageNumber $index (not $user_preferred) }}
68-->
69{{- if (and $.IsHome (eq $paginator.PageNumber 1) (eq $index 0) (not $user_preferred)) }}
70{{- $class = "first-entry" }}
71{{- else if $term }}
72{{- $class = "post-entry tag-entry" }}
73{{- end }}

在 hugo 的配置文件中配置 disableSpecial1stPost: true 即可不把首篇内容特殊展示

home_info.html:

{{- with site.Params.homeInfoParams }}
<article class="first-entry home-info">
    <header class="entry-header">
        <h1>{{ .Title | markdownify }}</h1>
    </header>
    <div class="entry-content">
        {{ .Content | markdownify }}
    </div>
    <footer class="entry-footer">
        {{ partial "social_icons.html" (dict "align" site.Params.homeInfoParams.AlignSocialIconsTo) }}
    </footer>
</article>
{{- end -}}

在 hugo 配置文件中添加内容比如:

params:
  homeInfoParams:
    balabala: V ME 50

即可在 home_info 中使用 {{ .Balabala | markdownify }}

在 home 中的 article list

<article class="post-entry"> 

<figure class="entry-cover"><img class="xxxx" loading="lazy" src="http://localhost:1313/images/guangzhou.jpeg" alt="this is a post image">
</figure>
  <header class="entry-header">
    <h2 class="entry-hint-parent">My First Post</h2>
  </header>
  <div class="entry-content">
    <p>this is summary</p>
  </div>
  <footer class="entry-footer"><span title="2024-08-11 15:18:51 +0800 CST">August 11, 2024</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;Dayday, Visan</footer>
  <a class="entry-link" aria-label="post link to My First Post" href="http://localhost:1313/posts/my-first-post/"></a>
</article>

在 home 中的 article list 是否展示 summary

在每篇文章中的 hideSummary 参数, 设置为 true 则不展示, 但是由于不展示后布局不好看, 于是修改了下

75<article class="{{ $class }}">
76  {{- $isHidden := (.Param "cover.hiddenInList") | default (.Param "cover.hidden") | default false }}
77  {{- partial "cover.html" (dict "cxt" . "IsSingle" false "isHidden" $isHidden "isList" true) }}
78  <header class="entry-header">
79    <h2 class="entry-hint-parent">
80      {{- .Title }}
81      {{- if .Draft }}
82      <span class="entry-hint" title="Draft">
83        <svg xmlns="http://www.w3.org/2000/svg" height="20" viewBox="0 -960 960 960" fill="currentColor">
84          <path
85            d="M160-410v-60h300v60H160Zm0-165v-60h470v60H160Zm0-165v-60h470v60H160Zm360 580v-123l221-220q9-9 20-13t22-4q12 0 23 4.5t20 13.5l37 37q9 9 13 20t4 22q0 11-4.5 22.5T862.09-380L643-160H520Zm300-263-37-37 37 37ZM580-220h38l121-122-18-19-19-18-122 121v38Zm141-141-19-18 37 37-18-19Z" />
86        </svg>
87      </span>
88      {{- end }}
89    </h2>
90  </header>
91  {{- if (ne (.Param "hideSummary") true) }}
92  <div class="entry-content">
93    <p>{{ .Summary | plainify | htmlUnescape }}{{ if .Truncated }}...{{ end }}</p>
94  </div>
95  {{- else }}
96  <!-- 为了用到 entry-content 的上下 margin -->
97  <div class="entry-content">
98  </div>
99  {{- end }}