Hugo Include file shortcode

Hugo Include file shortcode

If you include large snippets of text, like source code, in you content the markdown gets bloated quite a lot. This shortcode provides a solution by allowing to include resource files as plain text which can be used as text in markdown and wrapped by other shortcodes, e.g. highlighting source code.

This article was written for Hugo 0.74.

Usage

There are no options, just provide the name of the resource file. This is how all code snippets on this website are included in markdown.

Example, reading a file with sourcecode:

{{< read-file "readFile/read-file.html.txt" >}}
{{- (.Page.Resources.GetMatch (.Get 0)).Content | safeHTML -}}

Code

The implementation is trivial, using just Hugo’s build in functionality.

1
{{- (.Page.Resources.GetMatch (.Get 0)).Content | safeHTML -}}
Noticed an error in this post? Corrections are appreciated.

© Nelis Oostens