2024-04-27 12:38:19 +00:00
{{- /* based on Hugo 0.125.5 render-link.html */}}
2023-07-27 14:14:55 +00:00
{{- $page := .page }}
{{- if and (not $page) .context }}
{{- $page = .context }}
2024-09-29 21:54:17 +00:00
{{- warnf "%q: DEPRECATED parameter 'context' for shortcode 'link' found, use 'page' instead; see https://mcshelby.github.io/hugo-theme-relearn/introduction/releasenotes/5/#5-18-0" $page.File.Filename }}
2023-07-27 14:14:55 +00:00
{{- end }}
2024-05-28 19:50:58 +00:00
{{- if strings.HasPrefix .url "HAHAHUGOSHORTCODE" }}
2023-10-27 22:36:30 +00:00
{{- warnf "%q: WARNING you must call the ref / relref shortcode with '%% %%' instead of '< >' to work correctly for the anchor target attribute" $page.File.Filename }}
2023-10-10 17:36:59 +00:00
{{- end }}
2024-02-16 23:49:50 +00:00
{{- $attributes := dict }}
2022-12-16 17:41:22 +00:00
{{- $title := .title | default "" }}
{{- $title = trim $title " " }}
2024-04-22 20:43:59 +00:00
{{- $attributes = $attributes | merge (dict "title" ($title | transform.HTMLEscape)) }}
2022-12-16 17:41:22 +00:00
{{- $content := .content }}
{{- $target := .target | default "" }}
2024-05-28 19:50:58 +00:00
{{- $u := urls.Parse .url -}}
2024-02-16 23:49:50 +00:00
{{- $href := $u.String }}
2024-02-22 18:06:33 +00:00
{{- if $u.IsAbs }}
2024-04-07 11:44:48 +00:00
{{- $attributes = merge $attributes (dict "rel" "external") }}
2022-12-16 17:41:22 +00:00
{{- $target = "_blank" }}
2023-07-27 14:14:55 +00:00
{{- if isset $page.Site.Params "externallinktarget" }}
{{- $target = $page.Site.Params.externalLinkTarget }}
2022-12-23 11:24:04 +00:00
{{- end }}
2024-02-16 23:49:50 +00:00
{{- $attributes = $attributes | merge (dict "target" $target) }}
2024-02-22 18:06:33 +00:00
{{- else }}
2024-02-22 22:00:38 +00:00
{{- $linkPage := "" }}
2024-05-28 19:50:58 +00:00
{{- $path := strings.TrimPrefix "./" $u.Path }}
2024-02-22 18:06:33 +00:00
{{- if $path }}
{{- with or
($page.Page.GetPage $path)
2024-04-07 11:44:48 +00:00
($page.Page.GetPage (strings.TrimRight "/" $path))
2024-02-22 18:06:33 +00:00
($page.Page.Resources.Get $path)
(resources.Get $path)
}}
{{- $linkPage = . }}
{{- else }}
{{- /* is it a link into another translation? */}}
{{- if strings.HasPrefix $path "/" }}
2024-03-04 20:04:07 +00:00
{{- range $page.AllTranslations }}
2024-02-22 18:06:33 +00:00
{{- $lang := .Language.Lang }}
{{- $prefix := printf "/%s" $lang }}
{{- $suffix := strings.TrimPrefix $prefix $path | default "/" }}
2024-03-04 20:04:07 +00:00
{{- /* with the second check we check if the prefix was finished;
eg. /pir/index.html vs. /pirate/index.html, were the latter is
an external address outside of this site */}}
2024-02-22 18:06:33 +00:00
{{- if and (strings.HasPrefix $path $prefix) (strings.HasPrefix $suffix "/") }}
{{- with or
(.GetPage $suffix)
2024-04-07 11:44:48 +00:00
(.GetPage (strings.TrimRight "/" $suffix))
2024-02-22 18:06:33 +00:00
(.Resources.Get $suffix)
(resources.Get $suffix)
}}
{{- $linkPage = . }}
{{- break }}
{{- end }}
2023-10-24 20:49:12 +00:00
{{- end }}
{{- end }}
{{- end }}
{{- end }}
2024-02-22 18:06:33 +00:00
{{- else }}
{{- $linkPage = $page.Page }}
2024-02-16 23:49:50 +00:00
{{- end }}
2024-08-02 08:32:31 +00:00
{{- $errorlevel := or $page.Params.link.errorlevel $page.Site.Params.link.errorlevel }}
2024-02-16 23:49:50 +00:00
{{- with $linkPage }}
2024-08-29 10:28:34 +00:00
{{- $href = partial "permalink.gotmpl" (dict "to" .) }}
2024-02-16 23:49:50 +00:00
{{- with $u.RawQuery }}
{{- $href = printf "%s?%s" $href . }}
{{- end }}
{{- with $u.Fragment }}
2024-04-07 11:42:01 +00:00
{{- $ctx := dict
"contentPath" $page.File.Filename
2024-08-02 08:32:31 +00:00
"errorLevel" $errorlevel
2024-04-07 11:42:01 +00:00
"page" $linkPage
"parsedURL" $u
"renderHookName" "link"
}}
{{- partial "inline/h-rh-l/validate-fragment.html" $ctx }}
2024-02-16 23:49:50 +00:00
{{- $href = printf "%s#%s" $href . }}
2023-10-24 20:49:12 +00:00
{{- end }}
2024-02-16 23:49:50 +00:00
{{- else }}
2024-08-02 08:32:31 +00:00
{{- if eq $errorlevel "warning" }}
2024-05-28 19:50:58 +00:00
{{- warnf "%q: link '%s' is not a page or a resource but linked anyways" $page.File.Filename .url }}
2024-08-02 08:32:31 +00:00
{{- else if eq $errorlevel "error" }}
2024-05-28 19:50:58 +00:00
{{- errorf "%q: link '%s' is not a page or a resource" $page.File.Filename .url }}
2024-02-17 23:50:55 +00:00
{{- end }}
2023-10-24 20:49:12 +00:00
{{- end }}
2024-02-16 23:49:50 +00:00
{{- end }}
{{- $attributes = $attributes | merge (dict "href" $href) -}}
< a
{{- range $k, $v := $attributes }}
{{- if $v }}
{{- printf " %s=%q" $k $v | safeHTMLAttr }}
{{- end }}
2024-04-07 11:42:01 +00:00
{{- end }}>{{ $content | safeHTML }}< / a >
{{- define "partials/inline/h-rh-l/validate-fragment.html" }}
{{- /*
Validates the fragment portion of a link destination.
@context {string} contentPath The page containing the link.
@context {string} errorLevel The error level when unable to resolve destination; ignore (default), warning, or error.
@context {page} page The page corresponding to the link destination
@context {struct} parsedURL The link destination parsed by urls.Parse.
@context {string} renderHookName The name of the render hook.
*/}}
{{- /* Initialize. */}}
{{- $contentPath := .contentPath }}
{{- $errorLevel := .errorLevel }}
{{- $p := .page }}
{{- $u := .parsedURL }}
{{- $renderHookName := .renderHookName }}
{{- /* Validate. */}}
{{- with $u.Fragment }}
{{- if $p.Fragments.Identifiers.Contains . }}
{{- if gt ($p.Fragments.Identifiers.Count .) 1 }}
{{- $msg := printf "%q: duplicate heading ID %q found" $contentPath . }}
{{- if eq $errorLevel "warning" }}
{{- warnf $msg }}
{{- else if eq $errorLevel "error" }}
{{- errorf $msg }}
{{- end }}
{{- end }}
{{- else }}
{{- /* Determine target path for warning and error message. */}}
{{- $targetPath := "" }}
{{- with $p.File }}
{{- $targetPath = .Path }}
{{- else }}
{{- $targetPath = .Path }}
{{- end }}
{{- /* Set common message. */}}
{{- $msg := printf "%q: heading ID %q not found in %q" $contentPath . $targetPath }}
{{- if eq $targetPath $contentPath }}
{{- $msg := printf "%q: heading ID %q not found" $contentPath . }}
{{- end }}
{{- /* Throw warning or error. */}}
{{- if eq $errorLevel "warning" }}
{{- warnf $msg }}
{{- else if eq $errorLevel "error" }}
{{- errorf $msg }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}