88 w . hljs . lineNumbersBlock = lineNumbersBlock ;
99 }
1010
11- function initLineNumbersOnLoad ( ) {
11+ var defaultOptions = {
12+ withLinks : false
13+ } ;
14+
15+ function initLineNumbersOnLoad ( options ) {
16+ options = options || defaultOptions ;
17+
1218 w . addEventListener ( 'load' , function ( ) {
1319 try {
1420 var blocks = document . querySelectorAll ( 'code.hljs' ) ;
1521
1622 for ( var i in blocks ) {
1723 if ( blocks . hasOwnProperty ( i ) ) {
18- lineNumbersBlock ( blocks [ i ] ) ;
24+ lineNumbersBlock ( blocks [ i ] , {
25+ blockName : 'c' + i ,
26+ withLinks : options . withLinks
27+ } ) ;
1928 }
2029 }
2130 } catch ( e ) {
2433 } ) ;
2534 }
2635
27- function lineNumbersBlock ( element ) {
36+ function lineNumbersBlock ( element , options ) {
2837 if ( typeof element !== 'object' ) return ;
38+ if ( ! ! options ) {
39+ options . withLinks = options . withLinks || false ;
40+ options . blockName = options . blockName || false ;
41+ } else {
42+ options = defaultOptions ;
43+ options . blockName = '' ;
44+ }
2945
3046 var parent = element . parentNode ;
3147 var lines = getCountLines ( parent . textContent ) ;
3248
3349 if ( lines > 1 ) {
3450 var l = '' ;
3551 for ( var i = 0 ; i < lines ; i ++ ) {
36- l += ( i + 1 ) + '\n' ;
52+ l += options . withLinks
53+ ? getLineWithLink ( i + 1 , options . blockName )
54+ : ( i + 1 ) + '\n' ;
3755 }
3856
3957 var linesPanel = document . createElement ( 'code' ) ;
4058 linesPanel . className = 'hljs hljs-line-numbers' ;
4159 linesPanel . style . float = 'left' ;
42- linesPanel . textContent = l ;
60+ linesPanel . innerHTML = l ;
4361
4462 parent . insertBefore ( linesPanel , element ) ;
4563 }
4664 }
4765
48- function getCountLines ( text ) {
66+ function getCountLines ( text ) {
4967 if ( text . length === 0 ) return 0 ;
5068
5169 var regExp = / \r \n | \r | \n / g;
5876
5977 return lines ;
6078 }
79+
80+ function getLineWithLink ( i , blockName ) {
81+ var id = blockName + '_l' + i ;
82+ return '<a href="#' + id + '" id="' + id + '">' + i + '</span>\n'
83+ }
6184} ( window ) ) ;
0 commit comments