1
- var fs = require ( "fs" ) ,
2
- hljs = require ( "highlight.js" ) ,
3
- cheerio = require ( "cheerio" ) ,
4
- he = require ( "he" ) ,
5
- grunt = require ( "grunt" ) ,
6
- lineNumberTemplate = fs . readFileSync ( __dirname + "/lineNumberTemplate.jst" , "utf-8" ) ;
1
+ "use strict" ;
2
+
3
+ const fs = require ( "fs" ) ;
4
+ const hljs = require ( "highlight.js" ) ;
5
+ const cheerio = require ( "cheerio" ) ;
6
+ const he = require ( "he" ) ;
7
+ const grunt = require ( "grunt" ) ;
8
+ const lineNumberTemplate = fs . readFileSync ( __dirname + "/lineNumberTemplate.jst" , "utf-8" ) ;
7
9
8
10
// When parsing the class attribute, make sure a class matches an actually
9
11
// highlightable language, instead of being presentational (e.g. 'example')
10
12
function getLanguageFromClass ( str ) {
11
- var classes = ( str || "" ) . split ( " " ) ,
13
+ var classes = ( str || "" ) . split ( " " ) ,
12
14
i = 0 ,
13
15
length = classes . length ;
14
16
15
17
for ( ; i < length ; i ++ ) {
16
- if ( hljs . LANGUAGES [ classes [ i ] . replace ( / ^ l a n g - / , "" ) ] ) {
17
- return classes [ i ] . replace ( / ^ l a n g - / , "" ) ;
18
+ if ( hljs . getLanguage ( classes [ i ] . replace ( / ^ l a n g - / , "" ) ) ) {
19
+ return classes [ i ] . replace ( / ^ l a n g - / , "" ) ;
18
20
}
19
21
}
20
22
@@ -27,34 +29,34 @@ function outdent( string ) {
27
29
minTabs = Infinity ,
28
30
rLeadingTabs = / ^ \t + / ;
29
31
30
- string . split ( "\n" ) . forEach ( function ( line , i , arr ) {
32
+ string . split ( "\n" ) . forEach ( function ( line , i , arr ) {
31
33
32
34
// Don't include first or last line if it's nothing but whitespace
33
- if ( ( i === 0 || i === arr . length - 1 ) && ! line . trim ( ) . length ) {
35
+ if ( ( i === 0 || i === arr . length - 1 ) && ! line . trim ( ) . length ) {
34
36
return ;
35
37
}
36
38
37
39
// For empty lines inside the snippet, push a space so the line renders properly
38
40
if ( ! line . trim ( ) . length ) {
39
- adjustedLines . push ( " " ) ;
41
+ adjustedLines . push ( " " ) ;
40
42
return ;
41
43
}
42
44
43
45
// Count how many leading tabs there are and update the global minimum
44
46
var match = line . match ( rLeadingTabs ) ,
45
- tabs = match ? match [ 0 ] . length : 0 ;
47
+ tabs = match ? match [ 0 ] . length : 0 ;
46
48
minTabs = Math . min ( minTabs , tabs ) ;
47
49
48
50
adjustedLines . push ( line ) ;
49
- } ) ;
51
+ } ) ;
50
52
51
53
if ( minTabs !== Infinity ) {
52
54
53
55
// Outdent the lines as much as possible
54
56
rOutdent = new RegExp ( "^\t{" + minTabs + "}" ) ;
55
- adjustedLines = adjustedLines . map ( function ( line ) {
57
+ adjustedLines = adjustedLines . map ( function ( line ) {
56
58
return line . replace ( rOutdent , "" ) ;
57
- } ) ;
59
+ } ) ;
58
60
}
59
61
60
62
return adjustedLines . join ( "\n" ) ;
@@ -63,25 +65,25 @@ function outdent( string ) {
63
65
function syntaxHighlight ( html ) {
64
66
var $ = cheerio . load ( html ) ;
65
67
66
- $ ( "pre > code" ) . each ( function ( ) {
68
+ $ ( "pre > code" ) . each ( function ( ) {
67
69
var $t = $ ( this ) ,
68
70
code = he . decode ( outdent ( $t . html ( ) ) ) ,
69
71
lang = $t . attr ( "data-lang" ) ||
70
72
getLanguageFromClass ( $t . attr ( "class" ) ) ||
71
- ( code . trim ( ) . charAt ( 0 ) === "<" ? "xml" : "" ) ||
73
+ ( code . trim ( ) . charAt ( 0 ) === "<" ? "xml" : "" ) ||
72
74
"javascript" ,
73
75
linenumAttr = $t . attr ( "data-linenum" ) ,
74
76
linenum = parseInt ( linenumAttr , 10 ) || 1 ,
75
- gutter = linenumAttr === "false" ? false : true ,
76
- highlighted = hljs . highlight ( lang , code ) ,
77
- fixed = hljs . fixMarkup ( highlighted . value , " " ) ;
77
+ gutter = linenumAttr !== "false" ,
78
+ highlighted = hljs . highlight ( code , { language : lang } ) ,
79
+ fixed = highlighted . value . replace ( / \t / g , " " ) ;
78
80
79
81
// Handle multi-line comments (#32)
80
82
fixed = fixed . replace (
81
- / < s p a n c l a s s = " c o m m e n t " > \/ \* ( [ ^ < ] + ) \* \/ < \/ s p a n > / g,
82
- function ( full , comment ) {
83
- return "<span class=\"comment\">/*" +
84
- comment . split ( "\n" ) . join ( "</span>\n<span class=\"comment\">" ) +
83
+ / < s p a n c l a s s = " h l j s - c o m m e n t " > \/ \* ( [ ^ < ] + ) \* \/ < \/ s p a n > / g,
84
+ function ( _full , comment ) {
85
+ return "<span class=\"hljs- comment\">/*" +
86
+ comment . split ( "\n" ) . join ( "</span>\n<span class=\"hljs- comment\">" ) +
85
87
"*/</span>" ;
86
88
}
87
89
) ;
@@ -93,8 +95,8 @@ function syntaxHighlight( html ) {
93
95
gutter : gutter ,
94
96
lang : lang
95
97
}
96
- } ) ) ;
97
- } ) ;
98
+ } ) ) ;
99
+ } ) ;
98
100
99
101
return $ . html ( ) ;
100
102
}
0 commit comments