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" ) ;
79
810// When parsing the class attribute, make sure a class matches an actually
911// highlightable language, instead of being presentational (e.g. 'example')
1012function getLanguageFromClass ( str ) {
11- var classes = ( str || "" ) . split ( " " ) ,
13+ var classes = ( str || "" ) . split ( " " ) ,
1214 i = 0 ,
1315 length = classes . length ;
1416
1517 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 - / , "" ) ;
1820 }
1921 }
2022
@@ -27,34 +29,34 @@ function outdent( string ) {
2729 minTabs = Infinity ,
2830 rLeadingTabs = / ^ \t + / ;
2931
30- string . split ( "\n" ) . forEach ( function ( line , i , arr ) {
32+ string . split ( "\n" ) . forEach ( function ( line , i , arr ) {
3133
3234 // 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 ) {
3436 return ;
3537 }
3638
3739 // For empty lines inside the snippet, push a space so the line renders properly
3840 if ( ! line . trim ( ) . length ) {
39- adjustedLines . push ( " " ) ;
41+ adjustedLines . push ( " " ) ;
4042 return ;
4143 }
4244
4345 // Count how many leading tabs there are and update the global minimum
4446 var match = line . match ( rLeadingTabs ) ,
45- tabs = match ? match [ 0 ] . length : 0 ;
47+ tabs = match ? match [ 0 ] . length : 0 ;
4648 minTabs = Math . min ( minTabs , tabs ) ;
4749
4850 adjustedLines . push ( line ) ;
49- } ) ;
51+ } ) ;
5052
5153 if ( minTabs !== Infinity ) {
5254
5355 // Outdent the lines as much as possible
5456 rOutdent = new RegExp ( "^\t{" + minTabs + "}" ) ;
55- adjustedLines = adjustedLines . map ( function ( line ) {
57+ adjustedLines = adjustedLines . map ( function ( line ) {
5658 return line . replace ( rOutdent , "" ) ;
57- } ) ;
59+ } ) ;
5860 }
5961
6062 return adjustedLines . join ( "\n" ) ;
@@ -63,25 +65,25 @@ function outdent( string ) {
6365function syntaxHighlight ( html ) {
6466 var $ = cheerio . load ( html ) ;
6567
66- $ ( "pre > code" ) . each ( function ( ) {
68+ $ ( "pre > code" ) . each ( function ( ) {
6769 var $t = $ ( this ) ,
6870 code = he . decode ( outdent ( $t . html ( ) ) ) ,
6971 lang = $t . attr ( "data-lang" ) ||
7072 getLanguageFromClass ( $t . attr ( "class" ) ) ||
71- ( code . trim ( ) . charAt ( 0 ) === "<" ? "xml" : "" ) ||
73+ ( code . trim ( ) . charAt ( 0 ) === "<" ? "xml" : "" ) ||
7274 "javascript" ,
7375 linenumAttr = $t . attr ( "data-linenum" ) ,
7476 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 , " " ) ;
7880
7981 // Handle multi-line comments (#32)
8082 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\">" ) +
8587 "*/</span>" ;
8688 }
8789 ) ;
@@ -93,8 +95,8 @@ function syntaxHighlight( html ) {
9395 gutter : gutter ,
9496 lang : lang
9597 }
96- } ) ) ;
97- } ) ;
98+ } ) ) ;
99+ } ) ;
98100
99101 return $ . html ( ) ;
100102}
0 commit comments