feat: hljs syntax highlighting & nits

This commit is contained in:
endiliey 2018-08-26 17:06:16 +08:00
parent 0058b20d9e
commit ea37e54477
8 changed files with 44 additions and 52 deletions

View file

@ -0,0 +1,22 @@
const hljs = require('highlight.js');
const chalk = require('chalk');
const escapeHtml = require('escape-html');
export default (str, rawLang) => {
if (rawLang === 'text') {
return escapeHtml(str);
}
const lang = rawLang.toLowerCase();
try {
if (hljs.getLanguage(lang)) {
return hljs.highlight(lang, str).value;
}
} catch (e) {
console.error(
chalk.yellow(
`Highlight.js syntax highlighting for language "${lang}" is not supported.`
)
);
}
return hljs.highlightAuto(str).value;
};