Skip to content

Commit

Permalink
Create download.html
Browse files Browse the repository at this point in the history
Adds a new shortcode that lets you provide a download link to a file
  • Loading branch information
jputrino authored Feb 28, 2024
1 parent 0b2888f commit 2254d55
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions layouts/shortcodes/download.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{/* This shortcode lets you create a link to download a file */}}
{{/* 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 */}}
{{/* Usage: download <path/to/file.ext> <desired file name (optional)> */}}

<i class="fas fa-file-download fa-sm"></i>

{{/* Sets the file path provided in the shortcode as the href target, prepended by the Hugo BaseURL */}}

{{ $ref := .Get 0 | relURL }}

{{/* If it exists, get the second string provided by the user and run it through the markdown processor */}}

{{ if .Get 1 }}
{{ $linktext := .Get 1 | markdownify }}
<a href="{{$ref}}" download="$linktext">{{$linktext}}</a>

{{/* Otherwise, just get the path to the file to be downloaded. This will be run through the markdown processor
and used as the filename. */}}

{{ else if .Get 0 }}
{{ $linktext := .Get 0 | markdownify }}
<a href="{{$ref}}" download>{{$linktext}}</a>

{{ else }}

{{ errorf "no file path value provided in download shortcode: %s" .Position }}

{{ end }}

0 comments on commit 2254d55

Please sign in to comment.