diff --git a/jest.config.mjs b/jest.config.mjs index 8b9c36065..e0b4977c2 100644 --- a/jest.config.mjs +++ b/jest.config.mjs @@ -7,22 +7,21 @@ export default { }, moduleNameMapper: { - "^highlight\\.js/lib/languages/(.+)\\.js$": "/src/ui/__mocks__/highlightLanguage.js", + "^highlight\\.js/lib/languages/(.+)$": + "/src/ui/__mocks__/highlightLanguage.js", "^lowlight$": "/src/ui/__mocks__/lowlight.js", "(.+)\\.js": "$1", - "^ink-testing-library$": "/node_modules/ink-testing-library/build/index.js", - "^./SyntaxHighlighter\\.js$": "/src/ui/__mocks__/SyntaxHighlighter.ts", + "^ink-testing-library$": + "/node_modules/ink-testing-library/build/index.js", + "^./SyntaxHighlighter\\.js$": + "/src/ui/__mocks__/SyntaxHighlighter.ts", }, extensionsToTreatAsEsm: [".ts", ".tsx"], preset: "ts-jest/presets/default-esm", testTimeout: 10000, testEnvironment: "node", - testPathIgnorePatterns: [ - "/dist/" - ], - transformIgnorePatterns: [ - "node_modules/(?!(highlight.js|lowlight)/)" - ], + testPathIgnorePatterns: ["/dist/"], + transformIgnorePatterns: ["node_modules/(?!(highlight.js|lowlight)/)"], globals: { __dirname: path.dirname(fileURLToPath(import.meta.url)), __filename: path.resolve(fileURLToPath(import.meta.url)), diff --git a/src/ui/SyntaxHighlighter.ts b/src/ui/SyntaxHighlighter.ts index 5fd488fa2..efd5e8c7c 100644 --- a/src/ui/SyntaxHighlighter.ts +++ b/src/ui/SyntaxHighlighter.ts @@ -1,26 +1,46 @@ -import { createLowlight } from 'lowlight'; -import { Text } from 'ink'; -import React from 'react'; -import js from 'highlight.js/lib/languages/javascript'; -import ts from 'highlight.js/lib/languages/typescript'; -import python from 'highlight.js/lib/languages/python'; -import java from 'highlight.js/lib/languages/java'; -import c from 'highlight.js/lib/languages/c'; -import cpp from 'highlight.js/lib/languages/cpp'; -import csharp from 'highlight.js/lib/languages/csharp'; -import go from 'highlight.js/lib/languages/go'; -import rust from 'highlight.js/lib/languages/rust'; -import php from 'highlight.js/lib/languages/php'; -import ruby from 'highlight.js/lib/languages/ruby'; -import swift from 'highlight.js/lib/languages/swift'; -import kotlin from 'highlight.js/lib/languages/kotlin'; -import sql from 'highlight.js/lib/languages/sql'; -import json from 'highlight.js/lib/languages/json'; -import yaml from 'highlight.js/lib/languages/yaml'; -import bash from 'highlight.js/lib/languages/bash'; -import markdown from 'highlight.js/lib/languages/markdown'; -import css from 'highlight.js/lib/languages/css'; -import xml from 'highlight.js/lib/languages/xml'; +// @ts-ignore - Suppress deprecation warnings for highlight.js imports +import bash from "highlight.js/lib/languages/bash.js"; +// @ts-ignore +import c from "highlight.js/lib/languages/c.js"; +// @ts-ignore +import cpp from "highlight.js/lib/languages/cpp.js"; +// @ts-ignore +import csharp from "highlight.js/lib/languages/csharp.js"; +// @ts-ignore +import css from "highlight.js/lib/languages/css.js"; +// @ts-ignore +import go from "highlight.js/lib/languages/go.js"; +// @ts-ignore +import java from "highlight.js/lib/languages/java.js"; +// @ts-ignore +import js from "highlight.js/lib/languages/javascript.js"; +// @ts-ignore +import json from "highlight.js/lib/languages/json.js"; +// @ts-ignore +import kotlin from "highlight.js/lib/languages/kotlin.js"; +// @ts-ignore +import markdown from "highlight.js/lib/languages/markdown.js"; +// @ts-ignore +import php from "highlight.js/lib/languages/php.js"; +// @ts-ignore +import python from "highlight.js/lib/languages/python.js"; +// @ts-ignore +import ruby from "highlight.js/lib/languages/ruby.js"; +// @ts-ignore +import rust from "highlight.js/lib/languages/rust.js"; +// @ts-ignore +import sql from "highlight.js/lib/languages/sql.js"; +// @ts-ignore +import swift from "highlight.js/lib/languages/swift.js"; +// @ts-ignore +import ts from "highlight.js/lib/languages/typescript.js"; +// @ts-ignore +import xml from "highlight.js/lib/languages/xml.js"; +// @ts-ignore +import yaml from "highlight.js/lib/languages/yaml.js"; +import { Text } from "ink"; +import { createLowlight } from "lowlight"; +import React from "react"; export interface SyntaxHighlighterTheme { keyword: string; @@ -39,69 +59,69 @@ export interface SyntaxHighlighterTheme { } export const defaultTheme: SyntaxHighlighterTheme = { - keyword: 'magenta', - string: 'green', - comment: 'gray', - number: 'cyan', - operator: 'white', - punctuation: 'white', - function: 'yellow', - variable: 'white', - property: 'blue', - class: 'yellow', - constant: 'cyan', - regex: 'red', - default: 'white' + keyword: "magenta", + string: "green", + comment: "gray", + number: "cyan", + operator: "white", + punctuation: "white", + function: "yellow", + variable: "white", + property: "blue", + class: "yellow", + constant: "cyan", + regex: "red", + default: "white", }; // Map highlight.js token types to theme colors const tokenColorMap: Record = { - 'keyword': 'keyword', - 'string': 'string', - 'comment': 'comment', - 'number': 'number', - 'operator': 'operator', - 'punctuation': 'punctuation', - 'function': 'function', - 'variable': 'variable', - 'property': 'property', - 'class': 'class', - 'constant': 'constant', - 'regexp': 'regex', - 'boolean': 'constant', - 'null': 'constant', - 'undefined': 'constant', - 'builtin': 'keyword', - 'tag': 'keyword', - 'attr': 'property', - 'attribute': 'property', - 'selector': 'property', - 'important': 'keyword', - 'atrule': 'keyword', - 'rule': 'keyword', - 'entity': 'constant', - 'url': 'string', - 'symbol': 'constant', - 'prolog': 'comment', - 'doctype': 'comment', - 'cdata': 'comment', - 'title': 'function', - 'section': 'keyword', - 'name': 'function', - 'strong': 'keyword', - 'emphasis': 'string', - 'quote': 'string', - 'built_in': 'keyword', - 'literal': 'constant', - 'type': 'class', - 'params': 'variable', - 'meta': 'comment', - 'link': 'string' + keyword: "keyword", + string: "string", + comment: "comment", + number: "number", + operator: "operator", + punctuation: "punctuation", + function: "function", + variable: "variable", + property: "property", + class: "class", + constant: "constant", + regexp: "regex", + boolean: "constant", + null: "constant", + undefined: "constant", + builtin: "keyword", + tag: "keyword", + attr: "property", + attribute: "property", + selector: "property", + important: "keyword", + atrule: "keyword", + rule: "keyword", + entity: "constant", + url: "string", + symbol: "constant", + prolog: "comment", + doctype: "comment", + cdata: "comment", + title: "function", + section: "keyword", + name: "function", + strong: "keyword", + emphasis: "string", + quote: "string", + built_in: "keyword", + literal: "constant", + type: "class", + params: "variable", + meta: "comment", + link: "string", }; export function highlightCode( - code: string, - language: string = 'javascript', + code: string, + language: string = "javascript", theme: SyntaxHighlighterTheme = defaultTheme ): React.ReactNode[] { try { @@ -128,44 +148,51 @@ export function highlightCode( markdown: markdown, css: css, html: xml, - xml: xml + xml: xml, }); - - const result = lowlight.highlight(language, code, { prefix: '' }); - + + const result = lowlight.highlight(language, code, { prefix: "" }); + // Convert the hast tree to React elements const elements: React.ReactNode[] = []; - + let keyCounter = 0; - - function processNode(node: any, parentColor: string = theme.default): React.ReactNode { - if (node.type === 'text') { + + function processNode( + node: any, + parentColor: string = theme.default + ): React.ReactNode { + if (node.type === "text") { const text = node.value; if (text) { - return React.createElement(Text, { - key: `text-${keyCounter++}`, - color: parentColor - }, text); + return React.createElement( + Text, + { + key: `text-${keyCounter++}`, + color: parentColor, + }, + text + ); } return null; } - - if (node.type === 'element') { + + if (node.type === "element") { const className = node.properties?.className || []; - + // Extract token type from class names - let tokenType = ''; + let tokenType = ""; for (const cls of className) { if (tokenColorMap[cls]) { tokenType = cls; break; } } - + // Get color from theme - const colorKey = tokenColorMap[tokenType] || 'default'; + const colorKey = tokenColorMap[tokenType] || "default"; const color = theme[colorKey]; - + if (node.children && node.children.length > 0) { // Process child nodes with this color const children: React.ReactNode[] = []; @@ -179,35 +206,39 @@ export function highlightCode( } } } - + return children; } - + const textContent = getTextContent(node); if (textContent) { - return React.createElement(Text, { - key: `element-${keyCounter++}`, - color - }, textContent); + return React.createElement( + Text, + { + key: `element-${keyCounter++}`, + color, + }, + textContent + ); } } - + return null; } - + // Helper function to extract text content from a node function getTextContent(node: any): string { - if (node.type === 'text') { - return node.value || ''; + if (node.type === "text") { + return node.value || ""; } - - if (node.type === 'element' && node.children) { - return node.children.map(getTextContent).join(''); + + if (node.type === "element" && node.children) { + return node.children.map(getTextContent).join(""); } - - return ''; + + return ""; } - + // Process all child nodes if (result.children) { for (let i = 0; i < result.children.length; i++) { @@ -221,39 +252,44 @@ export function highlightCode( } } } - + return elements; - } catch (error) { // Fallback to plain text on error - return [React.createElement(Text, { key: 'error-fallback', color: theme.default }, code)]; + return [ + React.createElement( + Text, + { key: "error-fallback", color: theme.default }, + code + ), + ]; } } export function detectLanguage(code: string): string { // Simple language detection based on common patterns const patterns = [ - { regex: /^\s*import\s+.*from\s+['"]/, language: 'javascript' }, - { regex: /^\s*const\s+\w+\s*=\s*require\s*\(/, language: 'javascript' }, - { regex: /^\s*function\s+\w+\s*\(/, language: 'javascript' }, - { regex: /^\s*interface\s+\w+\s*\{/, language: 'typescript' }, - { regex: /^\s*type\s+\w+\s*=/, language: 'typescript' }, - { regex: /^\s*def\s+\w+\s*\(/, language: 'python' }, - { regex: /^\s*class\s+\w+\s*:/, language: 'python' }, - { regex: /^\s*public\s+class\s+\w+/, language: 'java' }, - { regex: /^\s*#include\s*