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:
Endilie Yacop Sucipto 2018-06-10 02:38:01 +08:00 committed by Yangshun Tay
parent 16da2fdbf6
commit c8bc00a3a7
10 changed files with 292 additions and 12 deletions

View file

@ -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) {}
}