sugar-high 0.0.5 → 0.0.9
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/README.md +21 -20
- package/index.d.ts +2 -0
- package/lib/index.mjs +54 -30
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -21,38 +21,39 @@ document.querySelector('pre > code').innerHTML = codeHTML
|
|
|
21
21
|
|
|
22
22
|
### Highlight with CSS
|
|
23
23
|
|
|
24
|
-
Then make your own theme with customized colors and put in global CSS
|
|
24
|
+
Then make your own theme with customized colors by token type and put in global CSS. The corresponding class names star with `sh__` prefix.
|
|
25
25
|
|
|
26
26
|
```css
|
|
27
|
-
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
27
|
+
/**
|
|
28
|
+
* Types that sugar-high have:
|
|
29
|
+
*
|
|
30
|
+
* identifier
|
|
31
|
+
* keyword
|
|
32
|
+
* string
|
|
33
|
+
* Class, number and null
|
|
34
|
+
* sign
|
|
35
|
+
* comment
|
|
36
|
+
*
|
|
36
37
|
*/
|
|
37
|
-
.
|
|
38
|
-
color: #
|
|
38
|
+
.sh__class {
|
|
39
|
+
color: #2d5e9d;
|
|
39
40
|
}
|
|
40
|
-
.
|
|
41
|
+
.sh__identifier {
|
|
41
42
|
color: #2d333b;
|
|
42
43
|
}
|
|
43
|
-
.
|
|
44
|
-
color: #
|
|
45
|
-
font-weight: bold;
|
|
44
|
+
.sh__sign {
|
|
45
|
+
color: #8996a3;
|
|
46
46
|
}
|
|
47
|
-
.
|
|
47
|
+
.sh__string {
|
|
48
48
|
color: #00a99a;
|
|
49
49
|
}
|
|
50
|
-
.
|
|
50
|
+
.sh__keyword {
|
|
51
51
|
color: #f47067;
|
|
52
52
|
}
|
|
53
|
-
.
|
|
54
|
-
color: #
|
|
53
|
+
.sh__comment {
|
|
54
|
+
color: #a19595;
|
|
55
55
|
}
|
|
56
|
+
|
|
56
57
|
```
|
|
57
58
|
|
|
58
59
|
### LICENSE
|
package/index.d.ts
ADDED
package/lib/index.mjs
CHANGED
|
@@ -62,6 +62,7 @@ const signs = new Set([
|
|
|
62
62
|
':',
|
|
63
63
|
'.',
|
|
64
64
|
',',
|
|
65
|
+
';',
|
|
65
66
|
`'`,
|
|
66
67
|
'"',
|
|
67
68
|
'.',
|
|
@@ -73,28 +74,39 @@ const signs = new Set([
|
|
|
73
74
|
'\\',
|
|
74
75
|
])
|
|
75
76
|
|
|
77
|
+
const types = [
|
|
78
|
+
'identifier',
|
|
79
|
+
'keyword',
|
|
80
|
+
'string',
|
|
81
|
+
'class',
|
|
82
|
+
'sign',
|
|
83
|
+
'comment',
|
|
84
|
+
'break',
|
|
85
|
+
'space',
|
|
86
|
+
]
|
|
87
|
+
|
|
76
88
|
/**
|
|
77
89
|
*
|
|
78
|
-
* 0 -
|
|
90
|
+
* 0 - identifier
|
|
79
91
|
* 1 - keyword
|
|
80
|
-
* 2 -
|
|
81
|
-
* 3 -
|
|
82
|
-
* 4 -
|
|
83
|
-
* 5 -
|
|
84
|
-
* 6 -
|
|
85
|
-
* 7 -
|
|
92
|
+
* 2 - string
|
|
93
|
+
* 3 - Class, number and null
|
|
94
|
+
* 4 - sign
|
|
95
|
+
* 5 - comment
|
|
96
|
+
* 6 - break
|
|
97
|
+
* 7 - space
|
|
86
98
|
*
|
|
87
99
|
*/
|
|
88
100
|
const [
|
|
89
|
-
|
|
101
|
+
T_IDENTIFIER,
|
|
90
102
|
T_KEYWORD,
|
|
91
|
-
T_BREAK,
|
|
92
103
|
T_STRING,
|
|
93
|
-
T_SPACE,
|
|
94
|
-
T_SIGN,
|
|
95
|
-
T_IDENTIFIER,
|
|
96
104
|
T_CLS_NUMBER,
|
|
97
|
-
|
|
105
|
+
T_SIGN,
|
|
106
|
+
T_COMMENT,
|
|
107
|
+
T_BREAK,
|
|
108
|
+
T_SPACE,
|
|
109
|
+
] = types.map((_, i) => i)
|
|
98
110
|
|
|
99
111
|
function isSpaces(str) {
|
|
100
112
|
return /^[^\S\r\n]+$/g.test(str)
|
|
@@ -151,8 +163,11 @@ export function tokenize(code) {
|
|
|
151
163
|
/** @type {{ tag: boolean; child: boolean; expr: boolean }} */
|
|
152
164
|
const jsx = { tag: false, child: false, expr: false }
|
|
153
165
|
|
|
166
|
+
const inJsxLiterals = () => !jsx.tag && jsx.child && !jsx.expr
|
|
167
|
+
|
|
154
168
|
function classify(token) {
|
|
155
|
-
|
|
169
|
+
const [chr0, chr1] = [token[0], token[1]]
|
|
170
|
+
if (isCommentStart(chr0 + chr1)) {
|
|
156
171
|
return T_COMMENT
|
|
157
172
|
} else if (keywords.has(token)) {
|
|
158
173
|
return T_KEYWORD
|
|
@@ -161,19 +176,23 @@ export function tokenize(code) {
|
|
|
161
176
|
} else if (
|
|
162
177
|
(
|
|
163
178
|
// is quoted string
|
|
164
|
-
(isStringQuotation(
|
|
179
|
+
(isStringQuotation(chr0) && !isStringQuotation(chr1)) ||
|
|
165
180
|
// is regex
|
|
166
|
-
(!jsx.tag && isRegexStart(
|
|
181
|
+
(!jsx.tag && isRegexStart(chr0 + chr1) && token[token.length - 1] === '/')
|
|
167
182
|
)
|
|
168
183
|
) {
|
|
169
184
|
return T_STRING
|
|
170
185
|
} else if (token === ' ') {
|
|
171
186
|
return T_SPACE
|
|
172
|
-
} else if (signs.has(
|
|
187
|
+
} else if (signs.has(chr0)) {
|
|
173
188
|
return T_SIGN
|
|
174
189
|
} else if (
|
|
175
|
-
|
|
176
|
-
|
|
190
|
+
!inJsxLiterals() &&
|
|
191
|
+
(
|
|
192
|
+
isIdentifierChar(chr0) &&
|
|
193
|
+
chr0 === chr0.toUpperCase() ||
|
|
194
|
+
token === 'null'
|
|
195
|
+
)
|
|
177
196
|
) {
|
|
178
197
|
return T_CLS_NUMBER
|
|
179
198
|
} else {
|
|
@@ -193,8 +212,8 @@ export function tokenize(code) {
|
|
|
193
212
|
const prev = code[i - 1]
|
|
194
213
|
const next = code[i + 1]
|
|
195
214
|
const c_n = curr + next // current and next
|
|
196
|
-
const p_c = prev + curr // previous and current
|
|
197
|
-
const isJsxLiterals =
|
|
215
|
+
const p_c = prev + curr // previous and current
|
|
216
|
+
const isJsxLiterals = inJsxLiterals()
|
|
198
217
|
|
|
199
218
|
if (jsx.tag) {
|
|
200
219
|
const isOpenElementEnd = curr === '>'
|
|
@@ -207,13 +226,6 @@ export function tokenize(code) {
|
|
|
207
226
|
jsx.tag = true
|
|
208
227
|
jsx.child = false
|
|
209
228
|
}
|
|
210
|
-
|
|
211
|
-
if (jsx.child && curr === '{') {
|
|
212
|
-
jsx.expr = true
|
|
213
|
-
}
|
|
214
|
-
if (jsx.child && jsx.expr && curr === '}') {
|
|
215
|
-
jsx.expr = false
|
|
216
|
-
}
|
|
217
229
|
|
|
218
230
|
if (
|
|
219
231
|
!string.entered &&
|
|
@@ -276,7 +288,7 @@ export function tokenize(code) {
|
|
|
276
288
|
} else {
|
|
277
289
|
append()
|
|
278
290
|
current = curr
|
|
279
|
-
if (c_n === '</') {
|
|
291
|
+
if (c_n === '</' || c_n === '/>') {
|
|
280
292
|
current = c_n
|
|
281
293
|
append()
|
|
282
294
|
i++
|
|
@@ -284,6 +296,13 @@ export function tokenize(code) {
|
|
|
284
296
|
else if (jsxBrackets.has(curr)) append()
|
|
285
297
|
}
|
|
286
298
|
}
|
|
299
|
+
|
|
300
|
+
if (jsx.child && curr === '{') {
|
|
301
|
+
jsx.expr = true
|
|
302
|
+
}
|
|
303
|
+
if (jsx.child && jsx.expr && curr === '}') {
|
|
304
|
+
jsx.expr = false
|
|
305
|
+
}
|
|
287
306
|
}
|
|
288
307
|
|
|
289
308
|
append()
|
|
@@ -302,12 +321,17 @@ function generate(tokens) {
|
|
|
302
321
|
output.push(
|
|
303
322
|
type === T_BREAK
|
|
304
323
|
? '<br>'
|
|
305
|
-
: `<span class="sh__${type}">${encode(token)}</span>`
|
|
324
|
+
: `<span class="sh__${types[type]}">${encode(token)}</span>`
|
|
306
325
|
)
|
|
307
326
|
}
|
|
308
327
|
return output
|
|
309
328
|
}
|
|
310
329
|
|
|
330
|
+
/**
|
|
331
|
+
*
|
|
332
|
+
* @param {string} code
|
|
333
|
+
* @returns {string}
|
|
334
|
+
*/
|
|
311
335
|
export function highlight(code) {
|
|
312
336
|
const tokens = tokenize(code)
|
|
313
337
|
const output = generate(tokens).join('')
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sugar-high",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": "./lib/index.mjs",
|
|
6
6
|
"description": "Super lightweight JSX syntax highlighter",
|
|
7
7
|
"files": [
|
|
8
8
|
"lib",
|
|
9
|
+
"index.d.ts",
|
|
9
10
|
"*.md"
|
|
10
11
|
],
|
|
11
12
|
"license": "MIT",
|