Created
May 31, 2020 09:24
-
-
Save brunoamaral/d12cbfc34db7b1f82b8974c15c0b66e1 to your computer and use it in GitHub Desktop.
Automatic Unsplash Copyright Shortcode for Hugo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
I use a lot of photos from unsplash as headers. This shortcode works in two ways to make that easier: | |
1. Standard use | |
{{< copyright name="Malte Wingen" href="https://unsplash.com/@maltewingen?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText" >}} | |
2. Automatic parsing of filename | |
{{< copyright >}} | |
First one allows you to customize the name and url, second one will infer that information from the default unsplash filename. | |
This assumes the following frontmatter data is present: | |
resources: | |
- src: images/markus-winkler--TRcaFMV5vk-unsplash.jpg | |
name: "header" | |
Hope it helps :) | |
--> | |
{{ $copyName := (.Get "name") }} | |
{{ $copyHref := (.Get "href") }} | |
{{- with $.Page.Resources.GetMatch "header" -}} | |
{{ if in .Permalink "-unsplash.jpg" }} | |
{{ $unsplash := replaceRE $.Page.Permalink "" .Permalink | replaceRE "images/" "" | replaceRE "-unsplash.jpg" ""}} | |
{{ $strings := split $unsplash "-" }} | |
{{$copyName = printf "%s %s" (index $strings 0) (index $strings 1)}} | |
{{$copyHref = replaceRE (printf "%s-%s-" (index $strings 0) (index $strings 1) ) "https://unsplash.com/photos/" $unsplash}} | |
{{ else }} | |
{{ end }} | |
{{end }} | |
<a style="background-color:black;color:white;text-decoration:none;padding:4px 6px;font-family:-apple-system, BlinkMacSystemFont, "San Francisco", "Helvetica Neue", Helvetica, Ubuntu, Roboto, Noto, "Segoe UI", Arial, sans-serif;font-size:12px;font-weight:bold;line-height:1.2;display:inline-block;border-radius:3px" href="{{ $copyHref }}" target="_blank" rel="noopener noreferrer" title="Photo by {{ $copyName }}"><span style="display:inline-block;padding:2px 3px"><svg xmlns="http://www.w3.org/2000/svg" style="height:12px;width:auto;position:relative;vertical-align:middle;top:-1px;fill:white" viewBox="0 0 32 32"><title>Unsplash Logo</title><path d="M20.8 18.1c0 2.7-2.2 4.8-4.8 4.8s-4.8-2.1-4.8-4.8c0-2.7 2.2-4.8 4.8-4.8 2.7.1 4.8 2.2 4.8 4.8zm11.2-7.4v14.9c0 2.3-1.9 4.3-4.3 4.3h-23.4c-2.4 0-4.3-1.9-4.3-4.3v-15c0-2.3 1.9-4.3 4.3-4.3h3.7l.8-2.3c.4-1.1 1.7-2 2.9-2h8.6c1.2 0 2.5.9 2.9 2l.8 2.4h3.7c2.4 0 4.3 1.9 4.3 4.3zm-8.6 7.5c0-4.1-3.3-7.5-7.5-7.5-4.1 0-7.5 3.4-7.5 7.5s3.3 7.5 7.5 7.5c4.2-.1 7.5-3.4 7.5-7.5z"></path></svg></span><span style="display:inline-block;padding:2px 3px">{{ $copyName }}</span></a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment