1
1
var Metalsmith = require ( 'metalsmith' ) ,
2
- metallic = require ( 'metalsmith-metallic' ) ,
3
2
drafts = require ( 'metalsmith-drafts' ) ,
4
3
layouts = require ( 'metalsmith-layouts' ) ,
5
4
markdown = require ( 'metalsmith-markdown' ) ,
@@ -10,6 +9,40 @@ var Metalsmith = require('metalsmith'),
10
9
var serve = require ( 'metalsmith-serve' ) ;
11
10
var watch = require ( 'metalsmith-watch' ) ;
12
11
var sass = require ( 'metalsmith-sass' ) ;
12
+ const hljs = require ( 'highlight.js' ) ;
13
+ const marked = require ( 'marked' ) ;
14
+
15
+ var renderer = new marked . Renderer ( ) ;
16
+
17
+ renderer . code = function ( code , infostring , escaped ) {
18
+ /** Copied from the default implementation, with the following changes.
19
+ * 1. Find a series of 4 spaces and convert it into tabs
20
+ * 2. Add "hljs" to the code class. I don't get why, but metalsmith-metallic was doing this, so I copied the pattern.
21
+ * 3. Remove this.options.langPrefix, because metalsmith-metallic didn't have this.
22
+ */
23
+ code = code . replace ( / { 4 } / g, '\t' ) ; // Convert spaces back into tabs
24
+ var lang = ( infostring || '' ) . match ( / \S * / ) [ 0 ] ;
25
+ if ( this . options . highlight ) {
26
+ var out = this . options . highlight ( code , lang ) ;
27
+ if ( out != null && out !== code ) {
28
+ escaped = true ;
29
+ code = out ;
30
+ }
31
+ }
32
+
33
+ if ( ! lang ) {
34
+ return '<pre><code>' + ( escaped ? code : escape ( code , true ) ) + '</code></pre>' ;
35
+ }
36
+
37
+ return (
38
+ '<pre><code class="hljs ' +
39
+ // this.options.langPrefix +
40
+ escape ( lang , true ) +
41
+ '">' +
42
+ ( escaped ? code : escape ( code , true ) ) +
43
+ '</code></pre>\n'
44
+ ) ;
45
+ } ;
13
46
14
47
build ( function ( ) {
15
48
console . log ( 'Done building.' ) ;
@@ -29,14 +62,16 @@ function build(callback) {
29
62
// Use the drafts plugin
30
63
. use ( drafts ( ) )
31
64
32
- // Use metallic plugin to add syntax highlighting
33
- . use ( metallic ( ) )
34
-
35
65
// Use Github Flavored Markdown for content
36
66
. use (
37
67
markdown ( {
38
68
gfm : true ,
39
69
tables : true ,
70
+ highlight : function ( code , lang ) {
71
+ // TODO what if there is no lang? highlightAuto..
72
+ return hljs . highlight ( lang , code ) . value ;
73
+ } ,
74
+ renderer,
40
75
} ) ,
41
76
)
42
77
0 commit comments