sugar-high 0.0.6 → 0.1.0

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 CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  Super lightweight JSX syntax highlighter, around 1KB after minified and gzipped
5
5
 
6
- > ⚠️ Still in experiment! Use it in production with caution!
6
+ ![img](https://repository-images.githubusercontent.com/453236442/9416c941-9c40-4dbe-a73e-0e0d21993802)
7
7
 
8
8
  ### Usage
9
9
 
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export function highlight(code: string): string
2
+ export function tokenize(code: string): Array<[number, string]>
package/lib/index.mjs CHANGED
@@ -163,8 +163,11 @@ export function tokenize(code) {
163
163
  /** @type {{ tag: boolean; child: boolean; expr: boolean }} */
164
164
  const jsx = { tag: false, child: false, expr: false }
165
165
 
166
+ const inJsxLiterals = () => !jsx.tag && jsx.child && !jsx.expr
167
+
166
168
  function classify(token) {
167
- if (isCommentStart(token[0] + token[1])) {
169
+ const [chr0, chr1] = [token[0], token[1]]
170
+ if (isCommentStart(chr0 + chr1)) {
168
171
  return T_COMMENT
169
172
  } else if (keywords.has(token)) {
170
173
  return T_KEYWORD
@@ -173,19 +176,23 @@ export function tokenize(code) {
173
176
  } else if (
174
177
  (
175
178
  // is quoted string
176
- (isStringQuotation(token[0]) && !isStringQuotation(token[1])) ||
179
+ (isStringQuotation(chr0) && !isStringQuotation(chr1)) ||
177
180
  // is regex
178
- (!jsx.tag && isRegexStart(token[0] + token[1]) && token[token.length - 1] === '/')
181
+ (!jsx.tag && isRegexStart(chr0 + chr1) && token[token.length - 1] === '/')
179
182
  )
180
183
  ) {
181
184
  return T_STRING
182
185
  } else if (token === ' ') {
183
186
  return T_SPACE
184
- } else if (signs.has(token[0])) {
187
+ } else if (signs.has(chr0)) {
185
188
  return T_SIGN
186
189
  } else if (
187
- token[0] === token[0].toUpperCase() ||
188
- token === 'null'
190
+ !inJsxLiterals() &&
191
+ (
192
+ isIdentifierChar(chr0) &&
193
+ chr0 === chr0.toUpperCase() ||
194
+ token === 'null'
195
+ )
189
196
  ) {
190
197
  return T_CLS_NUMBER
191
198
  } else {
@@ -205,8 +212,8 @@ export function tokenize(code) {
205
212
  const prev = code[i - 1]
206
213
  const next = code[i + 1]
207
214
  const c_n = curr + next // current and next
208
- const p_c = prev + curr // previous and current
209
- const isJsxLiterals = jsx.child && !jsx.expr
215
+ const p_c = prev + curr // previous and current
216
+ const isJsxLiterals = inJsxLiterals()
210
217
 
211
218
  if (jsx.tag) {
212
219
  const isOpenElementEnd = curr === '>'
@@ -219,13 +226,6 @@ export function tokenize(code) {
219
226
  jsx.tag = true
220
227
  jsx.child = false
221
228
  }
222
-
223
- if (jsx.child && curr === '{') {
224
- jsx.expr = true
225
- }
226
- if (jsx.child && jsx.expr && curr === '}') {
227
- jsx.expr = false
228
- }
229
229
 
230
230
  if (
231
231
  !string.entered &&
@@ -288,7 +288,7 @@ export function tokenize(code) {
288
288
  } else {
289
289
  append()
290
290
  current = curr
291
- if (c_n === '</') {
291
+ if (c_n === '</' || c_n === '/>') {
292
292
  current = c_n
293
293
  append()
294
294
  i++
@@ -296,6 +296,13 @@ export function tokenize(code) {
296
296
  else if (jsxBrackets.has(curr)) append()
297
297
  }
298
298
  }
299
+
300
+ if (jsx.child && curr === '{') {
301
+ jsx.expr = true
302
+ }
303
+ if (jsx.child && jsx.expr && curr === '}') {
304
+ jsx.expr = false
305
+ }
299
306
  }
300
307
 
301
308
  append()
@@ -320,6 +327,11 @@ function generate(tokens) {
320
327
  return output
321
328
  }
322
329
 
330
+ /**
331
+ *
332
+ * @param {string} code
333
+ * @returns {string}
334
+ */
323
335
  export function highlight(code) {
324
336
  const tokens = tokenize(code)
325
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.6",
3
+ "version": "0.1.0",
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",