Skip to content

Commit 1268168

Browse files
jputrinonginx-jack
andauthored
Create download shortcode (#50)
* Create download.html Adds a new shortcode that lets you provide a download link to a file * Update layouts/shortcodes/download.html --------- Co-authored-by: Jack Hickey <[email protected]>
1 parent 0b2888f commit 1268168

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

layouts/shortcodes/download.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{{/* This shortcode lets you create a link to download a file */}}
2+
{{/* It accepts 2 strings: the path to the file to download, relative to the Hugo build context (aka, content or static directory); the name to display for the target file */}}
3+
{{/* Usage: download <path/to/file.ext> <desired file name (optional)> */}}
4+
5+
<i class="fas fa-file-download fa-sm"></i>
6+
7+
{{/* Sets the file path provided in the shortcode as the href target, prepended by the Hugo BaseURL */}}
8+
9+
{{ $ref := .Get 0 | relURL }}
10+
11+
{{/* If it exists, get the second string provided by the user and run it through the markdown processor */}}
12+
13+
{{ if .Get 1 }}
14+
{{ $linktext := .Get 1 | markdownify }}
15+
<a href="{{$ref}}" download="{{$linktext}}">{{$linktext}}</a>
16+
17+
{{/* Otherwise, just get the path to the file to be downloaded. This will be run through the markdown processor
18+
and used as the filename. */}}
19+
20+
{{ else if .Get 0 }}
21+
{{ $linktext := .Get 0 | markdownify }}
22+
<a href="{{$ref}}" download>{{$linktext}}</a>
23+
24+
{{ else }}
25+
26+
{{ errorf "no file path value provided in download shortcode: %s" .Position }}
27+
28+
{{ end }}

0 commit comments

Comments
 (0)