-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (126 loc) · 3.24 KB
/
index.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
export const useFalse = (bools, val) => {
bools.forEach((bool) => {
bool.current = false;
});
return val || "";
};
export const usingTrue = (bools, val) => {
bools.forEach((bool) => {
bool.current = true;
});
return val || "";
};
export const useFlip = (bools, val) => {
bools.forEach((bool) => {
bool.current = !bool.current;
});
return val || "";
};
const useHighlight = (code, colors) => {
const {
normalName: normalColor,
stringName: stringColor,
className: classColor,
keywordName: keywordColor,
varName: varColor,
propertyName: propertyColor,
} = colors;
let isString = useRef(false);
let nextString = useRef(false);
let currentString = useRef("");
let isEscaped = useRef(false);
let isProperty = useRef(false);
let isObject = useRef(false);
let escapeHighlight = useRef(false);
let isClass = useRef(false);
let isKeyword = useRef("");
const keywords = [
"break",
"case",
"catch",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"else",
"export",
"extend",
"finally",
"for",
"function",
"if",
"import",
"in",
"instanceof",
"new",
"return",
"super",
"switch",
"this",
"throw",
"try",
"typeof",
"var",
"void",
"while",
"with",
"yield",
];
return (
<code>
{code.split("").map((piece) => {
if (piece === "\\" && !isString.current) isEscaped.current = true;
if (
(piece === '"' || piece === "'" || piece === "`") &&
isString.current &&
piece === currentString.current
) {
isString.current = false;
nextString.current = true;
} else if (
(piece === '"' || piece === "'" || piece === "`") &&
!isString.current
) {
isString.current = true;
currentString.current = piece;
}
if (!/[ .;[\]{}<>]/gi.test(piece) && !isObject.current)
isProperty.current = !isProperty.current;
else isObject.current = true;
if (piece.toUpperCase() === piece) isClass.current = true;
if (/[.,:;()[\]{}<>\-+*/=|&~!@#$%^&?]/.test(piece)) {
escapeHighlight.current = true;
isClass.current = false;
} else escapeHighlight.current = false;
if (!/[a-zA-Z]/.test(piece)) isKeyword.current += piece;
else isKeyword.current = "";
let highlight = isString.current
? stringColor || "#8c00ff"
: isEscaped.current
? useFlip([isEscaped, isString], "string")
: nextString.current
? useFalse([nextString], "string")
: escapeHighlight.current
? normalColor || "#111111"
: isClass.current
? classColor || "#0044ff"
: keywords.includes(isKeyword.current)
? keywordColor || "#6542ff"
: isObject.current
? varColor || "#0077ff"
: !isObject.current && isProperty
? propertyColor || "#521dff"
: normalColor || "#111111";
return (
<span style={{ color: highlight }} key={Math.random()}>
{piece}
</span>
);
})}
</code>
);
};
export default useHighlight;