From a5aba5093869be4ace9f40450fbccc4d33080510 Mon Sep 17 00:00:00 2001 From: George Cushen Date: Sat, 1 Jun 2024 14:25:52 +0100 Subject: [PATCH] feat: reimplement backlinks --- .../_markup/render-link.backlinks.json | 20 +++++++----------- .../layouts/_default/home.backlinks.json | 11 ++-------- .../partials/components/backlinks.html | 21 +++++++++---------- 3 files changed, 20 insertions(+), 32 deletions(-) diff --git a/modules/blox-tailwind/layouts/_default/_markup/render-link.backlinks.json b/modules/blox-tailwind/layouts/_default/_markup/render-link.backlinks.json index cc67e563..0200eb60 100644 --- a/modules/blox-tailwind/layouts/_default/_markup/render-link.backlinks.json +++ b/modules/blox-tailwind/layouts/_default/_markup/render-link.backlinks.json @@ -1,18 +1,14 @@ -{{- $u := urls.Parse .Destination -}} -{{- if or $u.IsAbs (strings.HasPrefix $u.String "#") }} - {{- /* do nothing */}} -{{- else }} - {{- with .PageInner.GetPage (strings.TrimPrefix "./" $u.Path) }} - {{- $source := dict +{{- $url_struct := urls.Parse .Destination -}} +{{/* Ignore anchor links to other sections of the same page. */}} +{{- if not (or $url_struct.IsAbs (strings.HasPrefix $url_struct.String "#")) }} + {{- $page_path := strings.TrimPrefix "./" $url_struct.Path -}} + {{- with .PageInner.GetPage $page_path }} + {{- $page := dict "date" ($.Page.Date.UTC.Format "2006-01-02T15:04:05-07:00") "linkTitle" $.Page.LinkTitle - "permalink" $.Page.Permalink "relPermalink" $.Page.RelPermalink - "section" $.Page.Section - "title" $.Page.Title - "type" $.Page.Type }} - {{- $el := dict "source" $source "target" .RelPermalink }} - {{- site.Home.Store.Add "backlinks" (slice $el) }} + {{- $backlink := dict "page" $page "links_to" .RelPermalink }} + {{- site.Home.Store.Add "backlinks" (slice $backlink) }} {{- end }} {{- end }} diff --git a/modules/blox-tailwind/layouts/_default/home.backlinks.json b/modules/blox-tailwind/layouts/_default/home.backlinks.json index b51db689..e249a154 100644 --- a/modules/blox-tailwind/layouts/_default/home.backlinks.json +++ b/modules/blox-tailwind/layouts/_default/home.backlinks.json @@ -1,9 +1,2 @@ -{{- $debug := false }} -{{- range site.Pages }} - {{- $noop := .WordCount }} -{{- end }} -{{- if $debug }} - {{- jsonify (dict "indent" " ") (.Store.Get "backlinks") }} -{{- else }} - {{- jsonify (dict "indent" " ") (dict "comment" "This file is intentionally empty.") }} -{{- end }} +{{- /* Hugo 0.126 requires a `home` layout even if it serves no purpose, otherwise shows warning */ -}} +{{- jsonify dict -}} diff --git a/modules/blox-tailwind/layouts/partials/components/backlinks.html b/modules/blox-tailwind/layouts/partials/components/backlinks.html index 945d49de..3ff15dc2 100644 --- a/modules/blox-tailwind/layouts/partials/components/backlinks.html +++ b/modules/blox-tailwind/layouts/partials/components/backlinks.html @@ -1,20 +1,19 @@ {{ $show_backlinks := .Params.backlinks | default true }} {{ if $show_backlinks }} - {{- $debug := false }} - {{- $backlinks := site.Home.Store.Get "backlinks" | uniq }} - {{- with (where $backlinks "target" .RelPermalink) }} + {{ $backlinks := site.Home.Store.Get "backlinks" | uniq }} + {{ with (where $backlinks "links_to" .RelPermalink) }} - {{- end }} - {{- if $debug }} -
{{ jsonify (dict "indent" "  ") $backlinks }}
- {{- end }} + {{ end }} {{ end }}