sugar-high 0.0.8 → 0.1.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/README.md +1 -1
- package/index.d.ts +2 -0
- package/lib/index.mjs +61 -47
- package/package.json +2 -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
|
-
|
|
6
|
+

|
|
7
7
|
|
|
8
8
|
### Usage
|
|
9
9
|
|
package/index.d.ts
ADDED
package/lib/index.mjs
CHANGED
|
@@ -147,15 +147,9 @@ function isRegexStart(str) {
|
|
|
147
147
|
export function tokenize(code) {
|
|
148
148
|
let current = ''
|
|
149
149
|
let type = -1
|
|
150
|
+
let last = [null, null]
|
|
150
151
|
/** @type {Array<[number, string]>} */
|
|
151
152
|
const tokens = []
|
|
152
|
-
// string.type = 0 for string or string template
|
|
153
|
-
// string.type = 1 for regex
|
|
154
|
-
const string = { entered: false, type: 0 }
|
|
155
|
-
|
|
156
|
-
// comment.type = 0 for single line comments
|
|
157
|
-
// comment.type = 1 for multi-line comments
|
|
158
|
-
const comment = { entered: false, type: 0 }
|
|
159
153
|
|
|
160
154
|
// jsx.tag for entering open or closed tag
|
|
161
155
|
// jsx.child for entering children
|
|
@@ -173,20 +167,20 @@ export function tokenize(code) {
|
|
|
173
167
|
return T_KEYWORD
|
|
174
168
|
} else if (token === '\n') {
|
|
175
169
|
return T_BREAK
|
|
176
|
-
} else if (
|
|
177
|
-
(
|
|
178
|
-
// is quoted string
|
|
179
|
-
(isStringQuotation(chr0) && !isStringQuotation(chr1)) ||
|
|
180
|
-
// is regex
|
|
181
|
-
(!jsx.tag && isRegexStart(chr0 + chr1) && token[token.length - 1] === '/')
|
|
182
|
-
)
|
|
183
|
-
) {
|
|
184
|
-
return T_STRING
|
|
185
170
|
} else if (token === ' ') {
|
|
186
171
|
return T_SPACE
|
|
187
|
-
} else if (signs.has(
|
|
172
|
+
} else if (token.split('').every(ch => signs.has(ch))) {
|
|
188
173
|
return T_SIGN
|
|
189
174
|
} else if (
|
|
175
|
+
(
|
|
176
|
+
// is quoted string
|
|
177
|
+
(isStringQuotation(chr0) && !isStringQuotation(chr1)) ||
|
|
178
|
+
// is regex
|
|
179
|
+
(!jsx.tag && isRegexStart(chr0 + chr1) && token[token.length - 1] === '/')
|
|
180
|
+
)
|
|
181
|
+
) {
|
|
182
|
+
return T_STRING
|
|
183
|
+
} else if (
|
|
190
184
|
!inJsxLiterals() &&
|
|
191
185
|
(
|
|
192
186
|
isIdentifierChar(chr0) &&
|
|
@@ -203,7 +197,12 @@ export function tokenize(code) {
|
|
|
203
197
|
const append = () => {
|
|
204
198
|
if (current) {
|
|
205
199
|
type = classify(current)
|
|
206
|
-
|
|
200
|
+
/** @type [number, string] */
|
|
201
|
+
const pair = [type, current]
|
|
202
|
+
if (type !== T_SPACE && type !== T_BREAK) {
|
|
203
|
+
last = pair
|
|
204
|
+
}
|
|
205
|
+
tokens.push(pair)
|
|
207
206
|
}
|
|
208
207
|
current = ''
|
|
209
208
|
}
|
|
@@ -222,48 +221,58 @@ export function tokenize(code) {
|
|
|
222
221
|
jsx.tag = !(isOpenElementEnd || isCloseElementEnd)
|
|
223
222
|
}
|
|
224
223
|
// if it's not in a jsx tag declaration or a string, close child if next is jsx close tag
|
|
225
|
-
if (!jsx.tag &&
|
|
224
|
+
if (!jsx.tag && (curr === '<' && isIdentifierChar(next) || c_n === '</')) {
|
|
226
225
|
jsx.tag = true
|
|
227
226
|
jsx.child = false
|
|
228
227
|
}
|
|
229
228
|
|
|
230
229
|
if (
|
|
231
|
-
!string.entered &&
|
|
232
230
|
(isStringQuotation(curr) || !jsx.tag && isRegexStart(c_n))
|
|
233
231
|
) {
|
|
234
|
-
string.entered = true
|
|
235
|
-
string.type = isStringQuotation(curr) ? 0 : 1
|
|
236
232
|
append()
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
if (string.type === 0 && isStringQuotation(curr)) {
|
|
241
|
-
string.entered = false
|
|
242
|
-
append()
|
|
243
|
-
} else if (string.type === 1 && prev !== '\\' && curr === '/') {
|
|
244
|
-
string.entered = false
|
|
233
|
+
const [lastType, lastToken] = last
|
|
234
|
+
if (!((lastType === T_SIGN && !'()[]'.includes(lastToken)) || lastType === T_COMMENT)) {
|
|
235
|
+
current = curr
|
|
245
236
|
append()
|
|
237
|
+
continue
|
|
246
238
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
239
|
+
let j = i + 1
|
|
240
|
+
let strEnd = i
|
|
241
|
+
const inString = () => j < code.length && code[j] !== '\n'
|
|
242
|
+
// string quotation
|
|
243
|
+
if (isStringQuotation(curr)) {
|
|
244
|
+
for (; inString(); j++) {
|
|
245
|
+
if (isStringQuotation(code[j])) {
|
|
246
|
+
strEnd = j
|
|
247
|
+
break
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
} else {
|
|
251
|
+
// regex
|
|
252
|
+
for (; inString(); j++) {
|
|
253
|
+
if (code[j] === '/' && code[j - 1] !== '\\') {
|
|
254
|
+
strEnd = j
|
|
255
|
+
break
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (strEnd !== i) {
|
|
260
|
+
current = code.slice(i, strEnd + 1)
|
|
260
261
|
append()
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
current
|
|
262
|
+
i = j
|
|
263
|
+
} else {
|
|
264
|
+
current = curr
|
|
264
265
|
append()
|
|
265
|
-
i++
|
|
266
266
|
}
|
|
267
|
+
} else if (isCommentStart(c_n)) {
|
|
268
|
+
const start = i
|
|
269
|
+
if (next === '/') {
|
|
270
|
+
for (; i < code.length && code[i] !== '\n'; i++);
|
|
271
|
+
} else {
|
|
272
|
+
for (; i < code.length && code[i - 1] + code[i] !== '*/'; i++);
|
|
273
|
+
}
|
|
274
|
+
current = code.slice(start, i + 1)
|
|
275
|
+
append()
|
|
267
276
|
} else if (curr === ' ' || curr === '\n') {
|
|
268
277
|
if (
|
|
269
278
|
curr === ' ' &&
|
|
@@ -327,6 +336,11 @@ function generate(tokens) {
|
|
|
327
336
|
return output
|
|
328
337
|
}
|
|
329
338
|
|
|
339
|
+
/**
|
|
340
|
+
*
|
|
341
|
+
* @param {string} code
|
|
342
|
+
* @returns {string}
|
|
343
|
+
*/
|
|
330
344
|
export function highlight(code) {
|
|
331
345
|
const tokens = tokenize(code)
|
|
332
346
|
const output = generate(tokens).join('')
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sugar-high",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.2",
|
|
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",
|