Skip to content

Commit

Permalink
tplimpl: Added shortcode for cloaking email
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Karlsson committed Oct 3, 2017
1 parent 4fc67fe commit 7040971
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/content/content-management/shortcodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,26 @@ Using the preceding `youtube` example (without `autoplay="true"`), the following

{{< youtube w7Ft2ymGmfc >}}

### `cloakemail`

This shortcode obfuscate the given email address thereby hiding it from simpler bots. It works with and without Javascript. The email reversed in the output and is reversed by the javascript in order to display properly.

#### Example `cloakemail` Input

{{< code file="cloakemail-input.md" >}}
{{</* cloakemail "example@example.com" */>}}
{{< /code >}}

#### Example `cloakemail` Output

{{< output file="cloakemail-output.html" >}}
{{< cloakemail "example@example.com" >}}
{{< /output >}}

#### Example `cloakemail` Display

{{< cloakemail "example@example.com" >}}

## Create Custom Shortcodes

To learn more about creating custom shortcodes, see the [shortcode template documentation][].
Expand Down
29 changes: 29 additions & 0 deletions hugolib/embedded_shortcodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,32 @@ title: Shorty

}
}

func TestShortcodeEmail(t *testing.T) {
t.Parallel()

for _, this := range []struct {
in, expected string
}{
{
`{{< cloakcemail "example@example.com" >}}`,
`(?s)^<style type="text\/css">\.cloaked-e-mail:before{content:attr\(data-domain\) "\\0040" attr\(data-user\);unicode-bidi:bidi-override;direction:rtl;}<\/style><span class="cloaked-e-mail" data-user="elpmaxe" data-domain="moc\.elpmaxe"><\/span><script id="id">var scriptTag = document.getElementById\("id"\);var link = document.createElement\("a"\);var mail = "elpmaxe".split\(''\).reverse\(\).join\(''\) \+ "@" \+ "moc.elpmaxe".split\(''\).reverse\(\).join\(''\);link.href = "mailto:" \+ mail;link.innerText = mail;scriptTag.parentElement.insertBefore\(link, scriptTag.previousElementSibling\);scriptTag.parentElement.removeChild\(scriptTag.previousElementSibling\)<\/script>`,
},
} {
var (
cfg, fs = newTestCfg()
th = testHelper{cfg, fs, t}
)

writeSource(t, fs, filepath.Join("content", "simple.md"), fmt.Sprintf(`---
title: Shorty
---
%s`, this.in))
writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), `{{ .Content }}`)

buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})

th.assertFileContentRegexp(filepath.Join("public", "simple", "index.html"), this.expected)

}
}
1 change: 1 addition & 0 deletions tpl/tplimpl/template_embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (t *templateHandler) embedShortcodes() {
</div>
{{ end }}`)
t.addInternalShortcode("gist.html", `<script src="//gist.github.com/{{ index .Params 0 }}/{{ index .Params 1 }}.js{{if len .Params | eq 3 }}?file={{ index .Params 2 }}{{end}}"></script>`)
t.addInternalShortcode("cloakemail.html", `{{ $parts := split (index .Params 0) "@" }}{{ $user := (index $parts 0) }}{{ $domain := (index $parts 1) }}<style type="text/css">.cloaked-e-mail:before{content:attr(data-domain) "\0040" 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 }}" data-domain="{{ range $index := seq (sub (len $domain) 1) 0}}{{ substr $domain $index 1}}{{ end }}"></span><script id="id">var scriptTag = document.getElementById("id");var link = document.createElement("a");var mail = "{{ range $index := seq (sub (len $user) 1) 0}}{{ substr $user $index 1}}{{ end }}".split('').reverse().join('') + "@" + "{{ range $index := seq (sub (len $domain) 1) 0}}{{ substr $domain $index 1}}{{ end }}".split('').reverse().join('');link.href = "mailto:" + mail;link.innerText = mail;scriptTag.parentElement.insertBefore(link, scriptTag.previousElementSibling);scriptTag.parentElement.removeChild(scriptTag.previousElementSibling)</script>`)
t.addInternalShortcode("tweet.html", `{{ (getJSON "https://api.twitter.com/1/statuses/oembed.json?id=" (index .Params 0)).html | safeHTML }}`)
t.addInternalShortcode("instagram.html", `{{ if len .Params | eq 2 }}{{ if eq (.Get 1) "hidecaption" }}{{ with getJSON "https://api.instagram.com/oembed/?url=https://instagram.com/p/" (index .Params 0) "/&hidecaption=1" }}{{ .html | safeHTML }}{{ end }}{{ end }}{{ else }}{{ with getJSON "https://api.instagram.com/oembed/?url=https://instagram.com/p/" (index .Params 0) "/&hidecaption=0" }}{{ .html | safeHTML }}{{ end }}{{ end }}`)
}
Expand Down

0 comments on commit 7040971

Please sign in to comment.