File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ ( function ( w ) {
2
+ 'use strict' ;
3
+
4
+ if ( typeof w . hljs == undefined ) {
5
+ console . error ( 'highlight.js not detected!' ) ;
6
+ } else {
7
+ w . hljs . initLineNumbersOnLoad = initLineNumbersOnLoad ;
8
+ w . hljs . lineNumbersBlock = lineNumbersBlock ;
9
+ }
10
+
11
+
12
+ function initLineNumbersOnLoad ( ) {
13
+ w . addEventListener ( 'load' , function ( ) {
14
+ try {
15
+ var blocks = document . getElementsByClassName ( 'hljs' ) ;
16
+
17
+ for ( var i in blocks ) {
18
+ if ( blocks . hasOwnProperty ( i ) ) {
19
+ lineNumbersBlock ( blocks [ i ] ) ;
20
+ }
21
+ }
22
+ } catch ( e ) {
23
+ console . error ( 'LineNumbers error: ' , e ) ;
24
+ }
25
+ } ) ;
26
+ }
27
+
28
+ function lineNumbersBlock ( element ) {
29
+ if ( typeof element !== 'object' ) return ;
30
+
31
+ var parent = element . parentNode ;
32
+ var lines = parent . outerText . match ( / \n / g) ;
33
+
34
+ lines = lines ? lines . length : 0 ;
35
+ if ( lines > 1 ) {
36
+ var l = '' ;
37
+ for ( var i = 0 ; i < lines ; i ++ ) {
38
+ l += ( i + 1 ) + '\n' ;
39
+ }
40
+
41
+ var linesPanel = document . createElement ( 'code' ) ;
42
+ linesPanel . className = 'hljs-line-numbers' ;
43
+ linesPanel . innerText = l ;
44
+
45
+ parent . insertBefore ( linesPanel , element ) ;
46
+ }
47
+ }
48
+ } ( window ) ) ;
You can’t perform that action at this time.
0 commit comments