sugar-high 0.2.0 → 0.2.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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/lib/index.mjs +24 -12
  3. package/package.json +7 -5
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
- ![img](https://repository-images.githubusercontent.com/453236442/9416c941-9c40-4dbe-a73e-0e0d21993802)
6
+ ![img](https://repository-images.githubusercontent.com/453236442/82cf2807-78f0-4009-bbdf-d7e753f73cf4)
7
7
 
8
8
  ### Usage
9
9
 
package/lib/index.mjs CHANGED
@@ -126,12 +126,16 @@ function encode(str) {
126
126
  .replace(/[^\S\r\n]/g, ' ')
127
127
  }
128
128
 
129
+ function isWord(chr) {
130
+ return /^[\w_]+$/.test(chr)
131
+ }
132
+
129
133
  function isIdentifierChar(chr) {
130
- return /^[\w_$]$/.test(chr)
134
+ return /^[a-zA-Z_$]$/.test(chr)
131
135
  }
132
136
 
133
137
  function isIdentifier(str) {
134
- return /[a-zA-Z_$]/.test(str[0]) && (str.length === 1 || /^[\w_$]+$/.test(str.slice(1)))
138
+ return isIdentifierChar(str[0]) && (str.length === 1 || isWord(str.slice(1)))
135
139
  }
136
140
 
137
141
  function isStringQuotation(chr) {
@@ -179,7 +183,7 @@ export function tokenize(code) {
179
183
  } else if (
180
184
  !inJsxLiterals() &&
181
185
  (
182
- isIdentifierChar(chr0) &&
186
+ isWord(chr0) &&
183
187
  chr0 === chr0.toUpperCase() ||
184
188
  token === 'null'
185
189
  )
@@ -228,13 +232,13 @@ export function tokenize(code) {
228
232
  append()
229
233
  const [lastType, lastToken] = last
230
234
  // Special cases that are not considered as regex:
231
- // * (expr) / expr: `()[]` before `/` is still in expression
235
+ // * (expr1) / expr2: `()` before `/` operator is still in expression
232
236
  // * <non comment start>/ expr: non comment start before `/` is not regex
233
237
  if (
234
238
  isRegexChar &&
235
239
  lastType &&
236
240
  !(
237
- (lastType === T_SIGN && !'()[]'.includes(lastToken)) ||
241
+ (lastType === T_SIGN && !'()'.includes(lastToken)) ||
238
242
  lastType === T_COMMENT
239
243
  )
240
244
  ) {
@@ -243,33 +247,41 @@ export function tokenize(code) {
243
247
  continue
244
248
  }
245
249
  const start = i++
250
+ let foundClose = false
246
251
 
247
- const isInline = () => i < code.length && code[i] !== '\n'
252
+ // end of line of end of file
253
+ const isEol = () => i >= code.length || code[i] === '\n'
248
254
  // string quotation
249
255
  if (isQuotationChar) {
250
- for (; isInline(); i++) {
251
- if (isStringQuotation(code[i])) {
256
+ for (; !isEol(); i++) {
257
+ if (isStringQuotation(code[i]) && code[i] === code[start]) {
258
+ foundClose = true
252
259
  break
253
260
  }
254
261
  }
255
262
  } else {
256
263
  // regex
257
- for (; isInline(); i++) {
264
+ for (; !isEol(); i++) {
258
265
  if (code[i] === '/' && code[i - 1] !== '\\') {
266
+ foundClose = true
259
267
  // append regex flags
260
- while (start !== i && /^[a-z]$/.test(code[i + 1]) && isInline()) {
268
+ while (start !== i && /^[a-z]$/.test(code[i + 1]) && !isEol()) {
261
269
  i++
262
270
  };
263
271
  break
264
272
  }
265
273
  }
266
274
  }
267
- if (start !== i) {
275
+ if (start !== i && foundClose) {
276
+ // If current line is fully closed with string quotes or regex slashes,
277
+ // add them to tokens
268
278
  current = code.slice(start, i + 1)
269
279
  append(T_STRING)
270
280
  } else {
281
+ // If it doesn't match any of the above, just leave it as operator and move on
271
282
  current = curr
272
283
  append()
284
+ i = start
273
285
  }
274
286
  } else if (isCommentStart(c_n)) {
275
287
  const start = i
@@ -297,7 +309,7 @@ export function tokenize(code) {
297
309
  } else {
298
310
  if (
299
311
  (isJsxLiterals && !jsxBrackets.has(curr)) ||
300
- isIdentifierChar(curr) === isIdentifierChar(current[current.length - 1]) &&
312
+ isWord(curr) === isWord(current[current.length - 1]) &&
301
313
  !signs.has(curr)
302
314
  ) {
303
315
  current += curr
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sugar-high",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "exports": "./lib/index.mjs",
6
6
  "description": "Super lightweight JSX syntax highlighter",
@@ -12,11 +12,13 @@
12
12
  "license": "MIT",
13
13
  "scripts": {
14
14
  "test": "vitest run",
15
- "dev": "vite docs",
16
- "build": "vite build docs"
15
+ "dev": "next dev docs",
16
+ "build": "next build docs"
17
17
  },
18
18
  "devDependencies": {
19
- "vite": "^2.8.6",
20
- "vitest": "^0.5.9"
19
+ "next": "12.1.1-canary.15",
20
+ "react": "18.0.0-rc.2",
21
+ "react-dom": "18.0.0-rc.2",
22
+ "vitest": "~0.6.0"
21
23
  }
22
24
  }