Title for download link

A complete noob here. How do I add a title to this link, so that instead of "Download files/sample.pdf" it says "Download a sample file?

For reference, I want to add a few links to my writing samples that a potential visitor can download.

1 Like

Is that a markdown question?

If so: Markdown Cheat Sheet | Markdown Guide

2 Likes

I think more of a shortcode question. What I’m trying to achieve is so that the file is downloaded instead of having it open in the browser window.

It might help to provide some more information. As it stands, I’d refer you to the documentation on HTML a elements. Which is probably not what you’re looking for here.

Also, if something is downloaded or not might depend on the browser settings.

structure

content/
└── post/
    └── test/
        β”œβ”€β”€ index.md
        └── sample.pdf

layouts/shortcodes/download.html

{{- with .Get 0 }}
  {{- $href := . }}
  {{- $content := . }}
  {{- with $.Inner }}
    {{- $content = strings.Trim . "\r\n" }}
  {{- end }}
  <a href="{{ $href }}" download>{{ $content }}</a>
{{- else }}
  {{- errorf "The %q shortcode requires a single positional parameter, a URL. See %s" .Name .Position }}
{{- end -}}

Example 1

content/post/test/index.md

{{< download sample.pdf />}}

produces:

<a href="sample.pdf" download>sample.pdf</a>

Example 2

content/post/test/index.md

{{< download sample.pdf >}}
Download this file
{{< /download >}}

produces:

<a href="sample.pdf" download>Download this file</a>

Reference:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-download

1 Like

Another suggestion would be to use the Page Resources Metadata to associate download resources with titles. See Page Resources | Hugo. You can find examples of using resources in Page Resources | Hugo.