Skip to content

Instantly share code, notes, and snippets.

@tahmasebi
Last active March 12, 2018 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tahmasebi/1aae118bc6c1e31276447271074dd78f to your computer and use it in GitHub Desktop.
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)
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;
}
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