feat: adopt Hugo embed render-link logic

This commit is contained in:
ZhenShuo Leo
2025-12-14 13:27:21 +08:00
parent 1be60f89da
commit f8417b8297
2 changed files with 28 additions and 9 deletions

View File

@@ -25,7 +25,7 @@ static
#
# This create an unclose node
layouts/_default/_markup/render-heading.html
layouts/_default/_markup
layouts/_default/index.json
layouts/shortcodes/screenshot.html
layouts/shortcodes/figure.html

View File

@@ -1,11 +1,30 @@
<a
href="{{ .Destination | safeURL }}"
{{- with .Title -}}
title="{{ . }}"
{{- end }}
{{- if or (strings.HasPrefix .Destination "http:") (strings.HasPrefix .Destination "https:") }}
target="_blank"
{{ end }}>
{{/* From Hugo, Apache v2 license
https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/_markup/render-link.html
*/}}
{{- $u := urls.Parse .Destination -}}
{{- $href := $u.String -}}
{{- if strings.HasPrefix $u.String "#" -}}
{{- $href = printf "%s#%s" .PageInner.RelPermalink $u.Fragment -}}
{{- else if and $href (not $u.IsAbs) -}}
{{- $path := strings.TrimPrefix "./" $u.Path -}}
{{- with or
($.PageInner.GetPage $path)
($.PageInner.Resources.Get $path)
(resources.Get $path)
-}}
{{- $href = .RelPermalink -}}
{{- with $u.RawQuery -}}
{{- $href = printf "%s?%s" $href . -}}
{{- end -}}
{{- with $u.Fragment -}}
{{- $href = printf "%s#%s" $href . -}}
{{- end -}}
{{- end -}}
{{- end -}}
<a href="{{ $href }}" {{ with .Title }}title="{{ . }}"{{ end }}
{{- if or (strings.HasPrefix .Destination "http:") (strings.HasPrefix .Destination "https:") }} target="_blank"{{ end }}>
{{- .Text | safeHTML -}}
</a>
{{- /* Trim EOF */ -}}