link: adjust to Hugo's build-in code #858

- resolve render hook destination with leading ./
- adjust variable naming
- remove special case for section resources borrowed from https://www.veriphor.com/articles/link-and-image-render-hooks/
This commit is contained in:
Sören Weber 2024-05-28 21:50:58 +02:00
parent 833ccac2d2
commit db38203826
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D

View file

@ -4,8 +4,7 @@
{{- $page = .context }}
{{- warnf "%q: DEPRECATED parameter 'context' for shortcode 'link' found, use 'page' instead; see https://mcshelby.github.io/hugo-theme-relearn/basics/migration#5180" $page.File.Filename }}
{{- end }}
{{- $url := .url }}
{{- if strings.HasPrefix $url "HAHAHUGOSHORTCODE" }}
{{- if strings.HasPrefix .url "HAHAHUGOSHORTCODE" }}
{{- warnf "%q: WARNING you must call the ref / relref shortcode with '%% %%' instead of '< >' to work correctly for the anchor target attribute" $page.File.Filename }}
{{- end }}
{{- $attributes := dict }}
@ -14,9 +13,8 @@
{{- $attributes = $attributes | merge (dict "title" ($title | transform.HTMLEscape)) }}
{{- $content := .content }}
{{- $target := .target | default "" }}
{{- $u := urls.Parse $url }}
{{- $u := urls.Parse .url -}}
{{- $href := $u.String }}
{{- $path := $u.Path }}
{{- if $u.IsAbs }}
{{- $attributes = merge $attributes (dict "rel" "external") }}
{{- $target = "_blank" }}
@ -26,12 +24,12 @@
{{- $attributes = $attributes | merge (dict "target" $target) }}
{{- else }}
{{- $linkPage := "" }}
{{- $path := strings.TrimPrefix "./" $u.Path }}
{{- if $path }}
{{- with or
($page.Page.GetPage $path)
($page.Page.GetPage (strings.TrimRight "/" $path))
($page.Page.Resources.Get $path)
(and (ne $page.Page.BundleType "leaf") ($page.Page.CurrentSection.Resources.Get $path))
(resources.Get $path)
}}
{{- $linkPage = . }}
@ -50,7 +48,6 @@
(.GetPage $suffix)
(.GetPage (strings.TrimRight "/" $suffix))
(.Resources.Get $suffix)
(and (ne .BundleType "leaf") (.CurrentSection.Resources.Get $suffix))
(resources.Get $suffix)
}}
{{- $linkPage = . }}
@ -81,9 +78,9 @@
{{- end }}
{{- else }}
{{- if eq $page.Site.Params.link.errorlevel "warning" }}
{{- warnf "%q: link '%s' is not a page but linked anyways" $page.File.Filename $url }}
{{- warnf "%q: link '%s' is not a page or a resource but linked anyways" $page.File.Filename .url }}
{{- else if eq $page.Site.Params.link.errorlevel "error" }}
{{- errorf "%q: link '%s' is not a page" $page.File.Filename $url }}
{{- errorf "%q: link '%s' is not a page or a resource" $page.File.Filename .url }}
{{- end }}
{{- end }}
{{- end }}