Update prism-line-highlight.js Making updates that allow this plugin to work with the line-numbers 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
diff --git a/plugins/line-highlight/prism-line-highlight.js b/plugins/line-highlight/prism-line-highlight.js
index 930e938..b8dddeb 100644
--- a/plugins/line-highlight/prism-line-highlight.js
+++ b/plugins/line-highlight/prism-line-highlight.js
@@ -8,6 +8,11 @@ function $$(expr, con) {
return Array.prototype.slice.call((con || document).querySelectorAll(expr));
}
+function hasClass(element, className) {
+ className = " " + className + " ";
+ return (" " + element.className + " ").replace(/[\n\t]/g, " ").indexOf(className) > -1
+}
+
var CRLF = crlf = /\r?\n|\r/g;
function highlightLines(pre, lines, classes) {
@@ -26,15 +31,25 @@ function highlightLines(pre, lines, classes) {
line.textContent = Array(end - start + 2).join(' \r\n');
line.className = (classes || '') + ' line-highlight';
- line.setAttribute('data-start', start);
-
- if(end > start) {
- line.setAttribute('data-end', end);
- }
-
+
+ //if the line-numbers plugin is enabled, then there is no reason for this plugin to display the line numbers
+ if(!hasClass(pre, 'line-numbers')) {
+ line.setAttribute('data-start', start);
+
+ if(end > start) {
+ line.setAttribute('data-end', end);
+ }
+ }
+
line.style.top = (start - offset - 1) * lineHeight + 'px';
-
- (pre.querySelector('code') || pre).appendChild(line);
+
+ //allow this to play nicely with the line-numbers plugin
+ if(hasClass(pre, 'line-numbers')) {
+ //need to attack to pre as when line-numbers is enabled, the code tag is relatively which screws up the positioning
+ pre.appendChild(line);
+ } else {
+ (pre.querySelector('code') || pre).appendChild(line);
+ }
}
}
@@ -91,4 +106,4 @@ Prism.hooks.add('after-highlight', function(env) {
addEventListener('hashchange', applyHash);
-})();
\ No newline at end of file
+})();