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 # This create an unclose node
layouts/_default/_markup/render-heading.html layouts/_default/_markup
layouts/_default/index.json layouts/_default/index.json
layouts/shortcodes/screenshot.html layouts/shortcodes/screenshot.html
layouts/shortcodes/figure.html layouts/shortcodes/figure.html

View File

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