Skip to content

Commit cae1eee

Browse files
committed
feat (language): 增加 NodeJS 高亮
1 parent ea1af29 commit cae1eee

3 files changed

Lines changed: 158 additions & 4 deletions

File tree

src-tauri/src/plugins/nodejs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ impl LanguagePlugin for NodeJSPlugin {
88
}
99

1010
fn get_language_name(&self) -> &'static str {
11-
"NodeJS"
11+
"Node.js"
1212
}
1313

1414
fn get_language_key(&self) -> &'static str {
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import type { HighlightRule, LanguageHighlighter } from '../types'
2+
3+
export class NodeJSHighlighter
4+
implements LanguageHighlighter
5+
{
6+
getLanguageName(): string
7+
{
8+
return 'nodejs'
9+
}
10+
11+
getDisplayName(): string
12+
{
13+
return 'Node.js'
14+
}
15+
16+
getOrder(): number
17+
{
18+
return 3
19+
}
20+
21+
getRules(): HighlightRule[]
22+
{
23+
return [
24+
// 1. 注释 (最高优先级)
25+
{
26+
pattern: /\/\/.*$/gm,
27+
className: 'text-gray-500 italic',
28+
priority: 1
29+
},
30+
{
31+
pattern: /\/\*[\s\S]*?\*\//g,
32+
className: 'text-gray-500 italic',
33+
priority: 1
34+
},
35+
36+
// 2. 字符串 (高优先级)
37+
{
38+
pattern: /`(?:[^`\\]|\\.)*`/g,
39+
className: 'text-green-600',
40+
priority: 2
41+
},
42+
{
43+
pattern: /"(?:[^"\\]|\\.)*"/g,
44+
className: 'text-green-600',
45+
priority: 2
46+
},
47+
{
48+
pattern: /'(?:[^'\\]|\\.)*'/g,
49+
className: 'text-green-600',
50+
priority: 2
51+
},
52+
53+
// 3. JavaScript/Node.js 关键字
54+
{
55+
pattern: /\b(const|let|var|function|class|if|else|for|while|do|switch|case|default|break|continue|return|try|catch|finally|throw|new|this|super|extends|import|export|from|as|async|await|yield|typeof|instanceof|in|of|delete|void|null|undefined|true|false)\b/g,
56+
className: 'text-purple-600 font-semibold',
57+
priority: 3
58+
},
59+
60+
// 4. Node.js 特定关键字和全局对象
61+
{
62+
pattern: /\b(require|module|exports|process|global|__dirname|__filename|Buffer|console|setTimeout|setInterval|clearTimeout|clearInterval)\b/g,
63+
className: 'text-indigo-600 font-semibold',
64+
priority: 3
65+
},
66+
67+
// 5. 数字
68+
{
69+
pattern: /\b\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b/g,
70+
className: 'text-blue-600',
71+
priority: 4
72+
},
73+
74+
// 6. 函数调用
75+
{
76+
pattern: /\b([a-zA-Z_$]\w*)(?=\s*\()/g,
77+
className: 'text-orange-600',
78+
priority: 5
79+
},
80+
81+
// 7. 类名定义
82+
{
83+
pattern: /\bclass\s+([A-Z]\w*)/g,
84+
className: 'text-yellow-600 font-semibold',
85+
priority: 6
86+
},
87+
88+
// 8. 对象属性
89+
{
90+
pattern: /\b([a-zA-Z_$]\w*)\s*:/g,
91+
className: 'text-cyan-600',
92+
priority: 7
93+
},
94+
95+
// 9. 箭头函数
96+
{
97+
pattern: /=>/g,
98+
className: 'text-pink-600 font-bold',
99+
priority: 8
100+
},
101+
102+
// 10. 模板字符串插值
103+
{
104+
pattern: /\$\{[^}]*\}/g,
105+
className: 'text-red-600 bg-yellow-100',
106+
priority: 9
107+
},
108+
109+
// 11. require 路径
110+
{
111+
pattern: /require\s*\(\s*(['"`])([^'"`]+)\1\s*\)/g,
112+
className: 'text-emerald-600',
113+
priority: 10
114+
},
115+
116+
// 12. 常见的 Node.js 模块名
117+
{
118+
pattern: /\b(fs|path|http|https|url|crypto|util|events|stream|os|cluster|child_process|readline|querystring|zlib|assert|buffer|domain|punycode|string_decoder|tls|tty|dgram|dns|net|vm|repl)\b/g,
119+
className: 'text-teal-600 font-medium',
120+
priority: 11
121+
},
122+
123+
// 13. 正则表达式
124+
{
125+
pattern: /\/(?:[^\/\\\r\n]|\\.)+\/[gimuy]*/g,
126+
className: 'text-rose-600',
127+
priority: 12
128+
},
129+
130+
// 14. 装饰器 (如果使用 TypeScript)
131+
{
132+
pattern: /@[a-zA-Z_$]\w*/g,
133+
className: 'text-violet-600',
134+
priority: 13
135+
}
136+
]
137+
}
138+
139+
preProcess(code: string): string
140+
{
141+
// Node.js 特定的预处理
142+
// 可以在这里处理一些特殊的语法转换
143+
return code
144+
}
145+
146+
postProcess(highlighted: string): string
147+
{
148+
// Node.js 特定的后处理
149+
// 可以在这里添加一些额外的样式处理
150+
return highlighted
151+
}
152+
}

src/utils/highlighter/manager.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { HighlightMatch, LanguageHighlighter } from './types'
22
import { Python2Highlighter } from './languages/python2'
33
import { Python3Highlighter } from './languages/python3'
4+
import { NodeJSHighlighter } from './languages/nodejs.ts'
45

56
export class HighlightManager
67
{
@@ -18,6 +19,7 @@ export class HighlightManager
1819
{
1920
this.register(new Python2Highlighter())
2021
this.register(new Python3Highlighter())
22+
this.register(new NodeJSHighlighter())
2123
}
2224

2325
/**
@@ -171,9 +173,9 @@ export class HighlightManager
171173
private escapeHtml(text: string): string
172174
{
173175
return text
174-
// .replace(/&/g, '&')
175-
.replace(/</g, '&lt;')
176-
.replace(/>/g, '&gt;')
176+
// .replace(/&/g, '&amp;')
177+
.replace(/</g, '&lt;')
178+
.replace(/>/g, '&gt;')
177179
// .replace(/"/g, '&quot;')
178180
// .replace(/'/g, '&#39;')
179181
}

0 commit comments

Comments
 (0)