Skip to content

Commit 4e16f91

Browse files
committed
refactor: improve formatting and readability of Go and TSX syntax highlighter functions
1 parent dfeb8c9 commit 4e16f91

1 file changed

Lines changed: 68 additions & 14 deletions

File tree

docs/src/components/landing/code-block.tsx

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,45 @@ function esc(s: string): string {
1313
// Pass 2: each token maps to an HTML span; unmatched text is plain (escaped).
1414
function highlightGo(code: string): string {
1515
const goKeywords = new Set([
16-
"package", "import", "func", "return", "if", "else", "for", "range",
17-
"var", "const", "type", "struct", "interface", "map", "chan", "go",
18-
"defer", "select", "case", "switch", "default", "break", "continue",
19-
"fallthrough", "nil", "true", "false", "err",
16+
"package",
17+
"import",
18+
"func",
19+
"return",
20+
"if",
21+
"else",
22+
"for",
23+
"range",
24+
"var",
25+
"const",
26+
"type",
27+
"struct",
28+
"interface",
29+
"map",
30+
"chan",
31+
"go",
32+
"defer",
33+
"select",
34+
"case",
35+
"switch",
36+
"default",
37+
"break",
38+
"continue",
39+
"fallthrough",
40+
"nil",
41+
"true",
42+
"false",
43+
"err",
2044
]);
2145
const goTypes = new Set([
22-
"string", "int", "int64", "float64", "bool", "error", "byte", "rune", "any",
46+
"string",
47+
"int",
48+
"int64",
49+
"float64",
50+
"bool",
51+
"error",
52+
"byte",
53+
"rune",
54+
"any",
2355
]);
2456

2557
// Groups: 1=comment, 2=string, 3=backtick-string, 4=word, 5=func-call (UpperWord before '(')
@@ -28,9 +60,8 @@ function highlightGo(code: string): string {
2860

2961
let out = "";
3062
let last = 0;
31-
let m: RegExpExecArray | null;
3263

33-
while ((m = tokenRe.exec(code)) !== null) {
64+
for (let m = tokenRe.exec(code); m !== null; m = tokenRe.exec(code)) {
3465
// Append any unmatched text before this token.
3566
if (m.index > last) {
3667
out += esc(code.slice(last, m.index));
@@ -55,7 +86,10 @@ function highlightGo(code: string): string {
5586
out += `<span class="text-purple-400 font-medium">${esc(word)}</span>`;
5687
} else if (goTypes.has(word)) {
5788
out += `<span class="text-cyan-400">${esc(word)}</span>`;
58-
} else if (/^[A-Z]/.test(word) && code.slice(last).trimStart().startsWith("(")) {
89+
} else if (
90+
/^[A-Z]/.test(word) &&
91+
code.slice(last).trimStart().startsWith("(")
92+
) {
5993
// Uppercase word followed by '(' — function/method call
6094
out += `<span class="text-blue-400">${esc(word)}</span>`;
6195
} else {
@@ -77,10 +111,31 @@ function highlightGo(code: string): string {
77111
// Tokenize-then-render TSX/JSX syntax highlighter.
78112
function highlightTSX(code: string): string {
79113
const tsxKeywords = new Set([
80-
"import", "export", "from", "const", "let", "var", "function", "return",
81-
"if", "else", "for", "while", "default", "new", "this", "class",
82-
"extends", "async", "await", "typeof", "instanceof", "null", "undefined",
83-
"true", "false",
114+
"import",
115+
"export",
116+
"from",
117+
"const",
118+
"let",
119+
"var",
120+
"function",
121+
"return",
122+
"if",
123+
"else",
124+
"for",
125+
"while",
126+
"default",
127+
"new",
128+
"this",
129+
"class",
130+
"extends",
131+
"async",
132+
"await",
133+
"typeof",
134+
"instanceof",
135+
"null",
136+
"undefined",
137+
"true",
138+
"false",
84139
]);
85140

86141
// Tokenize: groups in priority order.
@@ -91,10 +146,9 @@ function highlightTSX(code: string): string {
91146

92147
let out = "";
93148
let last = 0;
94-
let m: RegExpExecArray | null;
95149

96150
// Track whether we're inside a JSX tag (between < and >) for prop detection.
97-
while ((m = tokenRe.exec(code)) !== null) {
151+
for (let m = tokenRe.exec(code); m !== null; m = tokenRe.exec(code)) {
98152
if (m.index > last) {
99153
out += esc(code.slice(last, m.index));
100154
}

0 commit comments

Comments
 (0)