How to properly use shortcodes for URLs?

Sometime there’s a need to link to your own site, for instance, in one article you want to point to some other article. I’ve read about shortcodes, which can be added to layouts/shortcodes/ , and the shortcodes can substitute, for instance, the base url if you use the following code in baseurl.html file:

{{ .Page.Site.BaseURL }}

and then in the blog post:

{{< baseurl >}}/post/whatever/

This solution indeed works, but I noticed the following in the site code:

<a href="http://localhost:1313/
/post/whatever/">...</a> 

So as you can see there’s a new line between the base URL and the rest of URL. This of course doesn’t break the site, but validator.w3.org has issues with it and reports:

**Error** : Bad value https://site.tld↩/post/whatever/for attributehrefon element [a ](https://html.spec.whatwg.org/multipage/#the-a-element): Tab, new line or carriage return found.

Is there a way to fix it?

You don’t need a shortcode to link to your own or probably anywhere. Just use the markdown format"

[link text](relative-url-to-domain)

So, yourwebsite.com/abc can be linked as: [abc](/abc)

2 Likes

Yes, I know, but if I changed the website domain, I would also have to change the content of each post. When I use the shortcode, I don’t have to. Also this solution works for localhost. So it’s really nice, but the new line should be eliminated.

No, with relative URLs, you won’t have to change anything. That’s why they’re called relative.

Ok, now I get it, so simply /post/whatever/ without anything in front of it. Yes, that’s even better. :slight_smile:

1 Like

There is also some neat built in shortcodes for creating permalinks to internals pages.

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