How do I encode a URL in frontmatter?


#1

I have the following front matter in a page:

+++
authorurl=“http://foo.bar
+++

In my partial I have:

“url”:"{{ .Params.authorurl }}"

I get in the output:

“url”:“http://foo.bar

(Some lines above are omitted for clarity)

What do I need to do to get “url”:“http://foo.bar” in the output?


[SOLVED] Writing Base64 Image in Params
#2

Try

"url":"{{ .Params.authorurl | safeURL }}"

#3

Thank you . I tried that and I still get the same output.

Sorry for the newbie question…


#4

It’s hardly a newbie question when I cannot give you a correct answer …


#5

Try

"url": {{ printf "%s" $.Params.authorurl }}

See, it keeps coming up :stuck_out_tongue: I’m definitely adding it to the Themes > Variables section like now.

Let us know whether it works.


#6

What’s the context of this code? Are you trying to output into a javascript block? If so, try using this safeJS.

"url":"{{ .Params.authorurl | safeJS }}"

#7

I have posted a working example of the issue as a public GitHub repository. Clone this and run hugo and observe the output.

https://github.com/resourcefulcomputing/hugobug


#8

Oh, the joys of Go templates. This is one area where Go templates are often confusing or surprising. Thanks for the sample project. If you missed it, @rindolphus had the solution for you.

The issue revolves around Go template contexts. Go often doesn’t like inserting content into a value. It wants to create the entire value. In your case, Go wants to insert the entire JS string including the double-quotes, not just the stuff inside the quotes. Two ways to make this work:

"page":{{ printf "%s" .Permalink }}
"page":{{ printf "%q" .Permalink | safeJS }}

#9

Thank you for the reply and suggestion.

Maybe I am missing something. My web site is still generating the escaped URL.

I have changed my code to match your suggestion, did a rm -fr public and then hugo. I have pushed the results back up to my github repository so you can see the public/index.html file.

Is it important to note that I am using the templating inside JSON+LD?

In case you don’t want to review the Git repo:

<html>
<head>
</head>
<body>
<script type="application/ld+json">
{
  "@context":"http://schema.org",
  "@type":"BlogPosting",
  "@id":"http://resourcefulcomputing.com/post/rust/",
  "page":"{{ printf "%s" .Permalink  }}",
  "page":"{{ printf "%s" .Permalink | safeJS }}"
}
</script>

<h1>Bug Example?</h1>

</body>
</html>

Generates:

<html>
<head>
</head>
<body>
<script type="application/ld+json">
{
  "@context":"http://schema.org",
  "@type":"BlogPosting",
  "@id":"http://resourcefulcomputing.com/post/rust/",
  "page":"http:\/\/replace-this-with-your-hugo-site.com\/",
  "page":"http:\/\/replace-this-with-your-hugo-site.com\/"
}
</script>

<h1>Bug Example?</h1>

</body>
</html>

I have used URL processing in other parts of web sites without any issues. The only difference I can tell is that this is a HTML script element block containing JSON+LD.

Randy


#10

Fix your base URL in your config.toml to something with forward slashes:

---
baseurl: "http://www.yourkickasshugowebsite.com/"
---

#11

I tested your github repo before my last reply, and it worked for me. What version of Go and Hugo are you using?


#12

I ran into the same issue. @MooreReason has the right solution.

In your template you’ll want to remove the surrounding quotes from the schema.org value implementation.

So instead of:
"page":"{{ printf "%s" .Permalink }}",

You’ll want
"page":{{ printf "%s" .Permalink }},

Or you can just test from your original example,
"page":{{ .Params.authorurl }},


#13

Yeah, you are right. Just, that last one won’t work, cause the double quotes will be missing. From the original example:

"page":{{ printf "%s" .Params.authorurl }},