-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
highlight.js
35 lines (32 loc) · 912 Bytes
/
highlight.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const chalk = require('chalk')
const prism = require('prismjs')
const loadLanguages = require('prismjs/components/index')
// required to make embedded highlighting work...
loadLanguages(['markup', 'css', 'javascript'])
function wrap (code, lang) {
return `<pre v-pre class="language-${lang}"><code>${code}</code></pre>`
}
module.exports = (str, lang) => {
if (!lang) {
return wrap(str, 'text')
}
const rawLang = lang
if (lang === 'vue' || lang === 'html') {
lang = 'markup'
}
if (lang === 'md') {
lang = 'markdown'
}
if (!prism.languages[lang]) {
try {
loadLanguages([lang])
} catch (e) {
console.log(chalk.yellow(`[vuepress] Syntax highlight for language "${lang}" is not supported.`))
}
}
if (prism.languages[lang]) {
const code = prism.highlight(str, prism.languages[lang], lang)
return wrap(code, rawLang)
}
return wrap(str, 'text')
}