Prevent double-loading & add scope to File Highlight (#1586) Checks if a file has been loaded already and bails if it is and adds the ability to limit the scope of the file highlight plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
diff --git a/plugins/file-highlight/prism-file-highlight.js b/plugins/file-highlight/prism-file-highlight.js
index 6dee6c4..65efaf9 100644
--- a/plugins/file-highlight/prism-file-highlight.js
+++ b/plugins/file-highlight/prism-file-highlight.js
@@ -3,7 +3,11 @@
return;
}
- self.Prism.fileHighlight = function() {
+ /**
+ * @param {Element} [container=document]
+ */
+ self.Prism.fileHighlight = function(container) {
+ container = container || document;
var Extensions = {
'js': 'javascript',
@@ -17,7 +21,13 @@
'tex': 'latex'
};
- Array.prototype.slice.call(document.querySelectorAll('pre[data-src]')).forEach(function (pre) {
+ Array.prototype.slice.call(container.querySelectorAll('pre[data-src]')).forEach(function (pre) {
+ // ignore if already loaded
+ if (pre.hasAttribute('data-src-loaded')) {
+ return;
+ }
+
+ // load current
var src = pre.getAttribute('data-src');
var language, parent = pre;
@@ -55,6 +65,8 @@
code.textContent = xhr.responseText;
Prism.highlightElement(code);
+ // mark as loaded
+ pre.setAttribute('data-src-loaded', '');
}
else if (xhr.status >= 400) {
code.textContent = '✖ Error ' + xhr.status + ' while fetching file: ' + xhr.statusText;
@@ -85,6 +97,9 @@
};
- document.addEventListener('DOMContentLoaded', self.Prism.fileHighlight);
+ document.addEventListener('DOMContentLoaded', function () {
+ // execute inside handler, for dropping Event as argumnet
+ self.Prism.fileHighlight();
+ });
-})();
\ No newline at end of file
+})();
diff --git a/plugins/file-highlight/prism-file-highlight.min.js b/plugins/file-highlight/prism-file-highlight.min.js
index 301fa87..31869b3 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 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 a,n=e.getAttribute("data-src"),r=e,o=/\blang(?:uage)?-([\w-]+)\b/i;r&&!o.test(r.className);)r=r.parentNode;if(r&&(a=(e.className.match(o)||[,""])[1]),!a){var s=(n.match(/\.(\w+)$/)||[,""])[1];a=t[s]||s}var l=document.createElement("code");l.className="language-"+a,e.textContent="",l.textContent="Loading…",e.appendChild(l);var i=new XMLHttpRequest;i.open("GET",n,!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)}),Prism.plugins.toolbar&&Prism.plugins.toolbar.registerButton("download-file",function(t){var e=t.element.parentNode;if(e&&/pre/i.test(e.nodeName)&&e.hasAttribute("data-src")&&e.hasAttribute("data-download-link")){var a=e.getAttribute("data-src"),n=document.createElement("a");return n.textContent=e.getAttribute("data-download-link-label")||"Download",n.setAttribute("download",""),n.href=a,n}})},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(t){t=t||document;var e={js:"javascript",py:"python",rb:"ruby",ps1:"powershell",psm1:"powershell",sh:"bash",bat:"batch",h:"c",tex:"latex"};Array.prototype.slice.call(t.querySelectorAll("pre[data-src]")).forEach(function(t){if(!t.hasAttribute("data-src-loaded")){for(var a,n=t.getAttribute("data-src"),r=t,s=/\blang(?:uage)?-([\w-]+)\b/i;r&&!s.test(r.className);)r=r.parentNode;if(r&&(a=(t.className.match(s)||[,""])[1]),!a){var o=(n.match(/\.(\w+)$/)||[,""])[1];a=e[o]||o}var l=document.createElement("code");l.className="language-"+a,t.textContent="",l.textContent="Loading…",t.appendChild(l);var i=new XMLHttpRequest;i.open("GET",n,!0),i.onreadystatechange=function(){4==i.readyState&&(i.status<400&&i.responseText?(l.textContent=i.responseText,Prism.highlightElement(l),t.setAttribute("data-src-loaded","")):l.textContent=i.status>=400?"✖ Error "+i.status+" while fetching file: "+i.statusText:"✖ Error: File does not exist or is empty")},i.send(null)}}),Prism.plugins.toolbar&&Prism.plugins.toolbar.registerButton("download-file",function(t){var e=t.element.parentNode;if(e&&/pre/i.test(e.nodeName)&&e.hasAttribute("data-src")&&e.hasAttribute("data-download-link")){var a=e.getAttribute("data-src"),n=document.createElement("a");return n.textContent=e.getAttribute("data-download-link-label")||"Download",n.setAttribute("download",""),n.href=a,n}})},document.addEventListener("DOMContentLoaded",function(){self.Prism.fileHighlight()}))}();
\ No newline at end of file
diff --git a/prism.js b/prism.js
index 5116ebd..7f6dff2 100644
--- a/prism.js
+++ b/prism.js
@@ -807,7 +807,11 @@ Prism.languages.js = Prism.languages.javascript;
return;
}
- self.Prism.fileHighlight = function() {
+ /**
+ * @param {Element} [container=document]
+ */
+ self.Prism.fileHighlight = function(container) {
+ container = container || document;
var Extensions = {
'js': 'javascript',
@@ -821,7 +825,13 @@ Prism.languages.js = Prism.languages.javascript;
'tex': 'latex'
};
- Array.prototype.slice.call(document.querySelectorAll('pre[data-src]')).forEach(function (pre) {
+ Array.prototype.slice.call(container.querySelectorAll('pre[data-src]')).forEach(function (pre) {
+ // ignore if already loaded
+ if (pre.hasAttribute('data-src-loaded')) {
+ return;
+ }
+
+ // load current
var src = pre.getAttribute('data-src');
var language, parent = pre;
@@ -859,6 +869,8 @@ Prism.languages.js = Prism.languages.javascript;
code.textContent = xhr.responseText;
Prism.highlightElement(code);
+ // mark as loaded
+ pre.setAttribute('data-src-loaded', '');
}
else if (xhr.status >= 400) {
code.textContent = '✖ Error ' + xhr.status + ' while fetching file: ' + xhr.statusText;
@@ -889,6 +901,9 @@ Prism.languages.js = Prism.languages.javascript;
};
- document.addEventListener('DOMContentLoaded', self.Prism.fileHighlight);
+ document.addEventListener('DOMContentLoaded', function () {
+ // execute inside handler, for dropping Event as argumnet
+ self.Prism.fileHighlight();
+ });
})();
\ No newline at end of file