Hugo Attachment shortcode
Shortcode which allows users to download page resource files, the files can be any extension or size. The user can download the files or open them in a new tab.
.html
files cannot be part of page resources since Hugo will pick them up as an actual HTML page. In such case i add them as.html.txt
files.
This article was written for Hugo 0.74
.
For the tooltips the tooltips CSS library is used.
Usage
option | effect | optional | default |
---|---|---|---|
title | sets the title of the attachments | yes | Attachments |
caption | sets additional information for the attachments | yes | |
folder | specifies the folder to use | yes | files |
pattern | a regular expressions, used to filter the files in the folder. The pattern parameter value must be regular expressions
|
Attachments must be located in the page’s resources, i would strongly advice to use a leaf bundle for you content and add dedicated folders for downloadable resources. This also keeps your posts well organized.
If your page is a markdown file, attachments must be placed in a folder named like your page and ending with .files (or specified folder name).
- content
- _index.md
- page.files
- attachment.pdf
- page.md
- content
If your page is a folder (leaf bundle), attachments must be placed in a nested ‘files’ folder (or specified folder name).
- content
- _index.md
- page
- index.md
- files
- attachment.pdf
- content
Be aware that if you use a multilingual website, you will need to have as many folders as languages.
Example
{{< attachments folder="attachments" caption="example" >}}
{{< attachments folder="attachments" pattern="(attachments.scss)" caption="single file example" >}}
Code
The shortcode will look for the specified folder and add all files to a list with their permalink so Hugo knows the files need to be included on the site. In addition it also shows the size of the file.
|
|
The accompanying SCSS
|
|
- Permalink: //oostens.me/posts/hugo-attachment-shortcode/
- License: The text and content is licensed under CC BY-NC-SA 4.0. All source code I wrote on this page is licensed under The Unlicense; do as you please, I'm not liable nor provide warranty.