15th June 2024 - Hugo Links To Static Files ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I have finally got my website sorted for flux playlists. Yes, my gopher is a great place but for ease, I thought I could get a website going. It is https://stug.uk. I have a vps running FreeBSD which costs me Â10 a year. Throw in Â12 for the domain for 2 years and we have a cheap box to muck around with. I am inherently lazy and did not want to muck around too much with site generation. So I decided to use Hugo to build a static site. One of the blockers I found with Hugo was that it does not like you linking to text files or PDFs. It seems to be a key failing. After some search fu, I found a particularly helpful chap, Justin Pearson. I am recording this in gopher so I don't lose it and hopefully it can help others. It worked a treat and over multiple builds. The other oddity I found was that Hugo builds only for https and so I was struggling to get links to work until I set up SSL via certbot. A pain in the arse! Taken from https://user.it.uu.se/~justin/Hugo/post/linking_to_the_static_dir/ Hugo: Linking to the static directory Dec 13, 2022 I don’t know why this is so difficult. Hugo provides a static directory where you can put stuff that is directly copied to your web directory. In theory you can have links [A link]("/file.pdf") and if you the file static/file.pdf in your Hugo project base directory then all is well. If your homepage has its root at some other location than the root, say example.com/Hugo rather than just example.com, then this does not always work. I really don’t understand why. After googling I found this solution that creates a special short code. You use [A link]({{< static "/file.pdf" >}}) blah You need to create the file layouts/shortcodes/static.md with the following content: {{- .Scratch.Set "path" (.Get 0) -}} {{- if hasPrefix (.Scratch.Get "path") "/" -}} {{- .Scratch.Set "path" (slicestr (.Scratch.Get "path") 1) -}} {{- end -}} {{- .Scratch.Get "path" | absLangURL -}} It turns out that this rather long winded approach has its advantages. If you put all your papers in static/papers/ then you can create a shortcode layouts/shortcodes/paper.md : {{- .Scratch.Set "basedir" "papers/" -}} {{- .Scratch.Set "path" (.Get 0) -}} {{- if hasPrefix (.Scratch.Get "path") "/" -}} {{- .Scratch.Set "path" (slicestr (.Scratch.Get "path") 1) -}} {{- end -}} {{- .Scratch.Add "basedir" (.Scratch.Get "path") -}} {{- .Scratch.Get "basedir" | absLangURL -}} You can the create links to your papers as : [This paper]({{< paper "2017_ICTAI.pdf" >}}) Assuming that 2017_ICTAI.pdf is in your papers directory. Incidental, if you are having trouble showing unexpended short codes in code blocks, then this thread will help. [Link goes to https://discourse.gohugo.io/t/how-is-the-hugo-doc-site-showing- shortcodes-in-code-blocks/9074]