In my content files I have images, and I want to make it so when you click the image it opens the source file.
I can do this successfully with [![](img-path)](img-path)
However, writing the img-path
twice every time I place an image seems like wasted energy.
Is there a universal way to set all of my images to become links to their own source paths?
EDIT: SOLUTION FOR ANYONE WITH A SIMILAR QUESTION:
Per MunifTanjim’s suggestion, I implemented a shortcode for this.
In a file called linked-img.html
, place the following:
{{ $path := .Get 0 }}
<a href={{ $path }}>
<img src={{ $path }} />
</a>
You can add extra attributes as you please.
Depending on your setup, you may need to change the first line to include your site’s baseURL. In that case swap it with:
{{ $path := printf "%s%s" .Site.BaseURL (.Get 0) }}
Again, as needed you can tweak the string there as needed until Hugo is generating the paths the way you want with your setup.
Then when you want to place an image in your .md
file just type:
{{< linked-img "/path/to/image.png" >}}
And keep in mind that if you screw up any of your shortcodes in your .md
file, all of the other shortcodes will fail too.