Skip to content

Commit c8b6460

Browse files
committed
optimize code
1 parent 398789a commit c8b6460

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

lib/markdown-compiler.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var loaderUtils = require('loader-utils')
2+
var hljs = require('highlight.js')
23
var cheerio = require('cheerio')
34
var markdown = require('markdown-it')
45

@@ -11,6 +12,21 @@ var replaceDelimiters = function (str) {
1112
return str.replace(/({{|}})/g, '<span>$1</span>')
1213
}
1314

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+
1430
/**
1531
* html => vue file template
1632
* @param {[type]} html [description]
@@ -45,14 +61,15 @@ module.exports = function (source) {
4561
var parser
4662
var params = loaderUtils.parseQuery(this.query) || {}
4763
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)
4965

5066
if ({}.toString.call(opts.render) === '[object Function]') {
5167
parser = opts
5268
} else {
5369
opts = Object.assign({
5470
preset: 'default',
5571
html: true,
72+
highlight: renderHighlight
5673
}, opts)
5774

5875
var plugins = opts.use
@@ -82,21 +99,14 @@ module.exports = function (source) {
8299
source = preprocess.call(this, parser, source)
83100
}
84101

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-
93102
source = source.replace(/@/g, '__at__')
94103

95104
var content = parser.render(source).replace(/__at__/g, '@')
96105
var result = renderVueTemplate(content)
97106

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+
}
102112
}

0 commit comments

Comments
 (0)