Making a download link for a binary tiddler

Once again, trying to formulate a question has given me my own answer. I was able to write a macro like this:

tags: $:/tags/Macro

\define download-link(title, text:"download", class:"")
<a 
    download=<<__title__>> 
    title=<<__title__>> 
    class={{{ [[binary]] [<__class__>] +[join[ ]] }}} 
    href={{{ [[data:]] [<__title__>get[type]] [[;base64,]] [<__title__>get[text]] +[join[]] }}}
><<__text__>></a>
\end

Sample usage:

<<download-link "Bylaws.docx" text:"MS Word" class:"blue">>

Yielding:

<a 
  download="Bylaws.docx" 
  title="Bylaws.docx" 
  class="binary blue" 
  href="data:application/vnd.openxmlformats->officedocument.wordprocessingml.document;base64,UEsD..."
>MS Word</a>

which displays as:

MS Word

Any suggested improvements would be welcome, especially of the hey-dummy-this-is-already-built-in variety!   :wink:

Original Question, which I almost sent before coming on this idea

Is there a built-in way to make a download link for a binary tiddler? I’m building a wiki for a local political group, and I would really like it to be a single-page wiki. Occasionally, for legal reasons, some of the content, which I’m extracting from existing PDFs, Word documents, and Excel spreadsheets, needs to be presentable in the original binary format. While I can simply link to the tiddler with the ugly yellow warning and download icon, I would rather have on the converted tiddler something like:

<a 
 download="Bylaws.docx"
 href="data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,UEsDBBQ..." 
 title="Bylaws.docx"
>download Bylaws.docx</a>

which would let you download the MS Word file.

In some sense this is the converse of the External Attachments Plugin, which allows you to externalize such documents. I want to specifically embed them in my (relatively small) wiki

I see the mechanism used for the existing BinaryEditWidget, but I don’t see a way I can use it directly. Obviously I could simply clone that and expose it, but I was wondering if I’m missing a simpler alternative.

2 Likes