Automatically link to repo at current commit
I like writing blog posts, particularly about code, and I like to link to code on my repo from my blog post. I do this a lot. Until now, I’ve just been copying and pasting the full link to github. However, I ran into a problem today.
I moved a file that was referenced in a blog post. I then had to go to that blog post and update the links - this is fine if I remember the linked blog posts - but that’s not scalable.
Also:
- Finding the link on GitHub, then copying and pasting is annoying
- It worries me a little that the version they are linked to could be vastly different from what I mention on the post.
I was already trying to create permalinks using tags, but that is laborious and error prone.
What if I could get Hugo to:
- Automatically link to GitHub if the relative link is not within
blog/content
- What if I could get it to link it to the file at the last commit of the post.
The last one is something to bear in mind. If I update a blog post, I’ll have to ensure that the links are still relevant.
Alternatively, let’s allow an override at the page level where you can provide a specific commit to link to:
Step 1: Enable git info
To be able to get the commit of the post, we need to enable git info
|
|
Step 2: Update rendering of link
layout/_default/_markup/render-link.html
|
|
Bonus: Allow per-post commit override
In the above code:
|
|
The commit id is picked up from the page parameter link_commit
, or if that
doesn’t exist, from the last commit of the page.
You can therefore, set the commit to use for a post with:
|
|
Conclusion
Necessity might be the mother of invention, but sometimes it takes a fine-tuned sense of frustration to detect minor needs.