Help understanding shortcode behaviour

I created a quickstart website following the instructions here, then created the following shortcode:

  • layouts/shortcodes/baseurl.html
{{ $base := .Site.BaseURL }}{{ $parsed := urls.Parse $base }}{{ $path := $parsed.Path }}{{ if not (eq $path "/") }}{{- $path -}}{{ end }}

then modified content/posts/my-first-post.md and added the following:

[example1]({{< baseurl >}}foo)

[example2]({{< baseurl >}}/foo)

the hugo.toml file is as per the example:

baseURL = 'https://example.org'
languageCode = 'en-us'
title = 'My New Hugo Site'
theme = 'ananke'

considering that a slash is always appended to .Site.BaseURL, $path in both shortcodes usages should be /.

Now looking at the hyperlinks in the page I see:

why are the two links different?

{{ (urls.Parse "https://example.org/").Path }} → /

Then in your code you say, if $path is not / then render $path, otherwise do nothing. So your shortcode renders nothing. That makes your markdown links look like this:

[example1](foo)
[example2](/foo)

The first one is relative to the currrent page (no leading slash).
The second one is relative to the site.

When I place those links in content/posts/post-1.md and then mouse-over them in the browser…

http://localhost:1313/posts/post-1/foo
http://localhost:1313/foo

So everything is working exactly as it should.

What are trying to do? There is almost never a good reason to use .Site.BaseURL anywhere in any template.

Thanks, I was missing this bit.

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