From dc7d715dc30fd576f65f862ee3f1b0b46b48e690 Mon Sep 17 00:00:00 2001 From: Eltrac Date: Sun, 27 Jul 2025 10:48:59 +0800 Subject: refactor: extract backlinks function as a component --- layouts/_partials/components/backlinks.html | 37 +++++++++++++++++++++++++++++ layouts/cards/page.html | 25 +------------------ layouts/library/page.html | 1 + 3 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 layouts/_partials/components/backlinks.html 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 }} +
+

+ 反向链接 + ? +

+ +
+{{ 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 }} -
-

引用了本卡片的文章

- -
- {{ end }} + {{ partial "components/backlinks" (dict "filename" .File.BaseFileName "type" "cards" "context" .) }} {{ 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 @@

于 {{ partial "components/date" . }} 读完

{{ partial "components/tags" . }}

+ {{ partial "components/backlinks" (dict "filename" .File.BaseFileName "type" "library" "context" .) }} {{ partial "posts/comment.html" . }} {{ end }} -- cgit v1.2.3