Since I started running my blog on Hugo, I feel that it has become easier to expand it myself. My two last articles were kinda connected. Some times I would like to create a link to a post that isn’t complete yet. But how irritating would it not be, if somebody followed the link and I wasn’t done. So.. Lets create a link for the future.
There are are a few ways to do links within Hugo. The basic one is:
[link](url "title")
That will look like this, nice but that gives us links pages that doesn’t exist. If we look at the documentation, they sugggest using ref.
[About]({{< ref "/page/about" >}} "About Me")
That makes it easier to to handle.
But what happens with pages that dont exist or nor yet published?
We get an REF_NOT_FOUND: Ref
error.
So then I can’t use that for the future either.
So I decided to write my own.
So I started by creating my own shortcode, by creating a file called blogpoint.html
in layouts/shortcodes
.
This is the shortcode blogpoint.
I filled the file with:
{{$page := .Site.GetPage (.Get "page") }}
{{$text := .Get "text"}}
{{ if (and ($page) (not ($page.Draft)) (lt $page.PublishDate now)) }}
<a href="{{$page.Permalink}}" title="{{$page.Title}}">{{$text}}</a>
{{ else }}
{{ $text }}
{{ end }}
You call it using two paramaters. This is the sample I used in my last blog post.
{{<blogpoint text="a migrated domain controller" page="/sids-and-rids-in-the-domain">}}
- It uses two paramaters, text and page.
- It fetches the page using
.Site.GetPage
- Verifies that the page exists, isn’t a draft and the publish date has passed.
- If that is true it will create a link with a title.
- Otherwise it will just print the text without any extra.
So now I can link to the future and the links shows up when the time has come.
I took some coding ideas from