(text, uri, symbolName = null)
| 191 | * @param {string|null} symbolName - Optional symbol to highlight with special styling |
| 192 | */ |
| 193 | export async function highlightLine(text, uri, symbolName = null) { |
| 194 | if (!text || !text.trim()) return ""; |
| 195 | |
| 196 | const themeId = settings?.value?.editorTheme || "one_dark"; |
| 197 | const cacheKey = `line:${themeId}:${uri}:${text}:${symbolName || ""}`; |
| 198 | |
| 199 | if (highlightCache.has(cacheKey)) { |
| 200 | return highlightCache.get(cacheKey); |
| 201 | } |
| 202 | |
| 203 | const trimmedText = text.trim(); |
| 204 | |
| 205 | try { |
| 206 | const parser = await getLanguageParser(uri); |
| 207 | if (parser) { |
| 208 | const tree = parser.parse(trimmedText); |
| 209 | let result = ""; |
| 210 | |
| 211 | highlightCode( |
| 212 | trimmedText, |
| 213 | tree, |
| 214 | classHighlighter, |
| 215 | (code, classes) => { |
| 216 | if (classes) { |
| 217 | result += `<span class="${classes}">${escapeHtml(code)}</span>`; |
| 218 | } else { |
| 219 | result += escapeHtml(code); |
| 220 | } |
| 221 | }, |
| 222 | () => {}, |
| 223 | ); |
| 224 | |
| 225 | if (result) { |
| 226 | const highlighted = symbolName |
| 227 | ? addSymbolHighlight(result, symbolName) |
| 228 | : result; |
| 229 | setCache(cacheKey, highlighted); |
| 230 | return highlighted; |
| 231 | } |
| 232 | } |
| 233 | } catch (e) { |
| 234 | console.warn("Highlighting failed for", uri, e); |
| 235 | } |
| 236 | |
| 237 | const escaped = escapeHtml(trimmedText); |
| 238 | const highlighted = symbolName |
| 239 | ? addSymbolHighlight(escaped, symbolName) |
| 240 | : escaped; |
| 241 | setCache(cacheKey, highlighted); |
| 242 | return highlighted; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Highlights a code block for display in markdown/plugin pages |
no test coverage detected