How decode URLs in Hugo?

Hello,

How I can decode URLs in Hugo?

Thanks.

See Hugo Template Functions https://gohugo.io/categories/functions

Don’t know what you’re trying to do but have a look at replace replacere htmlUnescape safeHTML etc.

Actually I read them before posting here, thanks for your answer anyway.

I need some method/helper that do tha same job as decodeURI in JavaScript

If you use a taxonomy, and term’s value is an East Asia word, such as Chinese, the .URL will looks like %E6%B3%A2%E9%9F%B3.
HtmlUnescape can’t solved this problem.

And I also want to know how to do, if I want to get the original text from .URL?

Hi there, I just found a trick for that:

{{ $url := urls.Parse ($tag | urlize) }}
{{ $path := $url.Path }}
{{ with $.Site.GetPage (printf "/tags/%s" $path) }}
# See https://github.com/reuixiy/hugo-theme-meme/commit/aa106a71aff8bd7bbb2d4a6510cc72b466c2d79f

In this way, you can still get the page even if the name of the tag is SSG (Static Site Generator) or Raspberry Pi(树莓派).


Ref:

  1. https://gohugo.io/functions/urls.parse/
  2. https://godoc.org/net/url#URL
1 Like