feat: Simplify management of emails and phone number

This commit is contained in:
Simon 2021-10-21 21:47:42 +02:00
parent 8ca72479d4
commit 1221094d75
5 changed files with 19 additions and 32 deletions

View File

@ -5,31 +5,12 @@
{{ $isWebLink := or $isExternalLink (eq 0 (.Destination | strings.Count ":")) }}
{{ $isTelLink := strings.HasPrefix .Destination "tel:" }}
{{ $isMailtoLink := strings.HasPrefix .Destination "mailto:" }}
{{ $hasSlash := in .Destination "/" }}
{{- if $isWebLink -}}
{{ $hasSlash := in .Destination "/" }}
{{ $link := cond (and $isWebLink (and (not $hasSlash) (not $isExternalLink))) (path.Join "/" .Page.File.Dir .Destination) .Destination }}
<a href="{{ $link | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if $isExternalLink }} target="_blank" rel="noopener"{{ end }}>{{ .Text | safeHTML }}</a>
{{- else -}}
{{- $separators := cond $isMailtoLink ":@." ":" -}}
{{- $regex := cond $isMailtoLink "[a-z]*:(.*)" "[a-z]*:([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})"}}
{{- $replace := cond $isMailtoLink "$1" "$1 $2 $3 $4 $5" -}}
{{- $string := .Destination -}}
{{- $params := slice -}}
{{- range split $separators "" }}
{{ $parts := split $string . }}
{{ $params = $params | append (index $parts 0) }}
{{ $string = index $parts 1 }}
{{ end }}
{{- $params = $params | append (string $string) -}}
<span
class="obfuscate{{ if $isMailtoLink }} at{{ end }}"
data-separators="{{ $separators }}"
{{ range $param_index, $param_value := $params }}
data-param{{add $param_index 1}}="{{ range $index := seq (sub (len $param_value) 1) 0}}{{ substr $param_value $index 1}}{{ end }}"
{{ end }}
data-regex="{{ $regex }}"
data-replace="{{ $replace }}"
>
</span>
{{- else if $isMailtoLink -}}
{{ partial "contact/mail" .Destination }}
{{- else if $isTelLink -}}
{{ partial "contact/telephone" .Destination }}
{{- end -}}

View File

@ -1,9 +1,12 @@
{{- with .Site.Data.contact.mail -}}
{{- $protocol := "mailto" -}}
{{- $mail := . -}}
{{- $mailWithProtocol := print $protocol ":" . -}}
{{- $mailWithProtocol := $mail -}}
{{- if strings.HasPrefix $mail $protocol -}}
{{- $mail = substr $mail 7 -}}
{{- else -}}
{{- $mailWithProtocol = print $protocol ":" $mail -}}
{{- end -}}
<a
class="mail obfuscate"
data-link="{{ range $index := seq (sub (len $mailWithProtocol) 1) 0}}{{ substr $mailWithProtocol $index 1}}{{ end }}"
>{{ range $index := seq (sub (len $mail) 1) 0}}{{ substr $mail $index 1}}{{ end }}</a>
{{- end -}}

View File

@ -1,9 +1,12 @@
{{- with .Site.Data.contact.telephone -}}
{{- $protocol := "tel" -}}
{{- $tel := . -}}
{{- $telWithProtocol := print $protocol ":" (replace $tel " " "") -}}
{{- $telWithProtocol := $tel -}}
{{- if strings.HasPrefix $tel $protocol -}}
{{- $tel = substr $tel 4 -}}
{{- else -}}
{{- $telWithProtocol := print $protocol ":" (replace $tel " " "") -}}
{{- end -}}
<a
class="tel obfuscate"
data-link="{{ range $index := seq (sub (len $telWithProtocol) 1) 0}}{{ substr $telWithProtocol $index 1}}{{ end }}"
>{{ range $index := seq (sub (len $tel) 1) 0}}{{ substr $tel $index 1}}{{ end }}</a>
{{- end -}}

View File

@ -1 +1 @@
{{- partial "contact/mail" . -}}
{{- partial "contact/mail" .Site.Data.contact.mail -}}

View File

@ -1 +1 @@
{{- partial "contact/telephone" . -}}
{{- partial "contact/telephone" .Site.Data.contact.telephone -}}