Skip to content

Instantly share code, notes, and snippets.

@HighnessAtharva
Created July 2, 2023 12:08
Show Gist options
  • Save HighnessAtharva/fb8f94fd557689a634203f4d34a58220 to your computer and use it in GitHub Desktop.
Save HighnessAtharva/fb8f94fd557689a634203f4d34a58220 to your computer and use it in GitHub Desktop.
Hugo Shortcodes
<!-- reset scratch variables at the start -->
{{ $.Scratch.Set "bl_author" false }}
{{ $.Scratch.Set "bl_source" false }}
{{ $.Scratch.Set "bl_link" false }}
{{ $.Scratch.Set "bl_title" false }}
{{ if .IsNamedParams }}
{{ $.Scratch.Set "bl_author" (.Get "author") }}
{{ $.Scratch.Set "bl_source" (.Get "source") }}
{{ $.Scratch.Set "bl_link" (.Get "link") }}
{{ $.Scratch.Set "bl_title" (.Get "title") }}
{{ else }}
<!-- for the positional version if any -->
{{ end }}
<!-- if title is not set explicitly then we need to beautify the link
if length of link is more than 32 chars, we will cut it off by 32 and
then drop everything after the last / if any and put it in into title -->
{{ with $.Scratch.Get "bl_title" }}
<!-- do nothing -->
{{ else }}
{{ with $.Scratch.Get "bl_link" }} <!-- if link is given -->
{{ range last 1 (split ($.Scratch.Get "bl_link" ) "://") }} <!-- split by :// and then only take the items after it to remove protocol:// -->
{{ $.Scratch.Set "title_without_protocol" . }}
{{ end }}
{{ range last 1 (split ($.Scratch.Get "title_without_protocol" ) "www.") }} <!-- also remove the www. at the start if any. we are using a second split because all URLS may not start with it -->
{{ $.Scratch.Set "title_without_protocol" . }}
{{ end }}
{{ $.Scratch.Set "bl_title" ($.Scratch.Get "title_without_protocol") }}
<!-- if link is longer than 32 bytes we should trim it -->
{{ if (gt (len ($.Scratch.Get "title_without_protocol") ) 32) }}
{{ $title := (slicestr ($.Scratch.Get "title_without_protocol") 0 32) }} <!-- get the first 32 characters of title_without_protocol -->
{{ $split_by_fw_slash := split $title "/" }} <!-- now split on / because we want to stop after the last forward slash -->
{{ $count := (sub (len $split_by_fw_slash) 1) }} <!-- we want everything but the last part so we adjust the count accordingly -->
{{ $.Scratch.Set "tempstring" "" }} <!-- temp variable to hold the concatinated string -->
{{ range first $count $split_by_fw_slash }} <!-- loop through all parts except last and concat them (add / between halves) -->
{{ $.Scratch.Set "tempstring" ( . | printf "%s%s/" ($.Scratch.Get "tempstring") | printf "%s" ) }}
{{ end }}
{{ $.Scratch.Set "bl_title" ( printf "%s..." ($.Scratch.Get "tempstring") | printf "%s" ) }}
{{ end }}
{{ end }}
{{ end }}
<blockquote>
<p><i>{{ .Inner | markdownify }}</i></p>
<footer>
<strong>{{ with $.Scratch.Get "bl_author" }}{{ . }}{{ end }}</strong>
{{ with $.Scratch.Get "bl_source" }}
<cite>{{ . }}</cite>
{{ else }}
{{ with $.Scratch.Get "bl_link" }}
<cite>
<a href="{{ . }}" title="{{ . }}" rel="noopener noreferrer">{{ $.Scratch.Get "bl_title" }}</a> <!-- can't have new lines here -->
</cite>
{{ else }}
{{ with $.Scratch.Get "bl_title" }}
<cite>
{{ $.Scratch.Get "bl_title" }}</a>
</cite>
{{ end }}
{{ end }}
{{ end }}
</footer>
</blockquote>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nanogallery2@3.0.5/dist/css/nanogallery2.min.css">
<script src="https://cdn.jsdelivr.net/npm/nanogallery2@3.0.5/dist/jquery.nanogallery2.min.js"></script>
</head>
<body>
<div data-nanogallery2='{
"thumbnailDisplayTransition": "none",
"thumbnailDisplayTransitionDuration": 500,
"thumbnailDisplayInterval": 30,
"galleryDisplayTransition": "none",
"galleryDisplayTransitionDuration": 500,
"galleryDisplayMode": "rows",
"thumbnailDisplayOutsideScreen": "false",
"eventsDebounceDelay": 10,
"thumbnailL1BorderHorizontal": 0,
"thumbnailL1BorderVertical": 0,
"thumbnailLabel": {
"titleFontSize": "0.6em"
},
"thumbnailHoverEffect2": "image_scale_1.00_1.10|label_backgroundColor_rgba(0,0,0,0)_rgba(255,255,255,0)",
"galleryTheme": {
"thumbnail": {
"borderRadius": "5px"
}
},
"thumbnailToolbarImage": {
"topLeft": "",
"topRight": "",
"bottomLeft": "",
"bottomRight": ""
},
"viewerToolbar": {
"display": true,
"standard": "label"
},
"viewerTools": {
"topLeft": "pageCounter, playPauseButton",
"topRight": "downloadButton, rotateLeft, zoomButton, fullscreenButton, closeButton"
},
"viewerGalleryTWidth": 40,
"viewerGalleryTHeight": 40
}'>
{{ .Inner }}
</div>
</body>
</html>
<mark>{{ with .Get 0 }}{{.}}{{end}}</mark>
<!--
Parameters:
type - (Required) album / track / playlist / artist
id - (Required) Target ID
width - (Optional) width
height - (Optional) height
-->
{{ if .IsNamedParams }}
<iframe src="https://open.spotify.com/embed/{{ .Get "type" }}/{{ .Get "id" }}"
width="{{ default "100%" (.Get "width") }}"
height="{{ default "380" (.Get "height") }}"
frameborder="0"
allowtransparency="true"
allow="encrypted-media"></iframe>
{{ else }}
<iframe src="https://open.spotify.com/embed/{{ .Get 0 }}/{{ .Get 1 }}"
width="{{ default "100%" (.Get 2) }}"
height="{{ default "380" (.Get 3) }}"
frameborder="0"
allowtransparency="true"
allow="encrypted-media"></iframe>
{{ end }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment