mirror of
https://github.com/by-jp/www.byjp.me.git
synced 2025-08-24 11:43:11 +01:00
- Remove i18n & include drafts in dev build - Links have agree/accurate indicators - Also introduces `references` as a standard Params structure for my blog. I don't particularly like that it's a map rather than an array, but this is what my micropib enpoint currently creates, so I'll stick with this until I can be bothered to switch that over.
29 lines
No EOL
1.4 KiB
HTML
29 lines
No EOL
1.4 KiB
HTML
{{- $destination := .Destination -}}
|
|
{{- $title := .Title -}}
|
|
{{- $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 -}}
|
|
{{- $ref := dict -}}
|
|
{{- range $k, $refSpec := .Page.Params.references -}}
|
|
{{- if eq $refSpec.url $destination -}}
|
|
{{- $ref = $refSpec -}}
|
|
{{- if and (ne $title $ref.name) (ne $text $ref.name) -}}
|
|
{{- $title = printf "External article: %s" $ref.name -}}
|
|
{{- end -}}
|
|
{{- end -}}
|
|
{{- end -}}
|
|
<a href="{{ $destination | safeURL }}"{{ with .Class }} class="{{ . }}"{{ end }}{{ with $title }} title="{{ . }}"{{ end }}{{ if $isExternal }} target="_blank"{{ end }}{{ with $ref.rel }} rel="{{ . }}"{{ end }}>{{ $text }}</a> |