| 1 | import { Text } from "ink"; |
| 2 | import { common, createLowlight } from "lowlight"; |
| 3 | import React from "react"; |
| 4 | |
| 5 | export interface SyntaxHighlighterTheme { |
| 6 | keyword: string; |
| 7 | string: string; |
| 8 | comment: string; |
| 9 | number: string; |
| 10 | operator: string; |
| 11 | punctuation: string; |
| 12 | function: string; |
| 13 | variable: string; |
| 14 | property: string; |
| 15 | class: string; |
| 16 | constant: string; |
| 17 | regex: string; |
| 18 | default: string; |
| 19 | } |
| 20 | |
| 21 | export const defaultTheme: SyntaxHighlighterTheme = { |
| 22 | keyword: "magenta", |
| 23 | string: "green", |
| 24 | comment: "gray", |
| 25 | number: "cyan", |
| 26 | operator: "white", |
| 27 | punctuation: "white", |
| 28 | function: "yellow", |
| 29 | variable: "white", |
| 30 | property: "blue", |
| 31 | class: "yellow", |
| 32 | constant: "cyan", |
| 33 | regex: "red", |
| 34 | default: "white", |
| 35 | }; |
| 36 | |
| 37 | // Map highlight.js token types to theme colors |
| 38 | const tokenColorMap: Record<string, keyof SyntaxHighlighterTheme> = { |
| 39 | keyword: "keyword", |
| 40 | string: "string", |
| 41 | comment: "comment", |
| 42 | number: "number", |
| 43 | operator: "operator", |
| 44 | punctuation: "punctuation", |
| 45 | function: "function", |
| 46 | variable: "variable", |
| 47 | property: "property", |
| 48 | class: "class", |
| 49 | constant: "constant", |
| 50 | regexp: "regex", |
| 51 | boolean: "constant", |
| 52 | null: "constant", |
| 53 | undefined: "constant", |
| 54 | builtin: "keyword", |
| 55 | tag: "keyword", |
| 56 | attr: "property", |
| 57 | attribute: "property", |
| 58 | selector: "property", |
| 59 | important: "keyword", |
| 60 | atrule: "keyword", |
| 61 | rule: "keyword", |
| 62 | entity: "constant", |
| 63 | url: "string", |
| 64 | symbol: "constant", |
| 65 | prolog: "comment", |
| 66 | doctype: "comment", |
| 67 | cdata: "comment", |
| 68 | title: "function", |
| 69 | section: "keyword", |
| 70 | name: "function", |
| 71 | strong: "keyword", |
| 72 | emphasis: "string", |
| 73 | quote: "string", |
| 74 | built_in: "keyword", |
| 75 | literal: "constant", |
| 76 | type: "class", |
| 77 | params: "variable", |
| 78 | meta: "comment", |
| 79 | link: "string", |
| 80 | }; |
| 81 | |
| 82 | export function highlightCode( |
| 83 | code: string, |
| 84 | language: string = "javascript", |
| 85 | theme: SyntaxHighlighterTheme = defaultTheme, |
| 86 | ): React.ReactNode[] { |
| 87 | try { |
| 88 | // Create lowlight instance with common languages |
| 89 | const lowlight = createLowlight(common); |
| 90 | |
| 91 | const result = lowlight.highlight(language, code, { prefix: "" }); |
| 92 | |
| 93 | // Convert the hast tree to React elements |
| 94 | const elements: React.ReactNode[] = []; |
| 95 | |
| 96 | let keyCounter = 0; |
| 97 | |
| 98 | function processNode( |
| 99 | node: any, |
| 100 | parentColor: string = theme.default, |
| 101 | ): React.ReactNode { |
| 102 | if (node.type === "text") { |
| 103 | const text = node.value; |
| 104 | if (text) { |
| 105 | return React.createElement( |
| 106 | Text, |
| 107 | { |
| 108 | key: `text-${keyCounter++}`, |
| 109 | color: parentColor, |
| 110 | }, |
| 111 | text, |
| 112 | ); |
| 113 | } |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | if (node.type === "element") { |
| 118 | const className = node.properties?.className || []; |
| 119 | |
| 120 | // Extract token type from class names |
| 121 | let tokenType = ""; |
| 122 | for (const cls of className) { |
| 123 | if (tokenColorMap[cls]) { |
| 124 | tokenType = cls; |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Get color from theme |
| 130 | const colorKey = tokenColorMap[tokenType] || "default"; |
| 131 | const color = theme[colorKey]; |
| 132 | |
| 133 | if (node.children && node.children.length > 0) { |
| 134 | // Process child nodes with this color |
| 135 | const children: React.ReactNode[] = []; |
| 136 | for (let i = 0; i < node.children.length; i++) { |
| 137 | const child = processNode(node.children[i], color); |
| 138 | if (!child) continue; |
| 139 | |
| 140 | if (Array.isArray(child)) { |
| 141 | children.push(...child); |
| 142 | } else { |
| 143 | children.push(child); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return children; |
| 148 | } |
| 149 | |
| 150 | const textContent = getTextContent(node); |
| 151 | if (textContent) { |
| 152 | return React.createElement( |
| 153 | Text, |
| 154 | { |
| 155 | key: `element-${keyCounter++}`, |
| 156 | color, |
| 157 | }, |
| 158 | textContent, |
| 159 | ); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return null; |
| 164 | } |
| 165 | |
| 166 | // Helper function to extract text content from a node |
| 167 | function getTextContent(node: any): string { |
| 168 | if (node.type === "text") { |
| 169 | return node.value || ""; |
| 170 | } |
| 171 | |
| 172 | if (node.type === "element" && node.children) { |
| 173 | return node.children.map(getTextContent).join(""); |
| 174 | } |
| 175 | |
| 176 | return ""; |
| 177 | } |
| 178 | |
| 179 | // Process all child nodes |
| 180 | if (result.children) { |
| 181 | for (let i = 0; i < result.children.length; i++) { |
| 182 | const element = processNode(result.children[i]); |
| 183 | if (!element) continue; |
| 184 | |
| 185 | if (Array.isArray(element)) { |
| 186 | elements.push(...element); |
| 187 | } else { |
| 188 | elements.push(element); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return elements; |
| 194 | } catch { |
| 195 | // Fallback to plain text on error |
| 196 | return [ |
| 197 | React.createElement( |
| 198 | Text, |
| 199 | { key: "error-fallback", color: theme.default }, |
| 200 | code, |
| 201 | ), |
| 202 | ]; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | export function detectLanguage(code: string): string { |
| 207 | // Simple language detection based on common patterns |
| 208 | const patterns = [ |
| 209 | { regex: /^\s*import\s+.*from\s+['"]/, language: "javascript" }, |
| 210 | { regex: /^\s*const\s+\w+\s*=\s*require\s*\(/, language: "javascript" }, |
| 211 | { regex: /^\s*function\s+\w+\s*\(/, language: "javascript" }, |
| 212 | { regex: /^\s*interface\s+\w+/, language: "typescript" }, |
| 213 | { regex: /^\s*type\s+\w+\s*=/, language: "typescript" }, |
| 214 | { regex: /^\s*def\s+\w+.*:/, language: "python" }, |
| 215 | { regex: /^\s*class\s+\w+.*:/, language: "python" }, |
| 216 | { regex: /^\s*public\s+class\s+\w+/, language: "java" }, |
| 217 | { regex: /^\s*#include\s*</, language: "c" }, |
| 218 | { regex: /^\s*using\s+namespace\s+/, language: "cpp" }, |
| 219 | { regex: /^\s*using\s+System\s*;/, language: "csharp" }, |
| 220 | { regex: /^\s*package\s+main/, language: "go" }, |
| 221 | { regex: /^\s*fn\s+\w+\s*\(/, language: "rust" }, |
| 222 | { regex: /^\s*<\?php/, language: "php" }, |
| 223 | { regex: /^\s*SELECT\s+.*FROM\s+/i, language: "sql" }, |
| 224 | { regex: /^\s*\{\s*$/, language: "json" }, |
| 225 | { regex: /^\s*---\s*$/, language: "yaml" }, |
| 226 | { regex: /^\s*#!/, language: "bash" }, |
| 227 | { regex: /^\s*<(!DOCTYPE html|html)/i, language: "html" }, |
| 228 | { regex: /^\s*@media\s+/, language: "css" }, |
| 229 | { regex: /^\s*#\s+/, language: "markdown" }, |
| 230 | ]; |
| 231 | |
| 232 | // Check patterns line by line for multiline code |
| 233 | const lines = code.split("\n"); |
| 234 | for (const line of lines) { |
| 235 | for (const pattern of patterns) { |
| 236 | if (pattern.regex.test(line)) { |
| 237 | return pattern.language; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return "javascript"; // default fallback |
| 243 | } |