-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (59 loc) · 1.8 KB
/
index.js
File metadata and controls
68 lines (59 loc) · 1.8 KB
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
import React from 'react';
import codeEditor from './codeEditor/index.tsx';
import codemirror, { VariablesContext as cmVariableContext } from './codeMirror';
import canonical from './utils/canonical.ts';
import { modes } from './utils/modes.ts';
import uppercase from './utils/uppercase.ts';
const SyntaxHighlighter = (
code,
lang,
opts = {
// If `customTheme` is present, it will override the `dark` option.
className: '',
customTheme: false,
dark: false,
editable: false,
inline: false,
tokenizeVariables: false,
},
props = {},
) => {
const { mdx, ...editorProps } = props;
let theme = opts.dark ? 'material-palenight' : 'neo';
if (opts.customTheme) {
theme = opts.customTheme;
}
if (opts.foldGutter) {
// `foldGutter` does not work with runmode and has to be done with the codeEditor component
// NOTE: To disable editing, to still allow folding, `readOnly` must be `true`.
// oxlint-disable-next-line no-param-reassign
opts.editable = true;
// Default CSS classes
// oxlint-disable-next-line no-param-reassign
opts.gutters = ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'];
}
if (opts.editable) {
return React.createElement(codeEditor, {
...editorProps,
code,
lang,
theme,
options: opts,
});
}
const classes = [`cm-s-${theme}`];
if (opts.className) {
classes.push(opts.className);
}
if (opts.highlightMode) {
classes.push('CodeEditor');
}
const wrapper = opts.inline ? 'span' : 'div';
return React.createElement(
wrapper,
{ className: classes.join(' '), 'data-testid': 'SyntaxHighlighter' },
codemirror(typeof code === 'string' ? code : '', lang, opts, { mdx }),
);
};
export default SyntaxHighlighter;
export { uppercase, canonical, modes, cmVariableContext };