{{- /*
Link rendering partial for regular and card-style links.
- Auto-detects external links and applies target/rel policies.
- Supports optional icon, download hint, and external icon display.
- Supports card mode with URL meta and favicon fallback.
@param {String} .Destination target URL
@param {String} [.Content] link label HTML
@param {String} [.Title] title attribute
@param {String} [.Class] custom CSS classes
@param {String} [.Rel] rel attribute value
@param {Boolean} [.Newtab] force external behavior
@param {Boolean} [.Noreferrer] whether to include noreferrer (default: true)
@param {Boolean} [.ExternalIcon] whether to show external-link icon
@param {Object} [.Icon] icon params for plugin/icon.html
@param {Boolean} [.Card] whether to render card link layout
@param {String} [.CardIcon] card icon URL or icon class
@param {String} [.Download] download attribute value
@param {Boolean} [.IsGuarded] explicitly enable or disable link guard
@param {Object} [.Page] current page context
*/ -}}
{{- /* Regular link rendering */ -}}
{{- define "render-link-regular" -}}
{{- $ctx := .ctx -}}
{{- $config := .config -}}
{{- $isExternal := .isExternal -}}
{{- $externalIcon := $ctx.ExternalIcon | default $config.externalIcon -}}
{{- with $ctx.Icon -}}
{{- partial "plugin/icon.html" . }}
{{ end -}}
{{- with $ctx.Content -}}
{{- . | safeHTML -}}
{{- end -}}
{{- if
$externalIcon
| and $isExternal
| and (not (hasPrefix $ctx.Content `
`))
| and (not (strings.HasSuffix $ctx.Content `):`))
-}}
{{- if $ctx.Download -}}
{{- dict "Class" "fa-solid fa-cloud-download-alt ms-1 text-secondary" | partial "plugin/icon.html" -}}
{{- else -}}
{{- dict "Class" "fa-solid fa-external-link-alt fa-xs ms-1 text-secondary" | partial "plugin/icon.html" -}}
{{- end -}}
{{- else if $ctx.Download }}
{{- dict "Class" "fa-solid fa-download ms-1 text-secondary" | partial "plugin/icon.html" -}}
{{- end -}}
{{- end -}}
{{- /* Card link rendering */ -}}
{{- define "render-link-card" -}}
{{- $ctx := .ctx -}}
{{- $url := .url -}}
{{- $cardIcon := $ctx.CardIcon -}}
{{- if (not $cardIcon) | and $url.Host -}}
{{- with partial "function/get-remote-image.html" (dict "Src" (add "https://favicon.im/" $url.Host)) -}}
{{- $cardIcon = .RelPermalink -}}
{{- else with partial "function/get-remote-image.html" (dict "Src" (add "https://api.lruihao.cn/google/s2/favicons?sz=64&domain=" $url.Host)) -}}
{{- $cardIcon = .RelPermalink -}}
{{- end -}}
{{- end -}}
{{- with $ctx.Icon -}}
{{- partial "plugin/icon.html" . }}
{{ end -}}
{{- with $ctx.Content -}}
{{- . | safeHTML -}}
{{- end -}}
{{- $url.String | safeURL | strings.TrimPrefix (add $url.Scheme "://") -}}
{{- if $ctx.Download -}}
{{- else -}}
{{- with $cardIcon -}}
{{- if (strings.HasPrefix . "fa") -}}
{{- else -}}
{{- end -}}
{{- else -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $blockSchemes := slice "javascript" "mailto" "tel" "file" "data" "vscode" "trae" -}}
{{- $config := partial "function/snake2camel.html" site.Params.link -}}
{{- $rel := .Rel | default "" -}}
{{- $isExternal := false -}}
{{- $noreferrer := true -}}
{{- if eq .Noreferrer false -}}
{{- $noreferrer = false -}}
{{- end -}}
{{- $url := urls.Parse .Destination -}}
{{- if $url.IsAbs | and (not (in $blockSchemes $url.Scheme)) -}}
{{- $isExternal = not (strings.HasPrefix .Destination site.BaseURL) -}}
{{- end -}}
{{- if $isExternal -}}
{{- $rel = add $rel " external nofollow" -}}
{{- if $noreferrer -}}
{{- $rel = add $rel " noopener noreferrer" -}}
{{- end -}}
{{- end -}}
{{- $class := .Class | default "" -}}
{{- if eq .Card true -}}
{{- $class = trim (printf "%v %v" "card-link" $class) " " -}}
{{- end -}}
{{- /* External link guard */ -}}
{{- $isGuarded := false -}}
{{- $guardConfig := $config.guard -}}
{{- with .Page.Params.link_guard -}}
{{- if reflect.IsMap . -}}
{{- $guardConfig = (partial "function/snake2camel.html" .) | merge $guardConfig -}}
{{- else -}}
{{- $guardConfig = (dict "enable" .) | merge $guardConfig -}}
{{- end -}}
{{- end -}}
{{- if
$guardConfig.enable
| and $isExternal
| and (not (in $guardConfig.allowDomains $url.Host))
-}}
{{- $isGuarded = .IsGuarded | default true -}}
{{- end -}}
{{- $destination := cond $isGuarded (add (relLangURL "link") "?target=" (urlquery .Destination)) .Destination -}}
{{- $attrs := printf `href="%s"` ($destination | safeURL) -}}
{{- with .Title -}}
{{- $attrs = printf ` title="%s"` . | add $attrs -}}
{{- end -}}
{{- if $isExternal | or .Newtab -}}
{{- $attrs = add $attrs ` target="_blank"` -}}
{{- end -}}
{{- with $rel -}}
{{- $attrs = printf ` rel="%s"` (trim . " ") | add $attrs -}}
{{- end -}}
{{- with $class -}}
{{- $attrs = printf ` class="%s"` . | add $attrs -}}
{{- end -}}
{{- with .Download -}}
{{- $attrs = printf ` download="%s"` . | add $attrs -}}
{{- end -}}
{{- if $isGuarded | and (eq $guardConfig.mode "modal") -}}
{{- $attrs = add $attrs ` data-guard="modal"` -}}
{{- end -}}
{{- if eq .Card true -}}
{{- template "render-link-card" (dict "ctx" . "url" $url) -}}
{{- else -}}
{{- template "render-link-regular" (dict "ctx" . "config" $config "isExternal" $isExternal) -}}
{{- end -}}
{{- /* EOF */ -}}