-
Notifications
You must be signed in to change notification settings - Fork 47
/
link.html
109 lines (96 loc) · 4.3 KB
/
link.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!--
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 -}}