link: overhaul #748

- sync with Hugo 0.123.0 default behavior
- fix lookup if path contains dots
- also lookup for resources
This commit is contained in:
Sören Weber 2024-02-17 00:49:50 +01:00
parent 78808adbbc
commit 305145a893
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D

View file

@ -7,45 +7,66 @@
{{- 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 }}
{{- $title := .title | default "" }}
{{- $title = trim $title " " }}
{{- $attributes = $attributes | merge (dict "title" $title) }}
{{- $content := .content }}
{{- $target := .target | default "" }}
{{- if and (eq (len $target) 0) (or (strings.HasPrefix $url "http://") (strings.HasPrefix $url "https://") ) }}
{{- $u := urls.Parse $url }}
{{- $href := $u.String }}
{{- $path := $u.Path }}
{{- if $u.IsAbs }}
{{- $target = "_blank" }}
{{- if isset $page.Site.Params "externallinktarget" }}
{{- $target = $page.Site.Params.externalLinkTarget }}
{{- end }}
{{- else }}
{{- $url = urls.Parse $url }}
{{- if and $url.Path (not (path.Ext $url.Path)) }}
{{- /* ignore old style links with given extension */}}
{{- $found := false }}
{{- $fragment := "" }}
{{- with $url.Fragment }}
{{- $fragment = printf "#%s" . }}
{{- end }}
{{- $path := printf "%s/" (strings.TrimPrefix "/" (strings.TrimSuffix "/" $url.Path)) }}
{{- with $page.Page.GetPage (strings.TrimSuffix "/" $path | default "/") }}
{{- $url = printf "%s%s" (partial "relLangPrettyUglyURL.hugo" (dict "to" .)) $fragment }}
{{- $found = true }}
{{- $attributes = $attributes | merge (dict "target" $target) }}
{{- else if $path }}
{{- $linkPage := "" }}
{{- with or
($page.Page.GetPage $path)
($page.Page.Resources.Get $path)
(resources.Get $path)
}}
{{- $linkPage = . }}
{{- else }}
{{- /* is it a link into another translation? */}}
{{- if strings.HasPrefix $path "/" }}
{{- range $page.Sites }}
{{- $lang := .Language.Lang }}
{{- $prefix := printf "%s/" $lang }}
{{- if strings.HasPrefix $path $prefix }}
{{- $path = strings.TrimPrefix $prefix $path | default "/" }}
{{- with .GetPage (strings.TrimSuffix "/" $path | default "/") }}
{{- $url = printf "%s%s" (partial "relLangPrettyUglyURL.hugo" (dict "to" .)) $fragment }}
{{- $found = true }}
{{- $prefix := printf "/%s" $lang }}
{{- $suffix := strings.TrimPrefix $prefix $path | default "/" }}
{{- if and (strings.HasPrefix $path $prefix) (strings.HasPrefix $suffix "/") }}
{{- with or
(.GetPage $suffix)
(.Resources.Get $suffix)
(resources.Get $suffix)
}}
{{- $linkPage = . }}
{{- break }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- if not $found }}
{{- warnf "%q: no page was found for link '%s'" $page.File.Filename $url.Path }}
{{- end }}
{{- with $linkPage }}
{{- $href = partial "relLangPrettyUglyURL.hugo" (dict "to" .) }}
{{- with $u.RawQuery }}
{{- $href = printf "%s?%s" $href . }}
{{- end }}
{{- with $u.Fragment }}
{{- $href = printf "%s#%s" $href . }}
{{- end }}
{{- else }}
{{- warnf "%q: no page was found for link '%s'" $page.File.Filename $u.Path }}
{{- end }}
{{- end }}
{{- $attributes = $attributes | merge (dict "href" $href) -}}
<a
{{- range $k, $v := $attributes }}
{{- if $v }}
{{- printf " %s=%q" $k $v | safeHTMLAttr }}
{{- end }}
{{- end -}}
<a href="{{ $url | safeURL }}"{{ if $title }} title="{{ $title }}"{{ end }}{{ if gt (len $target) 0 }} target="{{ $target }}"{{ end }}>{{ $content | safeHTML }}</a>
>{{ $content | safeHTML }}</a>