{{- /*
  Render hook: resolve internal .md links to their Hugo RelPermalink.

  Plain "strip .md" is not enough under pretty URLs: a page served at
  /guides/DEPLOYMENT-OVERVIEW/ would resolve a bare relative href
  "CLOUD-DEPLOY-WALKTHROUGH" to /guides/DEPLOYMENT-OVERVIEW/CLOUD-DEPLOY-WALKTHROUGH
  (404).  Using site.GetPage gives the canonical /guides/CLOUD-DEPLOY-WALKTHROUGH/.

  Handles:
    - same-directory links:         CLOUD-DEPLOY-WALKTHROUGH.md
    - cross-directory links:        ../architecture/DEVICE-LOCAL-INSTALL.md
    - anchored links:               OTHER-PAGE.md#section
    - external links (http/https):  passed through unchanged, opened in new tab
*/ -}}
{{- $dest := .Destination -}}
{{- $isAbs := or (strings.HasPrefix $dest "http://") (strings.HasPrefix $dest "https://") (strings.HasPrefix $dest "//") -}}
{{- if not $isAbs -}}
  {{- /* Split off any fragment (#anchor) */ -}}
  {{- $fragment := "" -}}
  {{- $base := $dest -}}
  {{- if strings.Contains $dest "#" -}}
    {{- $parts := split $dest "#" -}}
    {{- $base = index $parts 0 -}}
    {{- $fragment = printf "#%s" (index $parts 1) -}}
  {{- end -}}
  {{- if strings.HasSuffix $base ".md" -}}
    {{- $pathNoExt := strings.TrimSuffix ".md" $base -}}
    {{- /* Resolve relative to the current content file's directory */ -}}
    {{- $dir := "" -}}
    {{- with .Page.File }}{{ $dir = .Dir }}{{ end -}}
    {{- $resolved := site.GetPage (path.Join $dir $pathNoExt) -}}
    {{- if $resolved -}}
      {{- $dest = printf "%s%s" $resolved.RelPermalink $fragment -}}
    {{- else -}}
      {{- /* Fallback: strip .md (better than leaving the extension) */ -}}
      {{- $dest = printf "%s%s" $pathNoExt $fragment -}}
    {{- end -}}
  {{- end -}}
{{- end -}}
<a href="{{ $dest }}"
  {{- with .Title }} title="{{ . }}"{{ end -}}
  {{- if $isAbs }} target="_blank" rel="noopener noreferrer"{{ end -}}
>{{ .Text | safeHTML }}</a>
{{- /* Whitespace trimmed intentionally */ -}}
