juxscript 1.1.51 → 1.1.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/utils/codeparser.d.ts +2 -5
- package/lib/utils/codeparser.d.ts.map +1 -1
- package/lib/utils/codeparser.js +39 -32
- package/lib/utils/codeparser.ts +40 -36
- package/package.json +1 -1
|
@@ -8,20 +8,17 @@ export interface ParsedLine {
|
|
|
8
8
|
*/
|
|
9
9
|
declare function escapeHtml(text: string): string;
|
|
10
10
|
/**
|
|
11
|
-
* Parse code into lines
|
|
11
|
+
* Parse code into lines - Simple keyword + special char highlighting
|
|
12
12
|
*/
|
|
13
13
|
export declare function parseCode(code: string, language?: string): ParsedLine[];
|
|
14
14
|
/**
|
|
15
|
-
* Render a parsed line
|
|
15
|
+
* Render a parsed line
|
|
16
16
|
*/
|
|
17
17
|
export declare function renderLineWithTokens(parsedLine: ParsedLine): string;
|
|
18
18
|
/**
|
|
19
19
|
* Generate CSS for syntax highlighting
|
|
20
20
|
*/
|
|
21
21
|
export declare function getSyntaxHighlightCSS(): string;
|
|
22
|
-
/**
|
|
23
|
-
* Main parser export
|
|
24
|
-
*/
|
|
25
22
|
declare const _default: {
|
|
26
23
|
parse: typeof parseCode;
|
|
27
24
|
renderLine: typeof renderLineWithTokens;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codeparser.d.ts","sourceRoot":"","sources":["codeparser.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"codeparser.d.ts","sourceRoot":"","sources":["codeparser.ts"],"names":[],"mappings":"AAkBA,MAAM,WAAW,UAAU;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,iBAAS,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAOxC;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAE,MAAqB,GAAG,UAAU,EAAE,CAQrF;AAmCD;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAEnE;AAED;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,MAAM,CAqE9C;;;;;;;AAED,wBAKE"}
|
package/lib/utils/codeparser.js
CHANGED
|
@@ -7,8 +7,13 @@ const KEYWORDS = new Set([
|
|
|
7
7
|
'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof',
|
|
8
8
|
'let', 'new', 'return', 'super', 'switch', 'this', 'throw', 'try',
|
|
9
9
|
'typeof', 'var', 'void', 'while', 'with', 'yield', 'from', 'of',
|
|
10
|
-
'static', 'get', 'set', 'as', 'interface', 'type', 'enum', 'namespace'
|
|
10
|
+
'static', 'get', 'set', 'as', 'interface', 'type', 'enum', 'namespace',
|
|
11
|
+
// ✅ JUX-specific
|
|
12
|
+
'jux', 'state', 'registry', 'render', 'bind', 'sync'
|
|
11
13
|
]);
|
|
14
|
+
// ✅ Reserved operators and punctuation
|
|
15
|
+
const OPERATORS = ['+', '-', '*', '/', '%', '=', '==', '===', '!=', '!==', '<', '>', '<=', '>=', '!', '&&', '||', '?', ':', '=>'];
|
|
16
|
+
const PUNCTUATION = ['{', '}', '(', ')', '[', ']', ';', ',', '.'];
|
|
12
17
|
/**
|
|
13
18
|
* Escape HTML entities
|
|
14
19
|
*/
|
|
@@ -21,7 +26,7 @@ function escapeHtml(text) {
|
|
|
21
26
|
.replace(/'/g, ''');
|
|
22
27
|
}
|
|
23
28
|
/**
|
|
24
|
-
* Parse code into lines
|
|
29
|
+
* Parse code into lines - Simple keyword + special char highlighting
|
|
25
30
|
*/
|
|
26
31
|
export function parseCode(code, language = 'javascript') {
|
|
27
32
|
const lines = code.split('\n');
|
|
@@ -32,34 +37,35 @@ export function parseCode(code, language = 'javascript') {
|
|
|
32
37
|
}));
|
|
33
38
|
}
|
|
34
39
|
/**
|
|
35
|
-
*
|
|
40
|
+
* Ultra-simple highlighting - Keywords + operators + punctuation
|
|
36
41
|
*/
|
|
37
42
|
function highlightLine(line) {
|
|
38
43
|
if (!line.trim())
|
|
39
44
|
return ' ';
|
|
40
|
-
// Escape
|
|
41
|
-
let
|
|
42
|
-
//
|
|
43
|
-
// 1. Comments
|
|
44
|
-
escaped = escaped.replace(/(\/\/.*$)/g, '<span class="token-comment">$1</span>');
|
|
45
|
-
escaped = escaped.replace(/(\/\*[\s\S]*?\*\/)/g, '<span class="token-comment">$1</span>');
|
|
46
|
-
// 2. Strings (already escaped, so look for " etc)
|
|
47
|
-
escaped = escaped.replace(/("[^&]*?"|'[^&]*?'|`[^`]*?`)/g, '<span class="token-string">$1</span>');
|
|
48
|
-
// 3. Numbers
|
|
49
|
-
escaped = escaped.replace(/\b(\d+\.?\d*)\b/g, '<span class="token-number">$1</span>');
|
|
50
|
-
// 4. Keywords (only whole words)
|
|
45
|
+
// Escape everything first
|
|
46
|
+
let result = escapeHtml(line);
|
|
47
|
+
// 1. Highlight keywords (whole words only)
|
|
51
48
|
KEYWORDS.forEach(keyword => {
|
|
52
49
|
const regex = new RegExp(`\\b(${keyword})\\b`, 'g');
|
|
53
|
-
|
|
50
|
+
result = result.replace(regex, '<span class="token-keyword">$1</span>');
|
|
54
51
|
});
|
|
55
|
-
//
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
52
|
+
// 2. Highlight operators (multi-char first, then single-char)
|
|
53
|
+
const sortedOps = [...OPERATORS].sort((a, b) => b.length - a.length);
|
|
54
|
+
sortedOps.forEach(op => {
|
|
55
|
+
const escaped = op.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
56
|
+
const regex = new RegExp(escaped, 'g');
|
|
57
|
+
result = result.replace(regex, `<span class="token-operator">${escapeHtml(op)}</span>`);
|
|
58
|
+
});
|
|
59
|
+
// 3. Highlight punctuation
|
|
60
|
+
PUNCTUATION.forEach(punct => {
|
|
61
|
+
const escaped = punct.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
62
|
+
const regex = new RegExp(escaped, 'g');
|
|
63
|
+
result = result.replace(regex, `<span class="token-punctuation">${escapeHtml(punct)}</span>`);
|
|
64
|
+
});
|
|
65
|
+
return result;
|
|
60
66
|
}
|
|
61
67
|
/**
|
|
62
|
-
* Render a parsed line
|
|
68
|
+
* Render a parsed line
|
|
63
69
|
*/
|
|
64
70
|
export function renderLineWithTokens(parsedLine) {
|
|
65
71
|
return parsedLine.html;
|
|
@@ -90,11 +96,10 @@ export function getSyntaxHighlightCSS() {
|
|
|
90
96
|
display: flex;
|
|
91
97
|
align-items: flex-start;
|
|
92
98
|
min-height: 1.6em;
|
|
93
|
-
transition: background-color 0.2s;
|
|
94
99
|
}
|
|
95
100
|
|
|
96
101
|
.jux-code-line:hover {
|
|
97
|
-
background-color: rgba(255, 255, 255, 0.
|
|
102
|
+
background-color: rgba(255, 255, 255, 0.03);
|
|
98
103
|
}
|
|
99
104
|
|
|
100
105
|
.jux-code-line-highlight {
|
|
@@ -125,17 +130,19 @@ export function getSyntaxHighlightCSS() {
|
|
|
125
130
|
}
|
|
126
131
|
|
|
127
132
|
/* Token colors */
|
|
128
|
-
.token-keyword {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
.token-
|
|
133
|
-
|
|
133
|
+
.token-keyword {
|
|
134
|
+
color: #c678dd;
|
|
135
|
+
font-weight: 600;
|
|
136
|
+
}
|
|
137
|
+
.token-operator {
|
|
138
|
+
color: #56b6c2;
|
|
139
|
+
font-weight: 500;
|
|
140
|
+
}
|
|
141
|
+
.token-punctuation {
|
|
142
|
+
color: #abb2bf;
|
|
143
|
+
}
|
|
134
144
|
`;
|
|
135
145
|
}
|
|
136
|
-
/**
|
|
137
|
-
* Main parser export
|
|
138
|
-
*/
|
|
139
146
|
export default {
|
|
140
147
|
parse: parseCode,
|
|
141
148
|
renderLine: renderLineWithTokens,
|
package/lib/utils/codeparser.ts
CHANGED
|
@@ -7,9 +7,15 @@ const KEYWORDS = new Set([
|
|
|
7
7
|
'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof',
|
|
8
8
|
'let', 'new', 'return', 'super', 'switch', 'this', 'throw', 'try',
|
|
9
9
|
'typeof', 'var', 'void', 'while', 'with', 'yield', 'from', 'of',
|
|
10
|
-
'static', 'get', 'set', 'as', 'interface', 'type', 'enum', 'namespace'
|
|
10
|
+
'static', 'get', 'set', 'as', 'interface', 'type', 'enum', 'namespace',
|
|
11
|
+
// ✅ JUX-specific
|
|
12
|
+
'jux', 'state', 'registry', 'render', 'bind', 'sync'
|
|
11
13
|
]);
|
|
12
14
|
|
|
15
|
+
// ✅ Reserved operators and punctuation
|
|
16
|
+
const OPERATORS = ['+', '-', '*', '/', '%', '=', '==', '===', '!=', '!==', '<', '>', '<=', '>=', '!', '&&', '||', '?', ':', '=>'];
|
|
17
|
+
const PUNCTUATION = ['{', '}', '(', ')', '[', ']', ';', ',', '.'];
|
|
18
|
+
|
|
13
19
|
export interface ParsedLine {
|
|
14
20
|
lineNumber: number;
|
|
15
21
|
html: string;
|
|
@@ -29,7 +35,7 @@ function escapeHtml(text: string): string {
|
|
|
29
35
|
}
|
|
30
36
|
|
|
31
37
|
/**
|
|
32
|
-
* Parse code into lines
|
|
38
|
+
* Parse code into lines - Simple keyword + special char highlighting
|
|
33
39
|
*/
|
|
34
40
|
export function parseCode(code: string, language: string = 'javascript'): ParsedLine[] {
|
|
35
41
|
const lines = code.split('\n');
|
|
@@ -42,43 +48,40 @@ export function parseCode(code: string, language: string = 'javascript'): Parsed
|
|
|
42
48
|
}
|
|
43
49
|
|
|
44
50
|
/**
|
|
45
|
-
*
|
|
51
|
+
* Ultra-simple highlighting - Keywords + operators + punctuation
|
|
46
52
|
*/
|
|
47
53
|
function highlightLine(line: string): string {
|
|
48
54
|
if (!line.trim()) return ' ';
|
|
49
55
|
|
|
50
|
-
// Escape
|
|
51
|
-
let
|
|
52
|
-
|
|
53
|
-
// Now add spans (on already-escaped text)
|
|
54
|
-
|
|
55
|
-
// 1. Comments
|
|
56
|
-
escaped = escaped.replace(/(\/\/.*$)/g, '<span class="token-comment">$1</span>');
|
|
57
|
-
escaped = escaped.replace(/(\/\*[\s\S]*?\*\/)/g, '<span class="token-comment">$1</span>');
|
|
56
|
+
// Escape everything first
|
|
57
|
+
let result = escapeHtml(line);
|
|
58
58
|
|
|
59
|
-
//
|
|
60
|
-
escaped = escaped.replace(/("[^&]*?"|'[^&]*?'|`[^`]*?`)/g, '<span class="token-string">$1</span>');
|
|
61
|
-
|
|
62
|
-
// 3. Numbers
|
|
63
|
-
escaped = escaped.replace(/\b(\d+\.?\d*)\b/g, '<span class="token-number">$1</span>');
|
|
64
|
-
|
|
65
|
-
// 4. Keywords (only whole words)
|
|
59
|
+
// 1. Highlight keywords (whole words only)
|
|
66
60
|
KEYWORDS.forEach(keyword => {
|
|
67
61
|
const regex = new RegExp(`\\b(${keyword})\\b`, 'g');
|
|
68
|
-
|
|
62
|
+
result = result.replace(regex, '<span class="token-keyword">$1</span>');
|
|
69
63
|
});
|
|
70
64
|
|
|
71
|
-
//
|
|
72
|
-
|
|
65
|
+
// 2. Highlight operators (multi-char first, then single-char)
|
|
66
|
+
const sortedOps = [...OPERATORS].sort((a, b) => b.length - a.length);
|
|
67
|
+
sortedOps.forEach(op => {
|
|
68
|
+
const escaped = op.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
69
|
+
const regex = new RegExp(escaped, 'g');
|
|
70
|
+
result = result.replace(regex, `<span class="token-operator">${escapeHtml(op)}</span>`);
|
|
71
|
+
});
|
|
73
72
|
|
|
74
|
-
//
|
|
75
|
-
|
|
73
|
+
// 3. Highlight punctuation
|
|
74
|
+
PUNCTUATION.forEach(punct => {
|
|
75
|
+
const escaped = punct.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
76
|
+
const regex = new RegExp(escaped, 'g');
|
|
77
|
+
result = result.replace(regex, `<span class="token-punctuation">${escapeHtml(punct)}</span>`);
|
|
78
|
+
});
|
|
76
79
|
|
|
77
|
-
return
|
|
80
|
+
return result;
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
/**
|
|
81
|
-
* Render a parsed line
|
|
84
|
+
* Render a parsed line
|
|
82
85
|
*/
|
|
83
86
|
export function renderLineWithTokens(parsedLine: ParsedLine): string {
|
|
84
87
|
return parsedLine.html;
|
|
@@ -110,11 +113,10 @@ export function getSyntaxHighlightCSS(): string {
|
|
|
110
113
|
display: flex;
|
|
111
114
|
align-items: flex-start;
|
|
112
115
|
min-height: 1.6em;
|
|
113
|
-
transition: background-color 0.2s;
|
|
114
116
|
}
|
|
115
117
|
|
|
116
118
|
.jux-code-line:hover {
|
|
117
|
-
background-color: rgba(255, 255, 255, 0.
|
|
119
|
+
background-color: rgba(255, 255, 255, 0.03);
|
|
118
120
|
}
|
|
119
121
|
|
|
120
122
|
.jux-code-line-highlight {
|
|
@@ -145,18 +147,20 @@ export function getSyntaxHighlightCSS(): string {
|
|
|
145
147
|
}
|
|
146
148
|
|
|
147
149
|
/* Token colors */
|
|
148
|
-
.token-keyword {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
.token-
|
|
153
|
-
|
|
150
|
+
.token-keyword {
|
|
151
|
+
color: #c678dd;
|
|
152
|
+
font-weight: 600;
|
|
153
|
+
}
|
|
154
|
+
.token-operator {
|
|
155
|
+
color: #56b6c2;
|
|
156
|
+
font-weight: 500;
|
|
157
|
+
}
|
|
158
|
+
.token-punctuation {
|
|
159
|
+
color: #abb2bf;
|
|
160
|
+
}
|
|
154
161
|
`;
|
|
155
162
|
}
|
|
156
163
|
|
|
157
|
-
/**
|
|
158
|
-
* Main parser export
|
|
159
|
-
*/
|
|
160
164
|
export default {
|
|
161
165
|
parse: parseCode,
|
|
162
166
|
renderLine: renderLineWithTokens,
|