Skip to content

Commit

Permalink
Prevent double-loading & add scope to File Highlight (#1586)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
OlehDutchenko authored and mAAdhaTTah committed Nov 28, 2018
1 parent a2230c3 commit 10239c1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
23 changes: 19 additions & 4 deletions plugins/file-highlight/prism-file-highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});

})();
})();
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.

21 changes: 18 additions & 3 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});

})();

0 comments on commit 10239c1

Please sign in to comment.