forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeHighlight.js
More file actions
323 lines (282 loc) · 9.14 KB
/
Copy pathcodeHighlight.js
File metadata and controls
323 lines (282 loc) · 9.14 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { classHighlighter, highlightCode } from "@lezer/highlight";
import { getModeForPath, getModesByName } from "cm/modelist";
import { getThemeConfig } from "cm/themes";
import DOMPurify from "dompurify";
import settings from "lib/settings";
const highlightCache = new Map();
const MAX_CACHE_SIZE = 500;
let styleElement = null;
let currentThemeId = null;
export function sanitize(text) {
if (!text) return "";
return DOMPurify.sanitize(text, { ALLOWED_TAGS: [] });
}
function escapeHtml(text) {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function addSymbolHighlight(html, symbol) {
if (!symbol) return html;
const escapedSymbol = escapeRegExp(sanitize(symbol));
const regex = new RegExp(`(${escapedSymbol})`, "gi");
return html.replace(regex, '<span class="symbol-match">$1</span>');
}
function setCache(key, value) {
if (highlightCache.size >= MAX_CACHE_SIZE) {
const firstKey = highlightCache.keys().next().value;
highlightCache.delete(firstKey);
}
highlightCache.set(key, value);
}
/**
* Generates CSS styles for syntax highlighting tokens
* @param {Object} config - Theme config with color values
* @param {string} selector - CSS selector to scope styles
* @param {boolean} includeBackground - Whether to include background/foreground base styles
*/
function generateStyles(config, selector, includeBackground = true) {
const c = config;
const keyword = c.keyword || "#c678dd";
const string = c.string || "#98c379";
const number = c.number || "#d19a66";
const comment = c.comment || "#5c6370";
const func = c.function || "#61afef";
const variable = c.variable || "#e06c75";
const type = c.type || "#e5c07b";
const className = c.class || type;
const constant = c.constant || number;
const operator = c.operator || keyword;
const invalid = c.invalid || "#ff6b6b";
const foreground = c.foreground || "#abb2bf";
const background = c.background || "#282c34";
const baseStyles = includeBackground
? `
${selector} {
background: ${background};
color: ${foreground};
}`
: "";
return `${baseStyles}
${selector} .tok-keyword { color: ${keyword}; }
${selector} .tok-operator { color: ${operator}; }
${selector} .tok-number { color: ${number}; }
${selector} .tok-string { color: ${string}; }
${selector} .tok-comment { color: ${comment}; font-style: italic; }
${selector} .tok-variableName { color: ${variable}; }
${selector} .tok-propertyName { color: ${func}; }
${selector} .tok-typeName { color: ${type}; }
${selector} .tok-className { color: ${className}; }
${selector} .tok-function { color: ${func}; }
${selector} .tok-bool { color: ${constant}; }
${selector} .tok-null { color: ${constant}; }
${selector} .tok-punctuation { color: ${foreground}; }
${selector} .tok-definition { color: ${variable}; }
${selector} .tok-labelName { color: ${variable}; }
${selector} .tok-namespace { color: ${type}; }
${selector} .tok-macroName { color: ${keyword}; }
${selector} .tok-atom { color: ${constant}; }
${selector} .tok-meta { color: ${foreground}; }
${selector} .tok-heading { color: ${variable}; font-weight: bold; }
${selector} .tok-link { color: ${func}; text-decoration: underline; }
${selector} .tok-strikethrough { text-decoration: line-through; }
${selector} .tok-emphasis { font-style: italic; }
${selector} .tok-strong { font-weight: bold; }
${selector} .tok-invalid { color: ${invalid}; }
${selector} .tok-name { color: ${variable}; }
${selector} .tok-deleted { color: ${invalid}; }
${selector} .tok-inserted { color: ${string}; }
${selector} .tok-changed { color: ${number}; }
`.trim();
}
/**
* Injects dynamic CSS for syntax highlighting based on current editor theme
*/
function injectStyles() {
const themeId = settings?.value?.editorTheme || "one_dark";
const config = getThemeConfig(themeId);
// Code blocks need background, references panel uses parent's background
const codeBlockStyles = generateStyles(config, ".cm-highlighted", true);
const refPreviewStyles = generateStyles(config, ".ref-preview", false);
const allStyles = `${codeBlockStyles}\n${refPreviewStyles}`;
if (!styleElement) {
styleElement = document.createElement("style");
styleElement.id = "cm-static-highlight-styles";
document.head.appendChild(styleElement);
}
styleElement.textContent = allStyles;
currentThemeId = themeId;
}
/**
* Gets the language parser for a given URI using the modelist
*/
async function getLanguageParser(uri) {
const mode = getModeForPath(uri);
if (!mode?.languageExtension) return null;
try {
const langExt = await mode.languageExtension();
if (!langExt) return null;
const langArray = Array.isArray(langExt) ? langExt : [langExt];
for (const ext of langArray) {
if (ext && typeof ext === "object" && "language" in ext) {
return ext.language.parser;
}
}
} catch (e) {
console.warn("Failed to get language parser for", uri, e);
}
return null;
}
/**
* Gets language parser by language name (e.g., "javascript", "python")
* Uses modelist to find the mode and get first valid extension for file matching
*/
async function getParserForLanguage(langName) {
if (!langName) return null;
const modesByName = getModesByName();
const normalizedName = langName.toLowerCase();
// Try to find mode by name (case-insensitive)
const mode = modesByName[normalizedName];
if (mode?.languageExtension) {
try {
const langExt = await mode.languageExtension();
if (!langExt) return null;
const langArray = Array.isArray(langExt) ? langExt : [langExt];
for (const ext of langArray) {
if (ext && typeof ext === "object" && "language" in ext) {
return ext.language.parser;
}
}
} catch (e) {
console.warn("Failed to get parser for language:", langName, e);
}
}
// Fallback: create a fake filename and use getModeForPath
// This handles cases where the language name doesn't match mode name exactly
const fakeUri = `file.${normalizedName}`;
return await getLanguageParser(fakeUri);
}
/**
* Highlights a single line of code for display in references panel
* @param {string} text - The line of code to highlight
* @param {string} uri - File URI for language detection
* @param {string|null} symbolName - Optional symbol to highlight with special styling
*/
export async function highlightLine(text, uri, symbolName = null) {
if (!text || !text.trim()) return "";
const themeId = settings?.value?.editorTheme || "one_dark";
const cacheKey = `line:${themeId}:${uri}:${text}:${symbolName || ""}`;
if (highlightCache.has(cacheKey)) {
return highlightCache.get(cacheKey);
}
const trimmedText = text.trim();
try {
const parser = await getLanguageParser(uri);
if (parser) {
const tree = parser.parse(trimmedText);
let result = "";
highlightCode(
trimmedText,
tree,
classHighlighter,
(code, classes) => {
if (classes) {
result += `<span class="${classes}">${escapeHtml(code)}</span>`;
} else {
result += escapeHtml(code);
}
},
() => {},
);
if (result) {
const highlighted = symbolName
? addSymbolHighlight(result, symbolName)
: result;
setCache(cacheKey, highlighted);
return highlighted;
}
}
} catch (e) {
console.warn("Highlighting failed for", uri, e);
}
const escaped = escapeHtml(trimmedText);
const highlighted = symbolName
? addSymbolHighlight(escaped, symbolName)
: escaped;
setCache(cacheKey, highlighted);
return highlighted;
}
/**
* Highlights a code block for display in markdown/plugin pages
* @param {string} code - The code to highlight
* @param {string} language - Language identifier from markdown fence (e.g., "javascript", "python")
*/
export async function highlightCodeBlock(code, language) {
if (!code) return "";
const themeId = settings?.value?.editorTheme || "one_dark";
const langKey = (language || "text").toLowerCase();
const cacheKey = `block:${themeId}:${langKey}:${code}`;
if (highlightCache.has(cacheKey)) {
return highlightCache.get(cacheKey);
}
try {
const parser = await getParserForLanguage(langKey);
if (parser) {
const tree = parser.parse(code);
let result = "";
highlightCode(
code,
tree,
classHighlighter,
(text, classes) => {
if (classes) {
result += `<span class="${classes}">${escapeHtml(text)}</span>`;
} else {
result += escapeHtml(text);
}
},
() => {
result += "\n";
},
);
if (result) {
setCache(cacheKey, result);
return result;
}
}
} catch (e) {
console.warn("Code block highlighting failed for", language, e);
}
const escaped = escapeHtml(code);
setCache(cacheKey, escaped);
return escaped;
}
export function clearHighlightCache() {
highlightCache.clear();
}
/**
* Initializes the static code highlighting system.
* Injects theme-based CSS and sets up listener for theme changes.
*/
export function initHighlighting() {
injectStyles();
settings.on("update:editorTheme:after", () => {
const newThemeId = settings?.value?.editorTheme || "one_dark";
if (newThemeId !== currentThemeId) {
injectStyles();
highlightCache.clear();
}
});
}
export default {
sanitize,
highlightLine,
highlightCodeBlock,
clearHighlightCache,
initHighlighting,
};