Skip to content

Instantly share code, notes, and snippets.

@danixland
Created November 13, 2020 13:42
Show Gist options
  • Save danixland/b569d5ead547d4627e9d649fc657548a to your computer and use it in GitHub Desktop.
Save danixland/b569d5ead547d4627e9d649fc657548a to your computer and use it in GitHub Desktop.
custom shortcode template for hugo to display a gravatar image inside a <figure> container. email address can be passed inside the shortcode or can be set inside the [params] group as "gravatarEmail"
{{/*
* The gravatar shortcode:
* All arguments are optional, main ones are mail and size and have a fallback set in place.
* Args:
* mail: [string] The email address. Falls back to .Site.Params.gravatarEmail which should be set in your config file.
* size: [int] The size of the fetched image. Defaults to 200 if not set.
* class: [string] The class to give to the figure block.
* link: [string] The address to link the picture to.
* target: [string] Where to open the link. One of "_blank", "_self", "_parent", "_top".
* caption: [string] Caption text to show with the image. Supports Markdown.
*
* Usage:
* {{< gravatar email="some@address.com" size=150 class="some class" link="https://example.com" target="_blank" caption="Here's a picture of a dog." >}}
*
* Output:
* <figure class="some class">
* <a href="https://example.com" target="_blank">
* <img src="https://www.gravatar.com/avatar/emailhash?s=150" alt="Here's a picture of a dog." />
* </a>
* <<figcaption>
* <p>
* Here's a picture of a dog.
* </p>
* </figcaption>
* </figure>
*
*/}}
{{- if .Get "mail" -}}
{{- $mailaddr := .Get "mail" -}}
{{- .Scratch.Set "mailhash" $mailaddr -}}
{{ else }}
{{- .Scratch.Set "mailhash" $.Site.Params.gravatarEmail -}}
{{ end }}
{{- $hash := .Scratch.Get "mailhash" | lower | md5 -}}
<figure{{ with .Get "class" }} class="{{ . }}"{{ end }}>
{{- if .Get "link" -}}
<a href="{{ .Get "link" }}"{{ with .Get "target" }} target="{{ . }}"{{ end }}{{ with .Get "rel" }} rel="{{ . }}"{{ end }}>
{{- end }}
<img src="https://www.gravatar.com/avatar/{{- $hash -}}?s={{- with .Get "size" }}{{.}}{{ else }}200{{ end }}"
{{- if or (.Get "alt") (.Get "caption") }}
alt="{{ with .Get "alt" }}{{ . }}{{ else }}{{ .Get "caption" | markdownify| plainify }}{{ end }}"
{{- end -}}
/>
{{- if .Get "link" -}}
</a>
{{- end }}
{{- if .Get "caption" -}}
<figcaption>
<p>
{{- .Get "caption" | markdownify -}}
</p>
</figcaption>
{{- end }}
</figure>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment