Skip to content

Commit

Permalink
File Highlight: Add option to provide a download button, when used wi…
Browse files Browse the repository at this point in the history
…th the Toolbar plugin. Fix #1030
  • Loading branch information
Golmote committed Mar 26, 2018
1 parent 8c0911a commit 9f22952
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
16 changes: 14 additions & 2 deletions plugins/file-highlight/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<base href="../.." />
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="themes/prism.css" data-noprefix />
<link rel="stylesheet" href="plugins/toolbar/prism-toolbar.css" data-noprefix />
<script src="prefixfree.min.js"></script>

<script>var _gaq = [['_setAccount', 'UA-33746269-1'], ['_trackPageview']];</script>
Expand All @@ -33,6 +34,13 @@ <h1>How to use</h1>
If, however, the language cannot be determined from the file extension or the file extension is incorrect, you may specify a language as well (with the usual class name way).</p>

<p>Please note that the files are fetched with XMLHttpRequest. This means that if the file is on a different origin, fetching it will fail, unless CORS is enabled on that website.</p>

<p>
When used in conjunction with the <a href="plugins/toolbar">Toolbar plugin</a>, this plugin can also display a button to download the file.
To use it, add a <code>data-download-link</code> attribute on the <code>&lt;pre></code> element.<br />
Optionally, the text can also be customized by using a <code>data-download-link-label</code> attribute.
</p>
<pre><code>&lt;pre data-src="myfile.js" data-download-link data-download-link-label="Download this file">&lt;/pre></code></pre>
</section>

<section>
Expand All @@ -43,17 +51,21 @@ <h1>Examples</h1>

<p>This page:</p>
<pre data-src="plugins/file-highlight/index.html"></pre>

<p>File that doesn’t exist:</p>
<pre data-src="foobar.js"></pre>


<p>With a download button:</p>
<pre data-src="plugins/file-highlight/prism-file-highlight.js" data-download-link data-download-link-label="Download this file"></pre>

<p>For more examples, browse around the Prism website. Most large code samples are actually files fetched with this plugin.</p>
</section>

<footer data-src="templates/footer.html" data-type="text/html"></footer>

<script src="prism.js"></script>
<!-- The File Highlight plugin is already included into Prism.js build -->
<script src="plugins/toolbar/prism-toolbar.js"></script>
<script src="utopia.js"></script>
<script src="components.js"></script>
<script src="code.js"></script>
Expand Down
10 changes: 10 additions & 0 deletions plugins/file-highlight/prism-file-highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@
}
};

if (pre.hasAttribute('data-download-link') && Prism.plugins.toolbar) {
Prism.plugins.toolbar.registerButton('download-file', function () {
var a = document.createElement('a');
a.textContent = pre.getAttribute('data-download-link-label') || 'Download';
a.setAttribute('download', '');
a.href = src;
return a;
});
}

xhr.send(null);
});

Expand Down
2 changes: 1 addition & 1 deletion plugins/file-highlight/prism-file-highlight.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,16 @@ Prism.languages.js = Prism.languages.javascript;
}
};

if (pre.hasAttribute('data-download-link') && Prism.plugins.toolbar) {
Prism.plugins.toolbar.registerButton('download-file', function () {
var a = document.createElement('a');
a.textContent = pre.getAttribute('data-download-link-label') || 'Download';
a.setAttribute('download', '');
a.href = src;
return a;
});
}

xhr.send(null);
});

Expand Down

0 comments on commit 9f22952

Please sign in to comment.