1
1
var loaderUtils = require ( 'loader-utils' )
2
+ var hljs = require ( 'highlight.js' )
2
3
var cheerio = require ( 'cheerio' )
3
4
var markdown = require ( 'markdown-it' )
4
5
@@ -11,6 +12,21 @@ var replaceDelimiters = function (str) {
11
12
return str . replace ( / ( { { | } } ) / g, '<span>$1</span>' )
12
13
}
13
14
15
+ /**
16
+ * renderHighlight
17
+ * @param {string } str
18
+ * @param {string } lang
19
+ */
20
+ var renderHighlight = function ( str , lang ) {
21
+ if ( ! ( lang && hljs . getLanguage ( lang ) ) ) {
22
+ return ''
23
+ }
24
+
25
+ try {
26
+ return replaceDelimiters ( hljs . highlight ( lang , str , true ) . value )
27
+ } catch ( err ) { }
28
+ }
29
+
14
30
/**
15
31
* html => vue file template
16
32
* @param {[type] } html [description]
@@ -45,14 +61,15 @@ module.exports = function (source) {
45
61
var parser
46
62
var params = loaderUtils . parseQuery ( this . query ) || { }
47
63
var vueMarkdownOptions = Object . create ( this . options . __vueMarkdownOptions__ ? this . options . __vueMarkdownOptions__ . __proto__ : { } )
48
- var opts = Object . assign ( vueMarkdownOptions , params , this . options . __vueMarkdownOptions__ , this . options . vueMarkdown )
64
+ var opts = Object . assign ( vueMarkdownOptions , params , this . options . __vueMarkdownOptions__ , this . vueMarkdown , this . options . vueMarkdown )
49
65
50
66
if ( { } . toString . call ( opts . render ) === '[object Function]' ) {
51
67
parser = opts
52
68
} else {
53
69
opts = Object . assign ( {
54
70
preset : 'default' ,
55
71
html : true ,
72
+ highlight : renderHighlight
56
73
} , opts )
57
74
58
75
var plugins = opts . use
@@ -82,21 +99,14 @@ module.exports = function (source) {
82
99
source = preprocess . call ( this , parser , source )
83
100
}
84
101
85
- function exportContent ( content ) {
86
- if ( opts . raw ) {
87
- callback ( null , content )
88
- } else {
89
- callback ( null , 'module.exports = ' + JSON . stringify ( content ) )
90
- }
91
- }
92
-
93
102
source = source . replace ( / @ / g, '__at__' )
94
103
95
104
var content = parser . render ( source ) . replace ( / _ _ a t _ _ / g, '@' )
96
105
var result = renderVueTemplate ( content )
97
106
98
- // for relative includes
99
- opts . filename = this . resourcePath
100
-
101
- exportContent ( result )
107
+ if ( opts . raw ) {
108
+ return result
109
+ } else {
110
+ return 'module.exports = ' + JSON . stringify ( result )
111
+ }
102
112
}
0 commit comments