getgrav / grav

Modern, Crazy Fast, Ridiculously Easy and Amazingly Powerful Flat-File CMS powered by PHP, Markdown, Twig, and Symfony
https://getgrav.org
MIT License
14.5k stars 1.4k forks source link

Markdown links: support for HTML download attribute #3679

Open clivebeckett opened 1 year ago

clivebeckett commented 1 year ago

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)

pmoreno-rodriguez commented 7 months ago

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.

clivebeckett commented 7 months ago

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

pmoreno-rodriguez commented 7 months ago

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.

clivebeckett commented 7 months ago

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 :)