-
Notifications
You must be signed in to change notification settings - Fork 182
/
code.tsx
128 lines (109 loc) · 3.21 KB
/
code.tsx
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
import React, {PureComponent, Ref} from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import highlight from 'highlight.js/lib/core';
import normalizeIndent from '../global/normalize-indent';
import trivialTemplateTag from '../global/trivial-template-tag';
import memoize from '../global/memoize';
import styles from './code.css';
import highlightStyles from './highlight.css';
function noop() {}
const registerLanguage = memoize(async (language: string) => {
const languageExports = await import(
/* webpackChunkName: "highlight-[request]" */
`highlight.js/lib/languages/${language}`
);
highlight.registerLanguage(language, languageExports.default);
});
export interface CodeProps {
code: string
inline: boolean
softWrap: boolean
replacer: ((element: HTMLElement | null) => void)
className?: string | null | undefined
language?: string | null | undefined
codeRef?: Ref<HTMLElement> | null | undefined
}
/**
* @name Code
*/
export default class Code extends PureComponent<CodeProps> {
static propTypes = {
className: PropTypes.string,
code: PropTypes.string.isRequired,
inline: PropTypes.bool,
softWrap: PropTypes.bool,
language: PropTypes.string,
replacer: PropTypes.func,
codeRef: PropTypes.oneOfType([
PropTypes.shape({current: PropTypes.instanceOf(Element)}),
PropTypes.func
])
};
static defaultProps = {
inline: false,
softWrap: false,
replacer: noop
};
componentDidMount() {
this.highlight();
}
componentDidUpdate() {
this.highlight();
}
code?: HTMLElement | null;
async highlight() {
const codeRef = this.codeRef;
if (codeRef == null) {
return;
}
const {language, inline, replacer} = this.props;
if (!inline) {
if (language != null && highlight.getLanguage(language) == null) {
await registerLanguage(language);
}
// @ts-expect-error https://github.com/highlightjs/highlight.js/issues/2945
highlight.highlightElement(codeRef);
}
replacer(codeRef);
}
get codeRef() {
const {codeRef} = this.props;
return !codeRef || typeof codeRef === 'function' ? this.code : codeRef.current;
}
get initCodeRef() {
const {codeRef} = this.props;
const isFunctionCodeRef = typeof codeRef === 'function';
if (codeRef && !isFunctionCodeRef) {
return codeRef;
}
return (ref: HTMLElement | null) => {
this.code = ref;
if (isFunctionCodeRef) {
codeRef(this.code);
}
};
}
render() {
const {code, className, inline, softWrap, language} = this.props;
const Tag = inline ? 'span' : 'pre';
const classes = classNames(styles.code, className, language, {
[styles.inline]: inline,
[styles.softWrap]: softWrap
});
return (
<Tag className={classes} data-test="ring-code">
<code
// should be focusable because it can be scrollable
tabIndex={inline ? -1 : 0}
ref={this.initCodeRef}
className={highlightStyles.highlightContainer}
>
{normalizeIndent(code)}
</code>
</Tag>
);
}
}
const code = trivialTemplateTag((source: string) => <Code code={source}/>);
export {code, highlight};