mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 08:37:25 +02:00
Enable using Prism for syntax highlighting (#735)
* Enable user to use prism.js as syntax highlighter * add package-lock * if 'usePrism' is true, use prismjs on all languages * don't get lang by hljs if use prism * Update api-site-config.md * Update api-doc-markdown.md * only load prism css when usePrism is true
This commit is contained in:
parent
16da2fdbf6
commit
c8bc00a3a7
10 changed files with 292 additions and 12 deletions
|
@ -175,7 +175,12 @@ class Head extends React.Component {
|
|||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
{this.props.config.usePrism && (
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href={this.props.config.baseUrl + 'css/prism.css'}
|
||||
/>
|
||||
)}
|
||||
{/* Site defined code. Keep these at the end to avoid overriding. */}
|
||||
<link
|
||||
rel="stylesheet"
|
||||
|
|
|
@ -7,11 +7,16 @@
|
|||
|
||||
const hljs = require('highlight.js');
|
||||
const Markdown = require('remarkable');
|
||||
const prismjs = require('prismjs');
|
||||
|
||||
const anchors = require('./anchors.js');
|
||||
|
||||
const CWD = process.cwd();
|
||||
|
||||
const alias = {
|
||||
js: 'jsx',
|
||||
};
|
||||
|
||||
class MarkdownRenderer {
|
||||
constructor() {
|
||||
const siteConfig = require(CWD + '/siteConfig.js');
|
||||
|
@ -19,13 +24,29 @@ class MarkdownRenderer {
|
|||
const md = new Markdown({
|
||||
// Highlight.js expects hljs css classes on the code element.
|
||||
// This results in <pre><code class="hljs css javascript">
|
||||
langPrefix: 'hljs css ',
|
||||
langPrefix: 'hljs css languages- ',
|
||||
highlight: function(str, lang) {
|
||||
lang =
|
||||
lang || (siteConfig.highlight && siteConfig.highlight.defaultLang);
|
||||
if (lang && hljs.getLanguage(lang)) {
|
||||
if (lang) {
|
||||
try {
|
||||
return hljs.highlight(lang, str).value;
|
||||
if (
|
||||
siteConfig.usePrism === true ||
|
||||
(siteConfig.usePrism &&
|
||||
siteConfig.usePrism.length > 0 &&
|
||||
siteConfig.usePrism.indexOf(lang) !== -1)
|
||||
) {
|
||||
try {
|
||||
const language = alias[lang] || lang;
|
||||
// Currently people using prismjs on Node have to individually require()
|
||||
// every single language (https://github.com/PrismJS/prism/issues/593)
|
||||
require('prismjs/components/prism-' + language + '.min');
|
||||
return prismjs.highlight(str, prismjs.languages[language]);
|
||||
} catch (err) {}
|
||||
}
|
||||
if (hljs.getLanguage(lang)) {
|
||||
return hljs.highlight(lang, str).value;
|
||||
}
|
||||
} catch (err) {}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue