Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
60 lines (49 sloc) 2 KB

+++ categories = ["english", "blog", "programming"] date = "2016-03-28" publishdate = "2016-03-28" draft = false tags = ["hugo", "go", "blog", "shortcodes", "github"] title = "Adding a view source link"


At the bottom of each of the blog posts I now have a [See the source]({{< viewsrc >}}) link. With hugo (and quite likely all other blog-enignes, what do I know?) this is pretty straight forward:


For a site you can define global variables in config.toml and each Node, for example a post has both Frontmatter is available per "post". All this is available in the templates which displays the content:

{{ .Params.github_src }}/blob/master/content/{{ $.Path }}

and the concept of partials helps getting such a link included on all posts:

{{ with .Site.Params.github_src }}
<div style="float: right; " class="src share"> 
    <a href="{{ . }}/blob/master/content/{{ $.Path }}"
       class="src" title="See the source">
        See the source <span class="fa fa-code-fork"></span>
{{ end }

A small note

It's worth noting that for me to be able to recreate the link inside of the actual .md of this post without manually entering it I had to enclose it in a shortcode - in the name of reuse I thought I could use the same mechanism in both the template and this post. No such luck. To get it into the post, outside of the template system, I needed to use a shortcode. But shortcodes don't work inside of templates - and it turns out the qualified paths to the data wasn't exactly the same either:

Viewsrc.html shortcode:

{{ with $.Page.Site.Params.github_src }}{{ . }}/blob/master/content/{{ $.Page.Path }}
{{ end }}