sugar-high 0.3.0 → 0.3.1
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 +2 -2
- package/lib/index.mjs +32 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
Super lightweight JSX syntax highlighter, around 1KB after minified and gzipped
|
|
5
5
|
|
|
6
|
-

|
|
7
7
|
|
|
8
8
|
### Usage
|
|
9
9
|
|
|
@@ -54,7 +54,7 @@ Then make your own theme with customized colors by token type and put in global
|
|
|
54
54
|
color: #a19595;
|
|
55
55
|
}
|
|
56
56
|
.sh__jsxliterals {
|
|
57
|
-
color: #
|
|
57
|
+
color: #6266d1;
|
|
58
58
|
}
|
|
59
59
|
```
|
|
60
60
|
|
package/lib/index.mjs
CHANGED
|
@@ -129,6 +129,13 @@ function isWord(chr) {
|
|
|
129
129
|
return /^[\w_]+$/.test(chr)
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
+
function isCls(str) {
|
|
133
|
+
const chr0 = str[0]
|
|
134
|
+
return isWord(chr0) &&
|
|
135
|
+
chr0 === chr0.toUpperCase() ||
|
|
136
|
+
str === 'null'
|
|
137
|
+
}
|
|
138
|
+
|
|
132
139
|
function isIdentifierChar(chr) {
|
|
133
140
|
return /^[a-zA-Z_$]$/.test(chr)
|
|
134
141
|
}
|
|
@@ -161,16 +168,22 @@ export function tokenize(code) {
|
|
|
161
168
|
/** @type {Array<[number, string]>} */
|
|
162
169
|
const tokens = []
|
|
163
170
|
|
|
164
|
-
// jsx.tag for entering open or closed
|
|
171
|
+
// jsx.tag for entering open or closed tags
|
|
172
|
+
// jsx.close for is the closing sign of open or close tags
|
|
165
173
|
// jsx.child for entering children
|
|
166
174
|
// jsx.expr for entering {expression}
|
|
167
|
-
/** @type {{ tag: boolean; child: boolean; expr: boolean }} */
|
|
168
|
-
const jsx = { tag: false, child: false, expr: false }
|
|
175
|
+
/** @type {{ tag: boolean; close: boolean; child: boolean; expr: boolean }} */
|
|
176
|
+
const jsx = { tag: false, close: false, child: false, expr: false }
|
|
169
177
|
|
|
170
|
-
const
|
|
178
|
+
const inJsxTag = () => jsx.tag || jsx.close
|
|
179
|
+
const inJsxLiterals = () => !inJsxTag() && jsx.child && !jsx.expr
|
|
171
180
|
|
|
181
|
+
/**
|
|
182
|
+
*
|
|
183
|
+
* @param {string} token
|
|
184
|
+
* @returns {number}
|
|
185
|
+
*/
|
|
172
186
|
function classify(token) {
|
|
173
|
-
const chr0 = token[0]
|
|
174
187
|
if (keywords.has(token)) {
|
|
175
188
|
return T_KEYWORD
|
|
176
189
|
} else if (token === '\n') {
|
|
@@ -179,17 +192,12 @@ export function tokenize(code) {
|
|
|
179
192
|
return T_SPACE
|
|
180
193
|
} else if (token.split('').every(ch => signs.has(ch))) {
|
|
181
194
|
return T_SIGN
|
|
182
|
-
} else if (
|
|
183
|
-
|
|
184
|
-
(
|
|
185
|
-
isWord(chr0) &&
|
|
186
|
-
chr0 === chr0.toUpperCase() ||
|
|
187
|
-
token === 'null'
|
|
188
|
-
)
|
|
189
|
-
) {
|
|
190
|
-
return T_CLS_NUMBER
|
|
195
|
+
} else if (isCls(token)) {
|
|
196
|
+
return inJsxLiterals() ? T_JSX_LITERALS : inJsxTag() ? T_IDENTIFIER : T_CLS_NUMBER
|
|
191
197
|
} else {
|
|
192
|
-
return
|
|
198
|
+
return inJsxLiterals()
|
|
199
|
+
? T_JSX_LITERALS
|
|
200
|
+
: isIdentifier(token) ? T_IDENTIFIER : T_STRING
|
|
193
201
|
}
|
|
194
202
|
}
|
|
195
203
|
|
|
@@ -209,15 +217,19 @@ export function tokenize(code) {
|
|
|
209
217
|
const curr = code[i]
|
|
210
218
|
const prev = code[i - 1]
|
|
211
219
|
const next = code[i + 1]
|
|
212
|
-
const c_n = curr + next // current and next
|
|
213
220
|
const p_c = prev + curr // previous and current
|
|
221
|
+
const c_n = curr + next // current and next
|
|
214
222
|
const isJsxLiterals = inJsxLiterals()
|
|
215
223
|
|
|
224
|
+
if (jsx.close && (last[1] === '>' || last[1] === '/>')) {
|
|
225
|
+
jsx.close = false
|
|
226
|
+
}
|
|
216
227
|
if (jsx.tag) {
|
|
217
228
|
const isOpenElementEnd = curr === '>'
|
|
218
229
|
const isCloseElementEnd = p_c === '/>'
|
|
219
|
-
jsx.child = !isCloseElementEnd
|
|
230
|
+
jsx.child = !isCloseElementEnd
|
|
220
231
|
jsx.tag = !(isOpenElementEnd || isCloseElementEnd)
|
|
232
|
+
jsx.close = isOpenElementEnd || isCloseElementEnd
|
|
221
233
|
}
|
|
222
234
|
// if it's not in a jsx tag declaration or a string, close child if next is jsx close tag
|
|
223
235
|
if (!jsx.tag && (curr === '<' && isIdentifierChar(next) || c_n === '</')) {
|
|
@@ -317,6 +329,9 @@ export function tokenize(code) {
|
|
|
317
329
|
!signs.has(curr)
|
|
318
330
|
) {
|
|
319
331
|
current += curr
|
|
332
|
+
if (next === '<') {
|
|
333
|
+
append();
|
|
334
|
+
}
|
|
320
335
|
} else {
|
|
321
336
|
append()
|
|
322
337
|
current = curr
|