Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document urlquery function #1627

Closed
schnerring opened this issue Jan 16, 2022 · 4 comments · Fixed by #1633
Closed

Document urlquery function #1627

schnerring opened this issue Jan 16, 2022 · 4 comments · Fixed by #1633
Assignees

Comments

@schnerring
Copy link
Contributor

schnerring commented Jan 16, 2022

Consider the following example:

{{ $formatString := "https://www.facebook.com/sharer.php?u={url}" }}
{{ $result := replace $formatString "{url}" .Permalink }}

The issue I've had is that I want to "percent-encode" .Permalink. People have had this issue before:

I've poked around the Hugo code base and found an undocumented built-in Hugo function urlquery:

https://github.com/gohugoio/hugo/blob/3d5dbdcb1a11b059fc2f93ed6fadb9009bf72673/tpl/internal/go_templates/texttemplate/funcs.go#L51

https://github.com/gohugoio/hugo/blob/3d5dbdcb1a11b059fc2f93ed6fadb9009bf72673/tpl/internal/go_templates/texttemplate/funcs.go#L738-L742

It's a thin wrapper around url.QueryEscape and perfectly solves my issue:

{{ urlquery .Permalink }}

Is the function here to stay and just misses documentation?

@jmooring
Copy link
Member

@schnerring Thanks for this; much appreciated. Note that your example can be addressed with the querify function.

{{ printf "%s?%s" "https://www.facebook.com/sharer.php" (querify "u" .Permalink) }}

@schnerring
Copy link
Contributor Author

schnerring commented Jan 16, 2022

I understand that, but my actual requirement is a bit more complex.

Here is the partial I use as "social provider":

{{ $url := .Permalink }}
{{ $title := .Title }}
{{ with $.Site.Params.socialShare }}
  <div class="social-share">
    <strong>Share this post: </strong>
    {{ range . }}
      {{ $href := .formatString }}
      {{ $href := replace $href "{url}" (urlquery $url) }}
      {{ $href := replace $href "{title}" (urlquery $title) }}
      <a href="{{ $href | safeURL }}" target="_blank" rel="noreferrer">
        {{ if eq .icon "Mail" }}
          {{ partial "icons/tabler-icon.html" .icon }}
        {{ else }}
          {{ partial "icons/simple-icon.html" .icon }}
        {{ end }}
      </a>
    {{ end }}
  </div>
{{ end }}

This allows me to add social share buttons via Hugo config like this

[[params.socialShare]]
    icon = "Facebook"
    formatString = "https://www.facebook.com/sharer.php?u={url}"
  [[params.socialShare]]
    icon = "Reddit"
    formatString = "https://reddit.com/submit?url={url}&title={title}"
  [[params.socialShare]]
    icon = "Twitter"
    formatString = "https://twitter.com/intent/tweet?url={url}&text={title}"
  [[params.socialShare]]
    icon = "LinkedIn"
    formatString = "https://www.linkedin.com/sharing/share-offsite/?url={url}"
  [[params.socialShare]]
    icon = "WhatsApp"
    formatString = "whatsapp://send/?text={title}%20{url}"
  [[params.socialShare]]
    icon = "Tumblr"
    formatString = "https://www.tumblr.com/widgets/share/tool?canonicalUrl={url}&title={title}&caption={title}"

As the query string parameter structure differs depending on the network, querify doesn't really work for me.

I tried doing something like (querify "" .Permalink) but this always prepends a =.

@schnerring
Copy link
Contributor Author

schnerring commented Jan 17, 2022

So from what I understand urlquery is just a thin wrapper around url.QueryEscape.

Looking at the go template docs I also found the urlquery function in the section of predefined global functions:

urlquery
	Returns the escaped value of the textual representation of
	its arguments in a form suitable for embedding in a URL query.
	This function is unavailable in html/template, with a few
	exceptions.

So is it really an undocumented Hugo feature or just a go templating feature?

@jmooring
Copy link
Member

Both.

schnerring added a commit to schnerring/hugoDocs that referenced this issue Jan 18, 2022
coliff pushed a commit to schnerring/hugoDocs that referenced this issue Oct 20, 2022
jmooring added a commit that referenced this issue Nov 10, 2022
* Add urlquery function docs

Fixes #1627

* Update content/en/functions/urlquery.md

* Update content/en/functions/urlquery.md

* Update content/en/functions/urlquery.md

* Update content/en/functions/urlquery.md

* Update content/en/functions/urlquery.md

* Update content/en/functions/urlquery.md

* Update content/en/functions/urlquery.md

* Update content/en/functions/urlquery.md

Co-authored-by: Joe Mooring <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants