How can I compute a “relative” link from a page to a resource? (rather than having links be site-relative)?
I have an odd use case: I am using Hugo to build “workbooks” that students download to their computers and run using a local web server. Sometimes, they don’t start the web server in the site root (for example, they might start it in the directory above - if they have a collection of workbooks).
For links, I can use relativeURLs - this works fine. I understand that this happens in a way specific to the links in the generated pages.
However: I have references to files that aren’t HTML links. For example, I pass parameters to scripts. If I just use “.RelPermalink”, I get a link relative to the domain route (which may not be the site root).
Is there a way to compute a relative link (using “..”) to a .RelPermalink within a template?
Here is an example (from a big messy page template)
There’s nothing built-in. You might be able to cobble together a partial template that handles it. That would be relatively simple if the site-relative link were in the same branch as the current page, but going sideways would be a hassle.
Although I think this is a terrible idea in 99.9% of sites, you might look at the relativeURLs setting.
We’d probably use Go’s filepath.Rel function. If using strings you could pass in whatever you want, but when passing page references we’d limit it to pages within the same site (role + version + language).