forked from facebook/docusaurus
/
renderMarkdown.js
131 lines (115 loc) · 3.82 KB
/
renderMarkdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const _ = require('lodash');
const hljs = require('highlight.js');
const {Remarkable: Markdown} = require('remarkable');
const {linkify} = require('remarkable/linkify');
const prismjs = require('prismjs');
const loadLanguages = require('prismjs/components/index');
const chalk = require('chalk');
const anchors = require('./anchors.js');
const CWD = process.cwd();
const alias = {
js: 'jsx',
html: 'markup',
sh: 'bash',
md: 'markdown',
};
class MarkdownRenderer {
constructor() {
const siteConfig = require(`${CWD}/siteConfig.js`);
let markdownOptions = {
// Highlight.js expects hljs css classes on the code element.
// This results in <pre><code class="hljs css languages-jsx">
langPrefix: 'hljs css language-',
highlight(str, lang) {
// User's own custom highlighting function
if (siteConfig.highlight && siteConfig.highlight.hljs) {
siteConfig.highlight.hljs(hljs);
}
// Fallback to default language
lang =
lang || (siteConfig.highlight && siteConfig.highlight.defaultLang);
if (lang === 'text') {
return str;
}
if (lang) {
try {
if (
siteConfig.usePrism === true ||
(siteConfig.usePrism &&
siteConfig.usePrism.length > 0 &&
siteConfig.usePrism.indexOf(lang) !== -1)
) {
const language = alias[lang] || lang;
try {
// Currently people using prismjs on Node have to individually require()
// every single language (https://github.com/PrismJS/prism/issues/593)
loadLanguages([language]);
return prismjs.highlight(
str,
prismjs.languages[language],
language,
);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
const unsupportedLanguageError = chalk.yellow(
`Warning: ${chalk.red(
language,
)} is not supported by prismjs.` +
'\nPlease refer to https://prismjs.com/#languages-list for the list of supported languages.',
);
console.log(unsupportedLanguageError);
} else console.error(err);
}
}
if (hljs.getLanguage(lang)) {
return hljs.highlight(lang, str).value;
}
} catch (err) {
console.error(err);
}
}
try {
return hljs.highlightAuto(str).value;
} catch (err) {
console.error(err);
}
return '';
},
html: true,
};
// Allow overriding default options
if (siteConfig.markdownOptions) {
markdownOptions = _.merge(
{},
markdownOptions,
siteConfig.markdownOptions,
);
}
const md = new Markdown(markdownOptions);
// Register anchors plugin
md.use(anchors, siteConfig.slugPreprocessor);
// Linkify
md.use(linkify);
// Allow client sites to register their own plugins
if (siteConfig.markdownPlugins) {
siteConfig.markdownPlugins.forEach((plugin) => {
md.use(plugin);
});
}
this.md = md;
}
toHtml(source) {
const html = this.md.render(source);
// Ensure fenced code blocks use Highlight.js hljs class
// https://github.com/jonschlinkert/remarkable/issues/224
return html.replace(/<pre><code>/g, '<pre><code class="hljs">');
}
}
const renderMarkdown = new MarkdownRenderer();
module.exports = (source) => renderMarkdown.toHtml(source);