summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-07-27 10:48:59 +0800
committerEltrac <hi@guhub.cn>2025-07-27 10:48:59 +0800
commitdc7d715dc30fd576f65f862ee3f1b0b46b48e690 (patch)
treed756ff19a9e85c387f0497b8eefa6212f12cce9d
parentefb3baca5a534e40555f379d6d3e6361b8e7cc8b (diff)
refactor: extract backlinks function as a component
-rw-r--r--layouts/_partials/components/backlinks.html37
-rw-r--r--layouts/cards/page.html25
-rw-r--r--layouts/library/page.html1
3 files changed, 39 insertions, 24 deletions
diff --git a/layouts/_partials/components/backlinks.html b/layouts/_partials/components/backlinks.html
new file mode 100644
index 00000000..e97c295c
--- /dev/null
+++ b/layouts/_partials/components/backlinks.html
@@ -0,0 +1,37 @@
+{{ $type := .type }}
+{{ $filename := .filename }}
+{{ $ctx := .context }}
+{{ $referencedBy := slice }}
+
+{{ if eq $type "library" }}
+ {{ range $ctx.Site.Pages }}
+ {{ if and (isset .Params "mentioned_books") (in .Params.mentioned_books $filename) }}
+ {{ $referencedBy = $referencedBy | append . }}
+ {{ end }}
+ {{ end }}
+{{ else if eq $type "cards" }}
+ {{ range $ctx.Site.Pages }}
+ {{ if and (isset .Params "related_cards") (in .Params.related_cards $filename) }}
+ {{ $referencedBy = $referencedBy | append . }}
+ {{ end }}
+ {{ end }}
+{{ end }}
+
+{{- /* 如果有文章引用这个卡片,就显示它们 */ -}}
+{{ if gt (len $referencedBy) 0 }}
+<section class="related-posts post-content my-5">
+ <h2 class="text-size-base m-0 flex items-center gap-2">
+ <span>反向链接</span>
+ <span class="inline-block bg-blockBg rounded-full px-1 text-secondary cursor-help text-size-sm"
+ data-tooltip="这里列出了引用了该页面的其他文章">?</span>
+ </h2>
+ <ul class="my-4 list-none ps-4">
+ {{- range $referencedBy }}
+ <li class="flex gap-4">
+ <span class="text-secondary">→</span>
+ <a href="{{ .Permalink }}">{{ .Title }}</a>
+ </li>
+ {{- end }}
+ </ul>
+</section>
+{{ end }}
diff --git a/layouts/cards/page.html b/layouts/cards/page.html
index 232c9593..0c528867 100644
--- a/layouts/cards/page.html
+++ b/layouts/cards/page.html
@@ -6,29 +6,6 @@
"imageClass" "w-full"
) }}
{{ partial "items/card" (dict "context" .) }}
-
- {{- /* 查询引用了这个卡片的文章 */ -}}
- {{- $currentCard := .File.BaseFileName -}} <!-- 获取当前卡片的文件名 -->
- {{- $referencedBy := slice -}} <!-- 初始化一个空数组 -->
-
- {{- range .Site.Pages }}
- {{- if and (isset .Params "related_cards") (in .Params.related_cards $currentCard) }}
- {{- $referencedBy = $referencedBy | append . }}
- {{- end }}
- {{- end }}
-
- {{- /* 如果有文章引用这个卡片,就显示它们 */ -}}
- {{ if gt (len $referencedBy) 0 }}
- <section class="related-posts post-content my-5">
- <h2 class="text-5 text-secondary m-0">引用了本卡片的文章</h2>
- <ul class="my-4">
- {{- range $referencedBy }}
- <li>
- <a href="{{ .Permalink }}">{{ .Title }}</a>
- </li>
- {{- end }}
- </ul>
- </section>
- {{ end }}
+ {{ partial "components/backlinks" (dict "filename" .File.BaseFileName "type" "cards" "context" .) }}
</article>
{{ end }}
diff --git a/layouts/library/page.html b/layouts/library/page.html
index e824077a..5e6a85be 100644
--- a/layouts/library/page.html
+++ b/layouts/library/page.html
@@ -34,6 +34,7 @@
<p>于 {{ partial "components/date" . }} 读完</p>
<p>{{ partial "components/tags" . }}</p>
</div>
+ {{ partial "components/backlinks" (dict "filename" .File.BaseFileName "type" "library" "context" .) }}
{{ partial "posts/comment.html" . }}
</article>
{{ end }}