{{- $currentPage := .PageInner | default .Page -}}
{{- $text := .Text -}}
{{- $destination := .Destination -}}
{{- $reference := dict "kind" "other" "destination" $destination -}}
{{- if and (hasPrefix $destination "{") (hasSuffix $destination "}") -}}
{{- $reference = dict "kind" "formats" "destination" $destination -}}
{{- $smallcaps := false -}}
{{- range split (trim $destination "{}") "," -}}
{{- $format := trim . " " -}}
{{- if eq $format "sc" -}}
{{- $smallcaps = true -}}
{{- else -}}
{{- warnf "In %s unknown format: %s" (partial "page-source.html" $currentPage) $format -}}
{{- end -}}
{{- end -}}
{{- if $smallcaps -}}{{ $text | safeHTML }}{{- else -}}{{ $text | safeHTML }}{{- end -}}
{{- else if hasPrefix $destination "#" -}}
{{- $reference = dict "kind" "anchors" "destination" $destination -}}
§⁄{{ $text | safeHTML }}
{{- else if hasPrefix $destination (site.Params.sandpoints.catalogPrefix | default "/library/") -}}
{{- $bib := strings.TrimPrefix (site.Params.sandpoints.catalogPrefix | default "/library/") $destination -}}
{{- $book := index hugo.Data.books.catalog $bib -}}
{{- if not $book.title -}}{{ warnf "In %s bib:%s not found." (partial "page-source.html" $currentPage) $bib }}{{- end -}}
{{- $reference = dict "kind" "library" "destination" $destination -}}
bib⁄{{ $text | default $book.title | safeHTML }}
{{- else -}}
{{- $url := urls.Parse $destination -}}
{{- if not $url.Scheme -}}
{{- with site.GetPage $destination -}}
{{- $reference = dict "kind" "internal" "destination" $destination "path" .Path -}}
{{- partial "page-link.html" (dict "page" . "text" $text "label" .Section) -}}
{{- else -}}
{{- warnf "In %s broken link: %s" (partial "page-source.html" $currentPage) $destination -}}
{{ $text | default $destination | safeHTML }}
{{- end -}}
{{- else -}}
{{- if not (in (slice "http" "https" "mailto") $url.Scheme) -}}
{{- warnf "In %s unsupported link scheme %s: %s" (partial "page-source.html" $currentPage) $url.Scheme $destination -}}
{{- end -}}
{{- if hasPrefix $destination "http" -}}{{- $reference = dict "kind" "external" "destination" $destination -}}www⁄{{- end -}}
{{ $text | default $destination | safeHTML }}
{{- end -}}
{{- end -}}
{{- partial "content-reference.html" $reference -}}