hugo-theme-lowtech/layouts/shortcodes/cloakemail.html

40 lines
2.2 KiB
HTML

{{/* https://github.com/martignoni/hugo-cloak-email */}}
{{/* Get address, protocol and other parameters */}}
{{- $address := .Get "address" | default (.Get 0) -}}
{{- $protocol := .Get "protocol" | default "mailto" -}}
{{- $class := .Get "class" -}}
{{- $displaytext := .Get "display" -}}
{{- $parts := split $address "@" -}}
{{- $user := (index $parts 0) -}}
{{- $domain := (index $parts 1) | default "" -}}
{{- $query := .Get "query" | default "" -}}
{{/* Compute md5 fingerprint */}}
{{- $fingerprint := md5 (print (.Get "address") $protocol (index (seq 999 | shuffle) 0)) | truncate 8 "" -}}
{{/* Set via CSS what is displayed when Javascript is disabled. Query is never displayed */}}
<style type="text/css">
#wjs-{{ $fingerprint }}.cloaked-e-mail:before {
content:{{ with $domain }}attr(data-domain) "\0040" {{ end }}attr(data-user);
unicode-bidi:bidi-override;
direction:rtl;
}
</style>
<span class="cloaked-e-mail" data-user="{{ range $index := seq (sub (len $user) 1) 0}}{{ substr $user $index 1}}{{ end }}"{{ with $domain }} data-domain="{{ range $index := seq (sub (len $domain) 1) 0}}{{ substr $domain $index 1}}{{ end }}"{{ end }} id="wjs-{{ $fingerprint }}"></span>
{{/* Alter display with Javascript by changing DOM */}}
<script id="id-{{ $fingerprint }}">
var scriptTag = document.getElementById("id-{{ $fingerprint }}");
var link = document.createElement("a");
var address = "{{ range $index := seq (sub (len $user) 1) 0}}{{ substr $user $index 1}}{{ end }}".split('').reverse().join(''){{ with $domain }} + "@" + "{{ range $index := seq (sub (len $domain) 1) 0}}{{ substr $domain $index 1}}{{ end }}".split('').reverse().join(''){{ with $query }} + "?" + "{{ range $index := seq (sub (len $query) 1) 0}}{{ substr $query $index 1}}{{ end }}".split('').reverse().join(''){{ end }}{{ end }};
link.href = {{ $protocol }} + ":" + address;
{{ with $displaytext }}
link.innerText = {{ $displaytext }};
{{ else }}
link.innerText = address.split('?')[0];
{{ end }}
{{ with $class }}
link.className = "{{ $class }}";
{{ end }}
scriptTag.parentElement.insertBefore(link, scriptTag.previousElementSibling);
scriptTag.parentElement.removeChild(scriptTag.previousElementSibling)
</script>
{{/* The end */}}