Skip to content

Commit cd22499

Browse files
committed
File Highlight: Fix issue causing the Download button to show up on every code blocks.
1 parent b94f01f commit cd22499

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

plugins/file-highlight/prism-file-highlight.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,24 @@
6565
}
6666
};
6767

68-
if (pre.hasAttribute('data-download-link') && Prism.plugins.toolbar) {
69-
Prism.plugins.toolbar.registerButton('download-file', function () {
70-
var a = document.createElement('a');
71-
a.textContent = pre.getAttribute('data-download-link-label') || 'Download';
72-
a.setAttribute('download', '');
73-
a.href = src;
74-
return a;
75-
});
76-
}
77-
7868
xhr.send(null);
7969
});
8070

71+
if (Prism.plugins.toolbar) {
72+
Prism.plugins.toolbar.registerButton('download-file', function (env) {
73+
var pre = env.element.parentNode;
74+
if (!pre || !/pre/i.test(pre.nodeName) || !pre.hasAttribute('data-src') || !pre.hasAttribute('data-download-link')) {
75+
return;
76+
}
77+
var src = pre.getAttribute('data-src');
78+
var a = document.createElement('a');
79+
a.textContent = pre.getAttribute('data-download-link-label') || 'Download';
80+
a.setAttribute('download', '');
81+
a.href = src;
82+
return a;
83+
});
84+
}
85+
8186
};
8287

8388
document.addEventListener('DOMContentLoaded', self.Prism.fileHighlight);

plugins/file-highlight/prism-file-highlight.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prism.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -849,19 +849,24 @@ Prism.languages.js = Prism.languages.javascript;
849849
}
850850
};
851851

852-
if (pre.hasAttribute('data-download-link') && Prism.plugins.toolbar) {
853-
Prism.plugins.toolbar.registerButton('download-file', function () {
854-
var a = document.createElement('a');
855-
a.textContent = pre.getAttribute('data-download-link-label') || 'Download';
856-
a.setAttribute('download', '');
857-
a.href = src;
858-
return a;
859-
});
860-
}
861-
862852
xhr.send(null);
863853
});
864854

855+
if (Prism.plugins.toolbar) {
856+
Prism.plugins.toolbar.registerButton('download-file', function (env) {
857+
var pre = env.element.parentNode;
858+
if (!pre || !/pre/i.test(pre.nodeName) || !pre.hasAttribute('data-src') || !pre.hasAttribute('data-download-link')) {
859+
return;
860+
}
861+
var src = pre.getAttribute('data-src');
862+
var a = document.createElement('a');
863+
a.textContent = pre.getAttribute('data-download-link-label') || 'Download';
864+
a.setAttribute('download', '');
865+
a.href = src;
866+
return a;
867+
});
868+
}
869+
865870
};
866871

867872
document.addEventListener('DOMContentLoaded', self.Prism.fileHighlight);

0 commit comments

Comments
 (0)