mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-03 20:27:20 +02:00
22 lines
554 B
JavaScript
22 lines
554 B
JavaScript
const hljs = require('highlight.js');
|
|
const chalk = require('chalk');
|
|
const escapeHtml = require('escape-html');
|
|
|
|
export default (str, rawLang) => {
|
|
if (rawLang === 'text' || !rawLang) {
|
|
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;
|
|
};
|