-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
prism.js
61 lines (53 loc) · 1.49 KB
/
prism.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
const prism = require('prismjs');
require('prismjs/components/prism-css');
require('prismjs/components/prism-bash');
require('prismjs/components/prism-diff');
require('prismjs/components/prism-javascript');
require('prismjs/components/prism-json');
require('prismjs/components/prism-jsx');
require('prismjs/components/prism-markup');
require('prismjs/components/prism-yaml');
require('prismjs/components/prism-tsx');
function highlight(code, language) {
let prismLanguage;
switch (language) {
case 'ts':
prismLanguage = prism.languages.tsx;
break;
case 'js':
prismLanguage = prism.languages.jsx;
break;
case 'sh':
throw new Error(
[
`docs-infra: Unsupported language: "sh" in:`,
'',
'```sh',
code,
'```',
'',
'Use "bash" instead.',
'',
].join('\n'),
);
case 'diff':
prismLanguage = { ...prism.languages.diff };
// original `/^[-<].*$/m` matches lines starting with `<` which matches
// <SomeComponent />
// we will only use `-` as the deleted marker
prismLanguage.deleted = /^[-].*$/m;
break;
default:
prismLanguage = prism.languages[language];
break;
}
if (!prismLanguage) {
if (language) {
throw new Error(`unsupported language: "${language}", "${code}"`);
} else {
prismLanguage = prism.languages.jsx;
}
}
return prism.highlight(code, prismLanguage);
}
module.exports = highlight;