Merge pull request #47620 from Andygol/render-link

Implementation of the Link Decorator function for processing localised links 🈂️
pull/47666/head
Kubernetes Prow Robot 2024-08-24 17:50:09 +01:00 committed by GitHub
commit 81e1083e91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,53 @@
{{- $u := urls.Parse .Destination -}}
{{- $href := $u.String -}}
{{- $langCode := .Page.Site.Language.Lang -}}
{{- if strings.HasPrefix $u.String "#" }}
{{- /* Anchor link in the document, leave unchanged */ -}}
{{- else if $u.IsAbs -}}
{{- /* External link, leave unchanged */ -}}
{{- else if strings.HasPrefix $u.Path (printf "/%s/" $langCode) -}}
{{- /* Internal link in the current language, leave unchanged */ -}}
{{- else if strings.HasPrefix $u.Path "/" -}}
{{- $localizedPath := printf "/%s%s" $langCode $u.Path -}}
{{- with or
(.Page.GetPage $localizedPath)
(.Page.Resources.Get $localizedPath)
(resources.Get $localizedPath)
-}}
{{- $href = .RelPermalink -}}
{{- else -}}
{{- $path := strings.TrimPrefix "./" $u.Path }}
{{- with or
(.Page.GetPage $path)
(.Page.Resources.Get $path)
(resources.Get $path)
-}}
{{- $href = .RelPermalink -}}
{{- end -}}
{{- end -}}
{{- if and $u.RawQuery (not (strings.Contains $href "?")) -}}
{{- $href = printf "%s?%s" $href $u.RawQuery -}}
{{- end -}}
{{- if and $u.Fragment (not (strings.Contains $href "#")) -}}
{{- $href = printf "%s#%s" $href $u.Fragment -}}
{{- end -}}
{{- else -}}
{{- /* Other internal links, leave unchanged */ -}}
{{- end -}}
{{- $attributes := dict "href" $href "title" (.Title | transform.HTMLEscape) -}}
<a
{{- range $k, $v := $attributes -}}
{{- if $v -}}
{{- printf " %s=%q" $k $v | safeHTMLAttr -}}
{{- end -}}
{{- end -}}
>{{ .Text | safeHTML }}</a>
{{- /**/ -}}