mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-27 16:06:59 +02:00
feat: hljs syntax highlighting & nits
This commit is contained in:
parent
0058b20d9e
commit
ea37e54477
8 changed files with 44 additions and 52 deletions
22
lib/core/components/Markdown/highlight.js
Normal file
22
lib/core/components/Markdown/highlight.js
Normal 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;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue