grav icon indicating copy to clipboard operation
grav copied to clipboard

Markdown links: support for HTML download attribute

Open clivebeckett opened this issue 2 years ago • 4 comments

It would be great to have the HTML5 “download” attribute for links supported, similar to other attributes like id, target, classes (which I reckon are Grav Markdown features).

[Download a PDF](file.pdf?download=other-filename.pdf) would result in

<a download="other-filename.pdf" href="file.pdf">Download a PDF</a>

The value is optional.

See the W3C reference on the download attribute

I don’t know if spaces in the desired filename could be possible, maybe by using quotes or hex url character encoding (%20)

clivebeckett avatar Jan 23 '23 18:01 clivebeckett

Hi @clivebeckett You can actually do it in a very simple way. Add the download attribute in system.pages.markdown.valid_link_attributes to your system.yaml. By default, you will find here: rel, target, id, class, classes. From the administration panel it is very simple. The result of your example [Download a PDF](file.pdf?download=other-filename.pdf), would be the one you expose in this issue.

Try it and tell us.

pmoreno-rodriguez avatar Feb 15 '24 20:02 pmoreno-rodriguez

Hi @pmoreno-rodriguez

That’s brilliant, thank you! It works indeed with one tiny exception compared to the W3C specs: if I leave the attribute empty (?download instead of ?download=filename.pdf), Grav translates it to download="1" which in turn, at least in Firefox, results in a suggested filename of 1.pdf. However, that’s a miniscule issue :)

In the meantime I had helped myself with a CSS class force-download and a small JavaScript, returning the download attribute on every link with that class.

Best, Clive

clivebeckett avatar Feb 17 '24 17:02 clivebeckett

Well, now I can't think of a situation where I would want to use an empty attribute, but if there is one, and using it without any value, it shouldn't return a 1.

pmoreno-rodriguez avatar Feb 17 '24 18:02 pmoreno-rodriguez

Well, now I can't think of a situation where I would want to use an empty attribute, but if there is one, and using it without any value, it shouldn't return a 1.

Oh, well … an empty download attribute will use the actual filename of the document, hence it’s less HTML if you wish to just use that name and would be less Markdown to write, too :)

clivebeckett avatar Feb 18 '24 17:02 clivebeckett