Hugo themes Doit 合并 tags , categories 为检索页

注意
本文最后更新于 2024-04-21,文中内容可能已过时。

总觉得 tags, categories 等页面可以合并成为一页。这样检索起来更方便一些。成果 https://www.ftls.xyz/retrieval/

config.toml

toml

# 用于分类的设置
[taxonomies]
author = "authors"
category = "categories"
tag = "tags"
series = "series"
retrieval = "retrieval"

尝试使用 yaml 中的锚点和引用。

yaml

tags: &tag []
categories: &cat []
retrieval:
 tag: *tag
 cat: *cat

很遗憾,失败了。这在单页面上,也就是说具体页面。可以成功,然而在检索页却并不是可以成功的。 https://www.ftls.xyz/retrieval/ 不能获取到正确数据。

使用如下在 terms.html 中获取自定义分类的具体内容。

text

{{- $terms := $.Site.Taxonomies.series.ByCount -}}

这里使用的是 Doit 的主题。大家可以借鉴一下看看大致原理。知道有这个写法,大致就知道怎么写的了。

themes\DoIt\layouts\taxonomy\terms.html end 前新增的内容

html

	{{- /* Sum Retrieval Page */ -}}
        {{- else if eq $taxonomies "retrieval" -}}
            <h3><a href="/tags/">>标签</a>&nbsp;&nbsp;<a href="/series/">>系列</a>&nbsp;&nbsp;<a href="/categories/">>分类</a></h3>
            <h2><a href="/tags/">标签</a></h2>
            <div class="tag-cloud-tags">
                {{- range $.Site.Taxonomies.tags.ByCount -}}
                    <a href="{{ .Page.RelPermalink }}">{{ .Page.Title }} <sup>{{ .Count }}</sup></a>
                {{- end -}}
            </div>
            <h2><a href="/series/">系列</a></h2>
            {{- $terms := $.Site.Taxonomies.series.ByCount -}}
            <div>
                {{- range $terms -}}
                    {{- $term := .Term -}}
                    {{- $pages := .Pages -}}
                    {{- $type := "series" -}}
                    {{-  with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
                    <div class="card-item">
                        <div class="card-item-wrapper">
                            <h3 class="card-item-title">
                                <a href="{{ .RelPermalink }}">
                                    <i class="far fa-folder fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                                </a>
                            </h3>
                            {{- range first 5 $pages -}}
                                <article class="archive-item">
                                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                                        {{- .Title -}}
                                    </a>
                                </article>
                            {{- end -}}
                            {{- if gt (len $pages) 5 -}}
                                <span class="more-post">
                                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                                </span>
                            {{- end -}}
                        </div>
                    </div>
                    {{- end -}}
                {{- end -}}
            </div>

            <h2><a href="/categories/">分类</a></h2>
	        {{- $terms := $.Site.Taxonomies.categories.ByCount -}}
            <div>
                {{- range $terms -}}
                    {{- $term := .Term -}}
                    {{- $pages := .Pages -}}
		    {{- $type := "categories" -}}
                    {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
                    <div class="card-item">
                        <div class="card-item-wrapper">
                            <h3 class="card-item-title">
                                <a href="{{ .RelPermalink }}">
                                    <i class="far fa-folder fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                                </a>
                            </h3>
                            {{- range first 5 $pages -}}
                                <article class="archive-item">
                                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                                        {{- .Title -}}
                                    </a>
                                </article>
                            {{- end -}}
                            {{- if gt (len $pages) 5 -}}
                                <span class="more-post">
                                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                                </span>
                            {{- end -}}
                        </div>
                    </div>
                    {{- end -}}
                {{- end -}}
            </div>

加入之后的整体 terms.html ,也可以直接放到 layouts\taxonomy\terms.html 下,这个文件夹优先级比主题高。

html

{{- define "title" -}}
    {{- .Params.Title | default (T .Data.Plural) | default .Data.Plural | dict "Some" | T "allSome" }} - {{ .Site.Title -}}
{{- end -}}

{{- define "content" -}}
    {{- $taxonomies := .Data.Plural -}}
    {{- $type := .Type -}}

    <div class="page archive">
        {{- /* Title */ -}}
        <h2 class="single-title animate__animated animate__pulse animate__faster">
            {{- .Params.Title | default (T $taxonomies) | default $taxonomies | dict "Some" | T "allSome" -}}
        </h2>

        {{- /* Categories Page */ -}}
        {{- if eq $taxonomies "categories" -}}
            {{- $terms := .Data.Terms.ByCount -}}
            <div class="categories-card">
                {{- range $terms -}}
                    {{- $term := .Term -}}
                    {{- $pages := .Pages -}}
                    {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
                    <div class="card-item">
                        <div class="card-item-wrapper">
                            <h3 class="card-item-title">
                                <a href="{{ .RelPermalink }}">
                                    <i class="far fa-folder fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                                </a>
                            </h3>
                            {{- range first 5 $pages -}}
                                <article class="archive-item">
                                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                                        {{- .Title -}}
                                    </a>
                                </article>
                            {{- end -}}
                            {{- if gt (len $pages) 5 -}}
                                <span class="more-post">
                                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                                </span>
                            {{- end -}}
                        </div>
                    </div>
                    {{- end -}}
                {{- end -}}
            </div>

        {{- /* Series Page */ -}}
        {{- else if eq $taxonomies "series" -}}
            {{- $terms := .Data.Terms.ByCount -}}
            <div class="series-card">
                {{- range $terms -}}
                    {{- $term := .Term -}}
                    {{- $pages := .Pages -}}
                    {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
                    <div class="card-item">
                        <div class="card-item-wrapper">
                            <h3 class="card-item-title">
                                <a href="{{ .RelPermalink }}">
                                    <i class="far fa-folder fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                                </a>
                            </h3>
                            {{- range first 5 $pages -}}
                                <article class="archive-item">
                                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                                        {{- .Title -}}
                                    </a>
                                </article>
                            {{- end -}}
                            {{- if gt (len $pages) 5 -}}
                                <span class="more-post">
                                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                                </span>
                            {{- end -}}
                        </div>
                    </div>
                    {{- end -}}
                {{- end -}}
            </div>
            
        {{- /* Author Page */ -}}
        {{- else if eq $taxonomies "authors" -}}
            {{- $terms := .Data.Terms.Alphabetical -}}
            <div class="author-card">
                {{- range $terms -}}
                    {{- $term := .Term -}}
                    {{- $pages := .Pages -}}
                    {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
                    <div class="card-item">
                        <div class="card-item-wrapper">
                            <h3 class="card-item-title">
                                <a href="{{ .RelPermalink }}">
                                    <i class="fas fa-user-circle fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                                </a>
                            </h3>
                            {{- range first 5 $pages -}}
                                <article class="archive-item">
                                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                                        {{- .Title -}}
                                    </a>
                                </article>
                            {{- end -}}
                            {{- if gt (len $pages) 5 -}}
                                <span class="more-post">
                                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                                </span>
                            {{- end -}}
                        </div>
                    </div>
                    {{- end -}}
                {{- end -}}
            </div>

        {{- /* Tag Cloud Page */ -}}
        {{- else if eq $taxonomies "tags" -}}
            <div class="tag-cloud-tags">
                {{- range $.Site.Taxonomies.tags.ByCount -}}
                    <a href="{{ .Page.RelPermalink }}">{{ .Page.Title }} <sup>{{ .Count }}</sup></a>
                {{- end -}}
            </div>

	{{- /* Sum Retrieval Page */ -}}
        {{- else if eq $taxonomies "retrieval" -}}
            <h3><a href="/tags/">>标签</a>&nbsp;&nbsp;<a href="/series/">>系列</a>&nbsp;&nbsp;<a href="/categories/">>分类</a></h3>
            <h2><a href="/tags/">标签</a></h2>
            <div class="tag-cloud-tags">
                {{- range $.Site.Taxonomies.tags.ByCount -}}
                    <a href="{{ .Page.RelPermalink }}">{{ .Page.Title }} <sup>{{ .Count }}</sup></a>
                {{- end -}}
            </div>
            <h2><a href="/series/">系列</a></h2>
            {{- $terms := $.Site.Taxonomies.series.ByCount -}}
            <div>
                {{- range $terms -}}
                    {{- $term := .Term -}}
                    {{- $pages := .Pages -}}
                    {{- $type := "series" -}}
                    {{-  with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
                    <div class="card-item">
                        <div class="card-item-wrapper">
                            <h3 class="card-item-title">
                                <a href="{{ .RelPermalink }}">
                                    <i class="far fa-folder fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                                </a>
                            </h3>
                            {{- range first 5 $pages -}}
                                <article class="archive-item">
                                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                                        {{- .Title -}}
                                    </a>
                                </article>
                            {{- end -}}
                            {{- if gt (len $pages) 5 -}}
                                <span class="more-post">
                                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                                </span>
                            {{- end -}}
                        </div>
                    </div>
                    {{- end -}}
                {{- end -}}
            </div>

            <h2><a href="/categories/">分类</a></h2>
	        {{- $terms := $.Site.Taxonomies.categories.ByCount -}}
            <div>
                {{- range $terms -}}
                    {{- $term := .Term -}}
                    {{- $pages := .Pages -}}
		    {{- $type := "categories" -}}
                    {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
                    <div class="card-item">
                        <div class="card-item-wrapper">
                            <h3 class="card-item-title">
                                <a href="{{ .RelPermalink }}">
                                    <i class="far fa-folder fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                                </a>
                            </h3>
                            {{- range first 5 $pages -}}
                                <article class="archive-item">
                                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                                        {{- .Title -}}
                                    </a>
                                </article>
                            {{- end -}}
                            {{- if gt (len $pages) 5 -}}
                                <span class="more-post">
                                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                                </span>
                            {{- end -}}
                        </div>
                    </div>
                    {{- end -}}
                {{- end -}}
            </div>
        {{- end -}}
    </div>
{{- end -}}

2024-04-21 优化,使用 Hugo 模板查找顺序。

content/retrieval.md

md

---
title: 检索
layout: retrieval
---

layouts/page/retrieval.html

html

{{- define "title" -}}
检索 - {{ .Site.Title -}}
{{- end -}}

{{- define "content" -}}
{{- /* Sum Retrieval Page */ -}}
<div class="page archive retrieval">
    <h2 class="archive-title">
        <a href="{{ relURL "posts/" }}">所有文章</a>&nbsp;&nbsp;
        <a href="{{ relURL "tags/" }}">标签</a>&nbsp;&nbsp;
        <a href="{{ relURL "series/" }}">系列</a>&nbsp;&nbsp;
        <a href="{{ relURL "categories/" }}">分类</a>&nbsp;&nbsp;
        <a href="{{ relURL "gallery/" }}">画廊</a>&nbsp;&nbsp;
        <a href="{{ relURL "showcase/" }}">作品</a>
    </h2>
    <hr />
    <h2><a href="{{ relURL "tags/" }}">标签</a></h2>
    <div class="tag-cloud-tags">
        {{- range $.Site.Taxonomies.tags.ByCount -}}
        <a href="{{ .Page.RelPermalink }}">{{ .Page.Title }} <sup>{{ .Count }}</sup></a>
        {{- end -}}
    </div>
    <hr />
    <h2><a href="{{ relURL "series/" }}">系列</a></h2>
    {{- $terms := $.Site.Taxonomies.series.ByCount -}}
    <div>
        {{- range $terms -}}
        {{- $term := .Term -}}
        {{- $pages := .Pages -}}
        {{- $type := "series" -}}
        {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
        <div class="card-item">
            <div class="card-item-wrapper">
                <h3 class="card-item-title">
                    <a href="{{ .RelPermalink }}">
                        <i class="far fa-folder fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                    </a>
                </h3>
                {{- range first 5 $pages -}}
                <article class="archive-item">
                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                        {{- .Title -}}
                    </a>
                </article>
                {{- end -}}
                {{- if gt (len $pages) 3 -}}
                <span class="more-post">
                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                </span>
                {{- end -}}
            </div>
        </div>
        {{- end -}}
        {{- end -}}
    </div>
    <hr />
    <h2><a href="{{ relURL "categories/" }}">分类</a></h2>
    {{- $terms := $.Site.Taxonomies.categories.ByCount -}}
    <div>
        {{- range $terms -}}
        {{- $term := .Term -}}
        {{- $pages := .Pages -}}
        {{- $type := "categories" -}}
        {{- with $.Site.GetPage "taxonomy" (printf "%v/%v" $type $term) -}}
        <div class="card-item">
            <div class="card-item-wrapper">
                <h3 class="card-item-title">
                    <a href="{{ .RelPermalink }}">
                        <i class="far fa-folder fa-fw"></i>&nbsp;{{ .Page.Title }} <sup>{{- len $pages -}}</sup>
                    </a>
                </h3>
                {{- range first 10 $pages -}}
                <article class="archive-item">
                    <a href="{{ .RelPermalink }}" class="archive-item-link">
                        {{- .Title -}}
                    </a>
                </article>
                {{- end -}}
                {{- if gt (len $pages) 10 -}}
                <span class="more-post">
                    <a href="{{ .RelPermalink }}" class="more-single-link">{{ T "more" }} >></a>
                </span>
                {{- end -}}
            </div>
        </div>
        {{- end -}}
        {{- end -}}
    </div>
    <!-- 未分类 -->
    <div>
        {{ $uncategorized := where .Site.RegularPages "Type" "!=" "page" }}
        {{- $pages := slice -}}
        {{- range $uncategorized -}}
        {{ $cat := (printf "%v" .Params.categories ) }}
        {{ if ne $cat "<nil>" | eq $cat "[]" }}
            {{ $pages = $pages | append . }}
            {{- end -}}
            {{- end -}}
            <div class="card-item">
                <div class="card-item-wrapper">
                    <h3 class="card-item-title">
                        <a>
                            <i class="far fa-folder fa-fw"></i>&nbsp;未分类 <sup>{{- len $pages -}}</sup>
                        </a>
                    </h3>
                    {{- range $pages -}}
                    <article class="archive-item">
                        <a href="{{ .RelPermalink }}" class="archive-item-link">
                            {{- .Title -}}
                        </a>
                    </article>
                    {{- end -}}
                </div>
            </div>
    </div>
</div>

{{- end -}}

assets/css/_custom.scss

scss

// 检索页 hr

.retrieval {
  .archive-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }
  hr {
    margin: 0.5rem 0;
    position: relative;
    border-top: 3px dashed $global-border-color;
    border-bottom: none;

    [theme="dark"] & {
      border-top: 3px dashed $global-border-color-dark;
    }

    [theme="black"] & {
      border-top: 3px dashed $global-border-color-black;
    }
  }
}