Make link downloadable file instead of displaying it

I want hugo to generate the link with “download” like here:

<a href="/documents/my_doc.txt" download>Click here to download</a>

currently it generates this:

<a href="/documents/my_doc.txt" >Click here to download</a>

I want the browser to download the link instead of displaying it

Please edit your examples. What you are looking for is unclear.

I want to add “download” to a link in hugo. I unfortunately don’t know how to make that clearer with more examples :frowning:

Your 1st example includes the download attribute twice. Your 2nd example includes the download attribute once. Please edit your examples.

that is the text of the link :confused:

oh ok
<a href="/documents/my_doc.txt" download>Click here to download</a>
currently it generates this:

<a href="/documents/my_doc.txt">Click here to download</a>

How are you adding the download attribute in your project’s templates?

Go templates come with strict security. Simply adding an HTML attribute will not work because it is considered unsafe.

Have a look at the safeHTMLAttr function.

If the above does not fix the issue, then you will need to share a sample repository replicating your project’s structure for people in this forum to see what is going on.

1 Like

Option 1 - Use a Shortcode

layouts/shortcodes/a.html

<a
  {{- range $k, $v := .Params -}}
    {{- (printf " %s=%q" $k $v) | safeHTMLAttr -}}
  {{- end -}}
  >
  {{- .Inner | .Page.RenderString -}}
</a>

Markdown:

{{< a href="/documents/my_doc.txt" download="download" >}}
Click here to download
{{< /a >}}

Option 2 - Use HTML within Markdown

config.toml

[markup.goldmark.renderer]
unsafe = true

Markdown:

<a href="/documents/my_doc.txt" download>Click here to download</a>
1 Like