sugar-high 0.2.1 → 0.2.2
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/index.mjs +26 -20
- package/package.json +4 -4
package/lib/index.mjs
CHANGED
|
@@ -71,6 +71,7 @@ const signs = new Set([
|
|
|
71
71
|
'[',
|
|
72
72
|
']',
|
|
73
73
|
'#',
|
|
74
|
+
'@',
|
|
74
75
|
'\\',
|
|
75
76
|
])
|
|
76
77
|
|
|
@@ -87,7 +88,7 @@ export const types = [
|
|
|
87
88
|
]
|
|
88
89
|
|
|
89
90
|
/**
|
|
90
|
-
*
|
|
91
|
+
*
|
|
91
92
|
* 0 - identifier
|
|
92
93
|
* 1 - keyword
|
|
93
94
|
* 2 - string
|
|
@@ -97,7 +98,7 @@ export const types = [
|
|
|
97
98
|
* 6 - break
|
|
98
99
|
* 7 - space
|
|
99
100
|
* 8 - jsx literals
|
|
100
|
-
*
|
|
101
|
+
*
|
|
101
102
|
*/
|
|
102
103
|
const [
|
|
103
104
|
T_IDENTIFIER,
|
|
@@ -151,8 +152,8 @@ function isRegexStart(str) {
|
|
|
151
152
|
return str[0] === '/' && !isCommentStart(str[0] + str[1])
|
|
152
153
|
}
|
|
153
154
|
|
|
154
|
-
/**
|
|
155
|
-
* @param {string} code
|
|
155
|
+
/**
|
|
156
|
+
* @param {string} code
|
|
156
157
|
* @return {Array<[number, string]>}
|
|
157
158
|
*/
|
|
158
159
|
export function tokenize(code) {
|
|
@@ -181,7 +182,7 @@ export function tokenize(code) {
|
|
|
181
182
|
} else if (token.split('').every(ch => signs.has(ch))) {
|
|
182
183
|
return T_SIGN
|
|
183
184
|
} else if (
|
|
184
|
-
!inJsxLiterals() &&
|
|
185
|
+
!inJsxLiterals() &&
|
|
185
186
|
(
|
|
186
187
|
isWord(chr0) &&
|
|
187
188
|
chr0 === chr0.toUpperCase() ||
|
|
@@ -211,11 +212,11 @@ export function tokenize(code) {
|
|
|
211
212
|
const prev = code[i - 1]
|
|
212
213
|
const next = code[i + 1]
|
|
213
214
|
const c_n = curr + next // current and next
|
|
214
|
-
const p_c = prev + curr // previous and current
|
|
215
|
+
const p_c = prev + curr // previous and current
|
|
215
216
|
const isJsxLiterals = inJsxLiterals()
|
|
216
217
|
|
|
217
218
|
if (jsx.tag) {
|
|
218
|
-
const isOpenElementEnd = curr === '>'
|
|
219
|
+
const isOpenElementEnd = curr === '>'
|
|
219
220
|
const isCloseElementEnd = p_c === '/>'
|
|
220
221
|
jsx.child = !isCloseElementEnd && !isCloseElementEnd
|
|
221
222
|
jsx.tag = !(isOpenElementEnd || isCloseElementEnd)
|
|
@@ -235,10 +236,10 @@ export function tokenize(code) {
|
|
|
235
236
|
// * (expr1) / expr2: `()` before `/` operator is still in expression
|
|
236
237
|
// * <non comment start>/ expr: non comment start before `/` is not regex
|
|
237
238
|
if (
|
|
238
|
-
isRegexChar &&
|
|
239
|
-
lastType &&
|
|
239
|
+
isRegexChar &&
|
|
240
|
+
lastType &&
|
|
240
241
|
!(
|
|
241
|
-
(lastType === T_SIGN && !'()'.includes(lastToken)) ||
|
|
242
|
+
(lastType === T_SIGN && !'()'.includes(lastToken)) ||
|
|
242
243
|
lastType === T_COMMENT
|
|
243
244
|
)
|
|
244
245
|
) {
|
|
@@ -247,14 +248,18 @@ export function tokenize(code) {
|
|
|
247
248
|
continue
|
|
248
249
|
}
|
|
249
250
|
const start = i++
|
|
251
|
+
const leftQuote = code[start]
|
|
250
252
|
let foundClose = false
|
|
251
|
-
|
|
253
|
+
|
|
252
254
|
// end of line of end of file
|
|
253
|
-
const
|
|
255
|
+
const isEof = () => i >= code.length
|
|
256
|
+
const isEol = () => isEof() || code[i] === '\n'
|
|
254
257
|
// string quotation
|
|
255
258
|
if (isQuotationChar) {
|
|
256
|
-
for (; !
|
|
257
|
-
|
|
259
|
+
for (; !isEof(); i++) {
|
|
260
|
+
// handle single quotes
|
|
261
|
+
if ((leftQuote === "'" || leftQuote === '"') && code[i] === '\n') break
|
|
262
|
+
if (isStringQuotation(code[i]) && code[i] === leftQuote) {
|
|
258
263
|
foundClose = true
|
|
259
264
|
break
|
|
260
265
|
}
|
|
@@ -267,7 +272,7 @@ export function tokenize(code) {
|
|
|
267
272
|
// append regex flags
|
|
268
273
|
while (start !== i && /^[a-z]$/.test(code[i + 1]) && !isEol()) {
|
|
269
274
|
i++
|
|
270
|
-
}
|
|
275
|
+
}
|
|
271
276
|
break
|
|
272
277
|
}
|
|
273
278
|
}
|
|
@@ -284,6 +289,7 @@ export function tokenize(code) {
|
|
|
284
289
|
i = start
|
|
285
290
|
}
|
|
286
291
|
} else if (isCommentStart(c_n)) {
|
|
292
|
+
append()
|
|
287
293
|
const start = i
|
|
288
294
|
if (next === '/') {
|
|
289
295
|
for (; i < code.length && code[i] !== '\n'; i++);
|
|
@@ -338,8 +344,8 @@ export function tokenize(code) {
|
|
|
338
344
|
return tokens
|
|
339
345
|
}
|
|
340
346
|
|
|
341
|
-
/**
|
|
342
|
-
* @param {Array<[number, string]>} tokens
|
|
347
|
+
/**
|
|
348
|
+
* @param {Array<[number, string]>} tokens
|
|
343
349
|
* @return {Array<string>}
|
|
344
350
|
*/
|
|
345
351
|
function generate(tokens) {
|
|
@@ -347,7 +353,7 @@ function generate(tokens) {
|
|
|
347
353
|
for (let i = 0; i < tokens.length; i++) {
|
|
348
354
|
const [type, token] = tokens[i]
|
|
349
355
|
output.push(
|
|
350
|
-
type === T_BREAK
|
|
356
|
+
type === T_BREAK
|
|
351
357
|
? '<br>'
|
|
352
358
|
: `<span class="sh__${types[type]}">${encode(token)}</span>`
|
|
353
359
|
)
|
|
@@ -356,8 +362,8 @@ function generate(tokens) {
|
|
|
356
362
|
}
|
|
357
363
|
|
|
358
364
|
/**
|
|
359
|
-
*
|
|
360
|
-
* @param {string} code
|
|
365
|
+
*
|
|
366
|
+
* @param {string} code
|
|
361
367
|
* @returns {string}
|
|
362
368
|
*/
|
|
363
369
|
export function highlight(code) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sugar-high",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": "./lib/index.mjs",
|
|
6
6
|
"description": "Super lightweight JSX syntax highlighter",
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
"build": "next build docs"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"next": "12.1.
|
|
20
|
-
"react": "18.0.0
|
|
21
|
-
"react-dom": "18.0.0
|
|
19
|
+
"next": "12.1.4",
|
|
20
|
+
"react": "18.0.0",
|
|
21
|
+
"react-dom": "18.0.0",
|
|
22
22
|
"vitest": "~0.6.0"
|
|
23
23
|
}
|
|
24
24
|
}
|