Not converting foo?a/b to foo?a%2fb

Hi,
I’m making a short code for linking to java doc.
The with-frame java docs is a bit crazy, as it wants href to be ....?../.../ , and replacing / by %2f doesn’t work. ( e.g. https://docs.oracle.com/javase/7/docs/api/index.html?java/lang/System.html )

My shortcode is simply
<a href="https://docs.oracle.com/javase/7/docs/api/index.html?{{.Get 0}}"> {{.Inner}}</a>
And usage is
{{% ref "java/lang/System.html" %}} System {{% /ref %}}
This results in
<a href="https://docs.oracle.com/javase/7/docs/api/index.html?java%2flang%2fSystem.html"> System</a>

I tried various functions in shortcode, but nothing generates the ‘/’ .

Now, the question is, how to make hugo emit ‘/’ instead of ‘%2f’, after it has seen the ‘?’ of href ?
I’ld prefer to handle it in shortcode, rather than md,


Regards

Try the piping your shortcode output to the safeHTML function.

Unfortunately that still generates %2f for ‘/’

Go template security contexts can be tricky. Try generating the entire href attribute.

<a {{ printf "href=\"https://docs.oracle.com/javase/7/docs/api/index.html?%s\"" (.Get 0) | safeHTMLAttr }}>

PS: Edited since I posted the wrong version of my comment. Too may tabs open!

1 Like