mirror of
https://github.com/McShelby/hugo-theme-relearn.git
synced 2024-11-23 07:47:54 +00:00
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:
parent
78808adbbc
commit
305145a893
1 changed files with 48 additions and 27 deletions
|
@ -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 -}}
|
||||
<a href="{{ $url | safeURL }}"{{ if $title }} title="{{ $title }}"{{ end }}{{ if gt (len $target) 0 }} target="{{ $target }}"{{ end }}>{{ $content | safeHTML }}</a>
|
||||
{{- 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 -}}
|
||||
>{{ $content | safeHTML }}</a>
|
Loading…
Reference in a new issue