-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-block-syntax-highlighter.ts
More file actions
155 lines (144 loc) · 3.05 KB
/
code-block-syntax-highlighter.ts
File metadata and controls
155 lines (144 loc) · 3.05 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
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
/**
* Tokenization utility for syntax highlighting code snippets.
* This utils will produce syntax-highlighted JSX output using theme colors.
*/
type TokenType = "keyword" | "string" | "number" | "comment" | "operator" | "punctuation" | "function" | "text"
const MASTER_REGEX = new RegExp(
[
// whitespace
"\\s+",
// single-line comment
"//.*?(?=\\n|$)",
// multi-line comment
"/\\*[\\s\\S]*?\\*/",
// hash comment at start of line
"^\\s*#.*$",
// strings
"(['\"])(?:(?!\\1)[^\\\\]|\\\\.)*\\1",
// numbers
"\\d+\\.?\\d*",
// identifiers
"[a-zA-Z_$][a-zA-Z0-9_$]*",
// operators and punctuation
"===|!==|<=|>=|==|!=|&&|\\|\\||\\+\\+|--|[+\\-*/%=<>!?:(){}\\[\\];,.]|[+\\-*/%]=",
// arrow function
"=>",
].join("|"),
"g"
)
const KEYWORDS = [
"import",
"export",
"default",
"from",
"const",
"let",
"var",
"function",
"return",
"if",
"else",
"for",
"while",
"do",
"switch",
"case",
"break",
"continue",
"try",
"catch",
"finally",
"throw",
"new",
"class",
"extends",
"interface",
"type",
"public",
"private",
"protected",
"static",
"async",
"await",
"true",
"false",
"null",
"undefined",
"typeof",
"instanceof",
]
const OPERATORS = [
"+",
"-",
"*",
"/",
"=",
"==",
"===",
"!=",
"!==",
"<",
">",
"<=",
">=",
"&&",
"||",
"!",
"?",
":",
"++",
"--",
"+=",
"-=",
"*=",
"/=",
"=>",
]
const isKeyword = (value: string) => KEYWORDS.includes(value)
const isOperator = (value: string) => OPERATORS.includes(value)
const isFunction = (value: string) => /^[A-Z]/.test(value)
const isWhitespace = (value: string) => /^\s/.test(value)
const isComment = (v: string) => v.startsWith("//") || v.startsWith("/*") || /^\s*#/.test(v)
const isString = (value: string) => /^['"`]/.test(value)
const isNumber = (value: string) => /^\d/.test(value)
const isIdentifier = (value: string) => /^[a-zA-Z_$]/.test(value)
const classifyIdentifier = (value: string) => {
return isKeyword(value) ? "keyword" : isFunction(value) ? "function" : "text"
}
const classifyToken = (value: string) => {
switch (true) {
case isWhitespace(value):
return "text"
case isComment(value):
return "comment"
case isString(value):
return "string"
case isNumber(value):
return "number"
case isIdentifier(value):
return classifyIdentifier(value)
case isOperator(value):
return "operator"
default:
return "punctuation"
}
}
export const tokenize = (code: string) =>
Array.from(code.matchAll(MASTER_REGEX), (match) => ({
type: classifyToken(match[0]),
value: match[0],
}))
const TOKEN_COLORS = {
keyword: "var(--color-code-keyword)",
string: "var(--color-code-string)",
number: "var(--color-code-number)",
comment: "var(--color-code-comment)",
operator: "var(--color-code-operator)",
punctuation: "var(--color-code-punctuation)",
function: "var(--color-code-function)",
text: "var(--color-code-block-text)",
} as const
export const getTokenColor = (type: TokenType) => TOKEN_COLORS[type]
export function isTokenType(value: unknown): value is TokenType {
return typeof value === "string" && value in TOKEN_COLORS
}