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