-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathHighlight.js
executable file
·172 lines (145 loc) · 3.97 KB
/
Highlight.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// @flow
import React, { Component, type Node } from "react";
import normalizeTokens from "../utils/normalizeTokens";
import themeToDict, { type ThemeDict } from "../utils/themeToDict";
import type {
Language,
Token,
LineInputProps,
LineOutputProps,
TokenInputProps,
TokenOutputProps,
RenderProps,
PrismGrammar,
PrismLib,
PrismTheme,
PrismToken,
} from "../types";
type Props = {
Prism: PrismLib,
theme?: PrismTheme,
language: Language,
code: string,
children: (props: RenderProps) => Node,
};
class Highlight extends Component<Props, *> {
prevTheme: PrismTheme | void;
prevLanguage: Language | void;
themeDict: ThemeDict | void;
getThemeDict = (props: Props): ThemeDict | void => {
if (
this.themeDict !== undefined &&
props.theme === this.prevTheme &&
props.language === this.prevLanguage
) {
return this.themeDict;
}
this.prevTheme = props.theme;
this.prevLanguage = props.language;
const themeDict = props.theme
? themeToDict(props.theme, props.language)
: undefined;
return (this.themeDict = themeDict);
};
getLineProps = ({
key,
className,
style,
line,
...rest
}: LineInputProps): LineOutputProps => {
const output: LineOutputProps = {
...rest,
className: "token-line",
style: undefined,
key: undefined,
};
const themeDict = this.getThemeDict(this.props);
if (themeDict !== undefined) {
output.style = themeDict.plain;
}
if (style !== undefined) {
output.style =
output.style !== undefined ? { ...output.style, ...style } : style;
}
if (key !== undefined) output.key = key;
if (className) output.className += ` ${className}`;
return output;
};
getStyleForToken = ({ types, empty }: Token) => {
const typesSize = types.length;
const themeDict = this.getThemeDict(this.props);
if (themeDict === undefined) {
return undefined;
} else if (typesSize === 1 && types[0] === "plain") {
return empty ? { display: "inline-block" } : undefined;
} else if (typesSize === 1 && !empty) {
return themeDict[types[0]];
}
const baseStyle = empty ? { display: "inline-block" } : {};
// $FlowFixMe
const typeStyles = types.map((type) => themeDict[type]);
return Object.assign(baseStyle, ...typeStyles);
};
getTokenProps = ({
key,
className,
style,
token,
...rest
}: TokenInputProps): TokenOutputProps => {
const output: TokenOutputProps = {
...rest,
className: `token ${token.types.join(" ")}`,
children: token.content,
style: this.getStyleForToken(token),
key: undefined,
};
if (style !== undefined) {
output.style =
output.style !== undefined ? { ...output.style, ...style } : style;
}
if (key !== undefined) output.key = key;
if (className) output.className += ` ${className}`;
return output;
};
tokenize = (
Prism: PrismLib,
code: string,
grammar: PrismGrammar,
language: Language
): Array<PrismToken | string> => {
const env = {
code,
grammar,
language,
tokens: [],
};
Prism.hooks.run("before-tokenize", env);
const tokens = (env.tokens = Prism.tokenize(
env.code,
env.grammar,
env.language
));
Prism.hooks.run("after-tokenize", env);
return tokens;
};
render(): Node {
const { Prism, language, code, children } = this.props;
const themeDict = this.getThemeDict(this.props);
const grammar = Prism.languages[language];
const mixedTokens =
grammar !== undefined
? this.tokenize(Prism, code, grammar, language)
: [code];
const tokens = normalizeTokens(mixedTokens);
return children({
tokens,
className: `prism-code language-${language}`,
style: themeDict !== undefined ? themeDict.root : {},
getLineProps: this.getLineProps,
getTokenProps: this.getTokenProps,
});
}
}
export default Highlight;