mirror of
https://github.com/gethinode/hinode.git
synced 2025-10-13 21:13:21 +00:00
110 lines
4.3 KiB
HTML
110 lines
4.3 KiB
HTML
<!--
|
|
Generates a link for a given named link or url. The shortcode supports a single unnamed parameter, or various named
|
|
parameters. The unnamed parameter is recognized as a url if it starts with "http", else it is treated as either a
|
|
named link or internal reference (in that order). Any inner text is rendered as the link title, otherwise it uses
|
|
the host name (for external links), link title (for internal links), or anchor name (for any local references
|
|
containing a `#`). The shortcode supports the following named arguments:
|
|
"href" Required reference to either an external link (if it starts with http), a named link (if it can be
|
|
found in params.links), or internal reference. External and internal references may include an
|
|
anchor "#".
|
|
"name" Alias of href.
|
|
"url" Alias of href.
|
|
"cue" Optional flag to indicate if an external link should show a visual cue, defaults to setting
|
|
"main.externalLinks.cue" in the site's parameters.
|
|
"tab" Optional flag to indicate if an external link should open in a new tab, defaults to setting
|
|
"main.externalLinks.tab" in the site's parameters.
|
|
"case" Optional flag to indicate if the retrieved title (e.g. no inner text is provided) of an internal
|
|
link should use its original case, defaults to true. If false, the title is set to lower case.
|
|
"class" Optional class attribute of the anchor element.
|
|
-->
|
|
|
|
{{- $error := false -}}
|
|
{{ $href := "" }}
|
|
{{ $name := "" }}
|
|
{{ $url := "" }}
|
|
{{ $class := "" }}
|
|
{{ $case := true }}
|
|
{{ $cue := site.Params.main.externalLinks.cue }}
|
|
{{ $tab := site.Params.main.externalLinks.tab }}
|
|
{{ $text := trim .Inner " \r\n" | .Page.RenderString | safeHTML }}
|
|
{{- $anchor := "" -}}
|
|
|
|
{{ if .IsNamedParams }}
|
|
{{- $href = .Get "href" | default "" -}}
|
|
{{- $name = .Get "name" | default "" -}}
|
|
{{- $url = .Get "url" | default "" -}}
|
|
{{- $cue = .Get "cue" | default site.Params.main.externalLinks.cue -}}
|
|
{{- $tab = .Get "tab" | default site.Params.main.externalLinks.tab -}}
|
|
{{- $case = .Get "case" | default true -}}
|
|
{{- $class = .Get "class" | default "" -}}
|
|
{{ else }}
|
|
{{ $href = .Get 0 }}
|
|
{{ end }}
|
|
|
|
{{- $href = or (or $href $name) $url -}}
|
|
{{ if not $href }}
|
|
{{ errorf "Expected param 'href': %s" .Position -}}
|
|
{{ $error = true -}}
|
|
{{ end }}
|
|
|
|
{{- if hasPrefix $href "http" -}}
|
|
{{ $url = $href }}
|
|
{{- else if not (strings.Contains $href "/") -}}
|
|
{{ $url = index site.Params.links $href }}
|
|
{{- end -}}
|
|
|
|
{{ if not $url }}
|
|
{{- $href = strings.TrimPrefix "./" $href -}}
|
|
{{- if strings.Contains $href "#" }}
|
|
{{ $segments := split $href "#" }}
|
|
{{- if ne (len $segments) 2 }}
|
|
{{ errorf "Malformed path, expected one anchor '#' only: '%s' at %s" $href .Position -}}
|
|
{{ else }}
|
|
{{- $url = index $segments 0 -}}
|
|
{{- $anchor = index $segments 1 -}}
|
|
{{ if not $url }}
|
|
{{ $url = .Page.File.Path }}
|
|
{{ end }}
|
|
{{ end }}
|
|
{{ else }}
|
|
{{- $url = $href -}}
|
|
{{ end }}
|
|
{{ end }}
|
|
|
|
{{- $isExternal := ne (urls.Parse (absURL $url)).Host (urls.Parse site.BaseURL).Host -}}
|
|
{{- if not $isExternal -}}
|
|
{{- $url = strings.TrimSuffix "/" $url -}}
|
|
|
|
{{- $ref := "" -}}
|
|
{{- if not $ref -}}
|
|
{{- $ref = .Page.GetPage $url -}}
|
|
{{- end -}}
|
|
|
|
{{- if not $ref }}
|
|
{{- $segments := split $url "/" -}}
|
|
{{- if and (hasPrefix $url "/") (gt (len $segments) 1) -}}
|
|
{{- $prefix := index $segments 1 -}}
|
|
{{- $page := .Page -}}
|
|
{{ with index (where site.Sites "Language.Lang" $prefix) 0 }}
|
|
{{- $path := printf "/%s" (strings.TrimPrefix (printf "/%s/" $prefix) $url) }}
|
|
{{- $ref = .GetPage $path -}}
|
|
{{ end }}
|
|
{{- end -}}
|
|
{{- end -}}
|
|
|
|
{{- if not $ref -}}
|
|
{{- errorf "Cannot find page: '%s' at %s" $href .Position -}}
|
|
{{- $error = true -}}
|
|
{{- end -}}
|
|
{{- end -}}
|
|
|
|
{{ with $anchor }}
|
|
{{ $url = printf "%s#%s" $url .}}
|
|
{{ end }}
|
|
{{- if not $error -}}
|
|
{{ partial "utilities/link.html" (dict "destination" $url "text" $text "cue" $cue "tab" $tab "case" $case "class" $class "page" .Page) }}
|
|
{{- end -}}
|
|
|
|
|
|
|