From 1221094d75eeef88ccb8937b3b28a27e5545e51f Mon Sep 17 00:00:00 2001 From: Simon C Date: Thu, 21 Oct 2021 21:47:42 +0200 Subject: [PATCH] feat: Simplify management of emails and phone number --- layouts/_default/_markup/render-link.html | 29 ++++------------------- layouts/partials/contact/mail.html | 9 ++++--- layouts/partials/contact/telephone.html | 9 ++++--- layouts/shortcodes/mail.html | 2 +- layouts/shortcodes/telephone.html | 2 +- 5 files changed, 19 insertions(+), 32 deletions(-) diff --git a/layouts/_default/_markup/render-link.html b/layouts/_default/_markup/render-link.html index e2ecb09..1afa930 100644 --- a/layouts/_default/_markup/render-link.html +++ b/layouts/_default/_markup/render-link.html @@ -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 }} {{ .Text | safeHTML }} -{{- 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) -}} - - +{{- else if $isMailtoLink -}} + {{ partial "contact/mail" .Destination }} +{{- else if $isTelLink -}} + {{ partial "contact/telephone" .Destination }} {{- end -}} diff --git a/layouts/partials/contact/mail.html b/layouts/partials/contact/mail.html index 8c8697a..ba9e703 100644 --- a/layouts/partials/contact/mail.html +++ b/layouts/partials/contact/mail.html @@ -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 -}} {{ range $index := seq (sub (len $mail) 1) 0}}{{ substr $mail $index 1}}{{ end }} -{{- end -}} diff --git a/layouts/partials/contact/telephone.html b/layouts/partials/contact/telephone.html index 9a73418..dc73641 100644 --- a/layouts/partials/contact/telephone.html +++ b/layouts/partials/contact/telephone.html @@ -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 -}} {{ range $index := seq (sub (len $tel) 1) 0}}{{ substr $tel $index 1}}{{ end }} -{{- end -}} diff --git a/layouts/shortcodes/mail.html b/layouts/shortcodes/mail.html index 6444fbe..6b81648 100644 --- a/layouts/shortcodes/mail.html +++ b/layouts/shortcodes/mail.html @@ -1 +1 @@ -{{- partial "contact/mail" . -}} +{{- partial "contact/mail" .Site.Data.contact.mail -}} diff --git a/layouts/shortcodes/telephone.html b/layouts/shortcodes/telephone.html index 8b944b0..fe6b17c 100644 --- a/layouts/shortcodes/telephone.html +++ b/layouts/shortcodes/telephone.html @@ -1 +1 @@ -{{- partial "contact/telephone" . -}} +{{- partial "contact/telephone" .Site.Data.contact.telephone -}}