www.byjp.me/layouts/_default/_markup/render-link.html
JP Hastings-Spital 6320396733 Import archived tweets
A first pass at importing my tweets. Includes the code I used to do it.

I've made some manual edits (to add code fence blocks, and fix some annoying encoding issues), but this should be a decent start.
2024-01-18 22:18:11 +00:00

18 lines
No EOL
997 B
HTML

{{- $isExternal := strings.HasPrefix .Destination "http" -}}
{{- $text := .Text -}}
{{/* 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 -}}
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if $isExternal }} target="_blank"{{ end }}>{{ $text }}</a>