How to schedule future internal links?

I’ve seen some discussions about get the current vs future date, but i’ve not found anything useful in my case.

Context

Sometimes i create multiple articles, linked by an href link, think about a guide or a walkthtrough.
Usually i want the articles to be published gradually over some weeks (i have a scheduled cronjob to do automate that).

The following “episodes” have a future published date, therefore they are published only after that date.

The issue

I create the links using the << ref >> shortcut as a url, setting the link to the next markdown file. My problem is that while i’m trying to build the hugo blog, there is usually a breaking error (REF_NOT_FOUND) which happens because the future post is still not “existing”.

ERROR 2021/03/19 15:21:32 [en] REF_NOT_FOUND: Ref "/path/2021-03-18-article-000264.md" from page "path/2021-03-18-article-000259.md": page not found

Expected Behaviour

Let’s say that

  • Page1 is published today (19 march)
  • Page 2 is scheduled to appear after 7 days (26 march)
  • There is a link from Page 1 to Page 2.

When Hugo build the site today, the link does not “appear” (or it’s empty, or pointing to self, it doesn’t matter), because the Page 2 does not exists today.

After the 26 of march, Hugo rebuild the site, this time the link is correctly written, because both pages exist.

Question

Is it possible to automate the expected behaviour or the only possible solution is for me is an “editorial” action, where i should change the article link only after the article has been published ?

Override the {{< ref >}} shortcode with your own.

layouts/shortcodes/ref.html

{{ with .Get 0 }}
  {{ if (site.GetPage .).PublishDate }}
    {{ ref $.Page . }}
  {{ end }}
{{ else }}
  {{ errorf "The %s shortcode requires a single positional parameter. See %s" .Name .Position }}
{{ end }}
4 Likes

This is genious, thanks!
Can i also check date instead of PublishDate?

So you want to hide the link if the creation date is greater than today’s date?

I’m not quite sure about the difference between “date” and “publishDate”, so i’m wondering if the script works in the same way if check the “date” property. (to avoid duplicating the properties, which would have the same value).

See https://gohugo.io/content-management/front-matter#predefined.

date

the datetime assigned to this page. This is usually fetched from the date field in front matter, but this behaviour is configurable.

publishDate

if in the future, content will not be rendered unless the --buildFuture flag is passed to hugo .

With a default archetype, the date in front matter will be set to the date the file was created.

The publishDate is the date that you wish the content to be visible.

1 Like

Thanks for the clarification!

Since we have already created a series of contents using “date” instead of publishDate to schedule future publications…

  1. May i use {{ if (site.GetPage .).Date }} instead of {{ if (site.GetPage .).PublishDate }} to have the same effect?

or

  1. I’ve done an heretical use of Date and should search and rename all those fields as PublishDate in my articles ?

Thanks for your time!

Yes, #2.

1 Like

Also, read this:
https://gohugo.io/getting-started/configuration/#configure-dates

1 Like

Opsie :sweat_smile:

Well, fortunately search and replace is easy on markdown.

Thanks for the support!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.