Last active
March 12, 2018 00:09
-
-
Save tahmasebi/1aae118bc6c1e31276447271074dd78f to your computer and use it in GitHub Desktop.
Highlightjs line numbers: adding line numbers to highlightjs and style it for line color (no table)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ul.code-lines { | |
list-style : decimal; | |
padding : 0; | |
} | |
li.line { | |
display : block; | |
padding : 0; | |
} | |
ul.code-lines li:nth-child(odd) { | |
background-color : rgba(200, 200, 200, .1); | |
} | |
li.line:hover { | |
background-color : rgba(200, 200, 200, .4) !important; | |
} | |
li.line i { | |
width : 30px; | |
border-right : 1px solid #ddd; | |
display : inline-block; | |
font-size : 12px; | |
text-align : right; | |
padding : 0 5px 0 0; | |
color : #999; | |
user-select : none; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function highlightjs_line_numbers() { | |
var elements = jQuery( 'pre code' ).not( ':has(.code-lines)' ); | |
elements.each( function() { | |
var wholeText = jQuery( this ).html().trim(); | |
var splitText = wholeText.split( '\n' ); | |
if ( splitText.length > 1 ) { | |
var line_number = 0; | |
var newHtml = '<ul class="code-lines">'; | |
for ( var j = 0; j < splitText.length; j ++ ) { | |
line_number ++; | |
newHtml += '<li class=\'line\'><i>' + line_number + '</i> ' + splitText[j] + '</li>'; | |
} | |
newHtml += '</ul>'; | |
jQuery( this ).html( newHtml ); | |
} | |
} ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment