From 305145a893852079194012d3db524f52d595540a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Sat, 17 Feb 2024 00:49:50 +0100 Subject: [PATCH] link: overhaul #748 - sync with Hugo 0.123.0 default behavior - fix lookup if path contains dots - also lookup for resources --- layouts/partials/shortcodes/link.html | 75 +++++++++++++++++---------- 1 file changed, 48 insertions(+), 27 deletions(-) diff --git a/layouts/partials/shortcodes/link.html b/layouts/partials/shortcodes/link.html index 465757dfdc..a4369078c3 100644 --- a/layouts/partials/shortcodes/link.html +++ b/layouts/partials/shortcodes/link.html @@ -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 }} - {{- else }} - {{- /* is it a link into another translation? */}} + {{- $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 }} {{- end }} -{{- end -}} -{{ $content | safeHTML }} \ No newline at end of file + {{- 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) -}} +{{ $content | safeHTML }} \ No newline at end of file