sugar-high 1.0.0 → 1.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
@@ -4,7 +4,7 @@
4
4
 
5
5
  Super lightweight syntax highlighter for JavaScript and JSX—about **1 kB** minified and gzipped. Works in the browser or any JS runtime that can set HTML strings.
6
6
 
7
- ![Sugar High preview](https://repository-images.githubusercontent.com/453236442/e9c853e9-4af6-45c4-aa4a-c27afbf16a0d)
7
+ ![Sugar High preview](https://repository-images.githubusercontent.com/453236442/9aa2144a-3a4c-4a93-a87f-92ca6a37ded6)
8
8
 
9
9
  ## Install
10
10
 
package/lib/index.d.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  type HighlightOptions = {
2
2
  keywords?: Set<string>
3
+ /**
4
+ * Highlighted as the `class` token type (e.g. built-in types). Checked before `keywords`.
5
+ */
6
+ typeKeywords?: Set<string>
3
7
  onCommentStart?: (curr: string, next: string) => number | boolean
4
8
  onCommentEnd?: (curr: string, prev: string) => number | boolean
5
9
  /**
package/lib/index.js CHANGED
@@ -69,6 +69,13 @@ const Keywords_Ts = new Set([
69
69
  'unknown',
70
70
  'never',
71
71
  'any',
72
+ // Built-in type names (annotations / type positions)
73
+ 'number',
74
+ 'string',
75
+ 'boolean',
76
+ 'bigint',
77
+ 'symbol',
78
+ 'object',
72
79
  ])
73
80
 
74
81
  const Signs = new Set([
@@ -298,6 +305,7 @@ function isRegexStart(str) {
298
305
  * @param {string} code
299
306
  * @param {{
300
307
  * keywords?: Set<string>
308
+ * typeKeywords?: Set<string>
301
309
  * onCommentStart?: (curr: string, next: string) => number | boolean
302
310
  * onCommentEnd?: (prev: string, curr: string) => number | boolean
303
311
  * onQuote?: (curr: string, i: number, code: string) => number | null | undefined
@@ -319,6 +327,9 @@ function tokenize(code, options) {
319
327
  onCommentEnd,
320
328
  } = mergedOptions
321
329
 
330
+ const resolvedTypeKeywords =
331
+ mergedOptions.typeKeywords instanceof Set ? mergedOptions.typeKeywords : null
332
+
322
333
  let current = ''
323
334
  let type = -1
324
335
  /** @type {[number, string]} */
@@ -388,6 +399,8 @@ function tokenize(code, options) {
388
399
  // Determine strings first before other types
389
400
  if (inStringQuotes() || inStrTemplateLiterals()) {
390
401
  return T_STRING
402
+ } else if (resolvedTypeKeywords && resolvedTypeKeywords.has(token)) {
403
+ return last[1] === '.' ? T_IDENTIFIER : T_CLS_NUMBER
391
404
  } else if (resolvedKeywords.has(token)) {
392
405
  return last[1] === '.' ? T_IDENTIFIER : T_KEYWORD
393
406
  } else if (isLineBreak) {
@@ -928,6 +941,7 @@ function toHtml(lines) {
928
941
  * @param {string} code
929
942
  * @param {{
930
943
  * keywords?: Set<string>
944
+ * typeKeywords?: Set<string>
931
945
  * onCommentStart?: (curr: string, next: string) => number | boolean
932
946
  * onCommentEnd?: (curr: string, prev: string) => number | boolean
933
947
  * onQuote?: (curr: string, i: number, code: string) => number | null | undefined
@@ -1,5 +1,6 @@
1
1
  type LanguageConfig = {
2
2
  keywords: Set<string>
3
+ typeKeywords?: Set<string>
3
4
  onCommentStart?(curr: string, next: string): 0 | 1 | 2
4
5
  onCommentEnd?(prev: string, curr: string): 0 | 1 | 2
5
6
  onQuote?(curr: string, i: number, code: string): number | null | undefined
@@ -8,3 +9,6 @@ type LanguageConfig = {
8
9
  export const css: LanguageConfig
9
10
  export const rust: LanguageConfig
10
11
  export const python: LanguageConfig
12
+ export const c: LanguageConfig
13
+ export const go: LanguageConfig
14
+ export const java: LanguageConfig
@@ -1,3 +1,6 @@
1
1
  export * as css from './lang/css.js'
2
2
  export * as rust from './lang/rust.js'
3
3
  export * as python from './lang/python.js'
4
+ export * as c from './lang/c.js'
5
+ export * as go from './lang/go.js'
6
+ export * as java from './lang/java.js'
@@ -0,0 +1,17 @@
1
+ // @ts-check
2
+ import { onCommentStart, onCommentEnd } from './clike-base.js'
3
+
4
+ export const typeKeywords = new Set([
5
+ 'void', 'char', 'short', 'int', 'long', 'float', 'double', 'signed', 'unsigned',
6
+ '_Bool', '_Complex', '_Imaginary',
7
+ ])
8
+
9
+ export const keywords = new Set([
10
+ 'auto', 'break', 'case', 'const', 'continue', 'default', 'do', 'else', 'enum',
11
+ 'extern', 'for', 'goto', 'if', 'inline', 'register', 'restrict', 'return',
12
+ 'sizeof', 'static', 'struct', 'switch', 'typedef', 'union', 'volatile', 'while',
13
+ '_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn', '_Static_assert',
14
+ '_Thread_local',
15
+ ])
16
+
17
+ export { onCommentStart, onCommentEnd }
@@ -0,0 +1,12 @@
1
+ // @ts-check
2
+ export const onCommentStart = (currentChar, nextChar) => {
3
+ const pair = currentChar + nextChar
4
+ if (pair === '//') return 1
5
+ if (pair === '/*') return 1
6
+ return 0
7
+ }
8
+
9
+ export const onCommentEnd = (prevChar, currChar) => {
10
+ if (currChar === '\n') return 1
11
+ return prevChar + currChar === '*/' ? 1 : 0
12
+ }
@@ -0,0 +1,16 @@
1
+ // @ts-check
2
+ import { onCommentStart, onCommentEnd } from './clike-base.js'
3
+
4
+ export const typeKeywords = new Set([
5
+ 'bool', 'byte', 'complex64', 'complex128', 'error', 'float32', 'float64', 'int',
6
+ 'int8', 'int16', 'int32', 'int64', 'rune', 'string', 'uint', 'uint8', 'uint16',
7
+ 'uint32', 'uint64', 'uintptr',
8
+ ])
9
+
10
+ export const keywords = new Set([
11
+ 'break', 'case', 'chan', 'const', 'continue', 'default', 'defer', 'else',
12
+ 'fallthrough', 'for', 'func', 'go', 'goto', 'if', 'import', 'interface', 'map',
13
+ 'package', 'range', 'return', 'select', 'struct', 'switch', 'type', 'var',
14
+ ])
15
+
16
+ export { onCommentStart, onCommentEnd }
@@ -0,0 +1,17 @@
1
+ // @ts-check
2
+ import { onCommentStart, onCommentEnd } from './clike-base.js'
3
+
4
+ export const typeKeywords = new Set([
5
+ 'boolean', 'byte', 'char', 'double', 'float', 'int', 'long', 'short', 'void',
6
+ ])
7
+
8
+ export const keywords = new Set([
9
+ 'abstract', 'assert', 'break', 'case', 'catch', 'class', 'const', 'continue',
10
+ 'default', 'do', 'else', 'enum', 'extends', 'final', 'finally', 'for', 'goto',
11
+ 'if', 'implements', 'import', 'instanceof', 'interface', 'native', 'new',
12
+ 'package', 'private', 'protected', 'public', 'return', 'static', 'strictfp',
13
+ 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient',
14
+ 'try', 'volatile', 'while',
15
+ ])
16
+
17
+ export { onCommentStart, onCommentEnd }
package/package.json CHANGED
@@ -1,6 +1,11 @@
1
1
  {
2
2
  "name": "sugar-high",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "git+https://github.com/huozhi/sugar-high.git",
7
+ "directory": "packages/sugar-high"
8
+ },
4
9
  "type": "module",
5
10
  "types": "./lib/index.d.ts",
6
11
  "main": "./lib/index.js",
@@ -25,7 +30,7 @@
25
30
  },
26
31
  "devDependencies": {
27
32
  "@types/node": "22.12.0",
28
- "typescript": "5.7.3",
33
+ "typescript": "6.0.2",
29
34
  "vitest": "^3.0.2"
30
35
  }
31
36
  }