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).
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.
+
+
+ When used in conjunction with the Toolbar plugin, this plugin can also display a button to download the file.
+ To use it, add a data-download-link attribute on the <pre> element.
+ Optionally, the text can also be customized by using a data-download-link-label attribute.
+
+
<pre data-src="myfile.js" data-download-link data-download-link-label="Download this file"></pre>
@@ -43,10 +51,13 @@
Examples
This page:
-
+
File that doesn’t exist:
-
+
+
With a download button:
+
+
For more examples, browse around the Prism website. Most large code samples are actually files fetched with this plugin.
@@ -54,6 +65,7 @@
Examples
+
diff --git a/plugins/file-highlight/prism-file-highlight.js b/plugins/file-highlight/prism-file-highlight.js
index dd113ebf6f..4c7f7d1acd 100644
--- a/plugins/file-highlight/prism-file-highlight.js
+++ b/plugins/file-highlight/prism-file-highlight.js
@@ -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);
});
diff --git a/plugins/file-highlight/prism-file-highlight.min.js b/plugins/file-highlight/prism-file-highlight.min.js
index acbaf12f05..1a30d2304b 100644
--- a/plugins/file-highlight/prism-file-highlight.min.js
+++ b/plugins/file-highlight/prism-file-highlight.min.js
@@ -1 +1 @@
-!function(){"undefined"!=typeof self&&self.Prism&&self.document&&document.querySelector&&(self.Prism.fileHighlight=function(){var e={js:"javascript",py:"python",rb:"ruby",ps1:"powershell",psm1:"powershell",sh:"bash",bat:"batch",h:"c",tex:"latex"};Array.prototype.slice.call(document.querySelectorAll("pre[data-src]")).forEach(function(t){for(var s,a=t.getAttribute("data-src"),n=t,r=/\blang(?:uage)?-(?!\*)(\w+)\b/i;n&&!r.test(n.className);)n=n.parentNode;if(n&&(s=(t.className.match(r)||[,""])[1]),!s){var o=(a.match(/\.(\w+)$/)||[,""])[1];s=e[o]||o}var l=document.createElement("code");l.className="language-"+s,t.textContent="",l.textContent="Loading…",t.appendChild(l);var i=new XMLHttpRequest;i.open("GET",a,!0),i.onreadystatechange=function(){4==i.readyState&&(i.status<400&&i.responseText?(l.textContent=i.responseText,Prism.highlightElement(l)):l.textContent=i.status>=400?"✖ Error "+i.status+" while fetching file: "+i.statusText:"✖ Error: File does not exist or is empty")},i.send(null)})},document.addEventListener("DOMContentLoaded",self.Prism.fileHighlight))}();
\ No newline at end of file
+!function(){"undefined"!=typeof self&&self.Prism&&self.document&&document.querySelector&&(self.Prism.fileHighlight=function(){var t={js:"javascript",py:"python",rb:"ruby",ps1:"powershell",psm1:"powershell",sh:"bash",bat:"batch",h:"c",tex:"latex"};Array.prototype.slice.call(document.querySelectorAll("pre[data-src]")).forEach(function(e){for(var n,a=e.getAttribute("data-src"),o=e,r=/\blang(?:uage)?-(?!\*)(\w+)\b/i;o&&!r.test(o.className);)o=o.parentNode;if(o&&(n=(e.className.match(r)||[,""])[1]),!n){var s=(a.match(/\.(\w+)$/)||[,""])[1];n=t[s]||s}var l=document.createElement("code");l.className="language-"+n,e.textContent="",l.textContent="Loading…",e.appendChild(l);var i=new XMLHttpRequest;i.open("GET",a,!0),i.onreadystatechange=function(){4==i.readyState&&(i.status<400&&i.responseText?(l.textContent=i.responseText,Prism.highlightElement(l)):l.textContent=i.status>=400?"✖ Error "+i.status+" while fetching file: "+i.statusText:"✖ Error: File does not exist or is empty")},e.hasAttribute("data-download-link")&&Prism.plugins.toolbar&&Prism.plugins.toolbar.registerButton("download-file",function(){var t=document.createElement("a");return t.textContent=e.getAttribute("data-download-link-label")||"Download",t.setAttribute("download",""),t.href=a,t}),i.send(null)})},document.addEventListener("DOMContentLoaded",self.Prism.fileHighlight))}();
\ No newline at end of file
diff --git a/prism.js b/prism.js
index b09ccb0099..1f8717d797 100644
--- a/prism.js
+++ b/prism.js
@@ -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);
});