mirror of
https://github.com/by-jp/www.byjp.me.git
synced 2025-08-09 09:46:11 +01:00
22 lines
No EOL
1.3 KiB
HTML
22 lines
No EOL
1.3 KiB
HTML
{{- $url := .url -}}
|
|
{{- $title := .Title -}}
|
|
{{- $isExternal := or (strings.HasPrefix $url "https://") (strings.HasPrefix $url "http://") -}}
|
|
{{- $text := .Text | default $url -}}
|
|
{{/* It pains me that Hugo doesn't have a urls.IsValid method, and has no error catching to allow me to try urls.Parse and catch failures */}}
|
|
{{- if gt (len (strings.FindRE `^https?:\/\/[A-z0-9_-]*?[:]?[A-z0-9_-]*?[@]?[A-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$` $text)) 0 -}}
|
|
{{- $url := urls.Parse $text -}}
|
|
{{- $text = strings.TrimPrefix "www." $url.Hostname -}}
|
|
{{- if lt (len $url.Path) 16 -}}
|
|
{{- $text = path.Join $text $url.Path -}}
|
|
{{- else -}}
|
|
{{- $parts := strings.Split $url.Path "/" -}}
|
|
{{- $last := index ($parts | last 1) 0 -}}
|
|
{{- if gt (len $last) 16 -}}
|
|
{{- $last = printf "%s…" (strings.Substr $last 0 16) -}}
|
|
{{- end -}}
|
|
{{- $text = path.Join $text "…" $last -}}
|
|
{{- end -}}
|
|
{{- end -}}
|
|
{{- $ref := partial "reference.obj" (dict "url" $url "references" .references) -}}
|
|
{{- $url = $ref.archiveURL | default $url -}}
|
|
<a href="{{ $url | safeURL }}"{{ with .Class }} class="{{ . }}"{{ end }}{{ with $title }} title="{{ . }}"{{ end }}{{ if $isExternal }} target="_blank"{{ end }}{{ with $ref.rel }} rel="{{ . }}"{{ end }}>{{ $text | safeHTML }}</a> |