sugar-high 0.8.1 → 0.8.3

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 +6 -0
  2. package/lib/index.js +33 -13
  3. package/package.json +9 -9
package/README.md CHANGED
@@ -96,6 +96,12 @@ You can use `.sh__token--<token type>` to customize the output node of each toke
96
96
  }
97
97
  ```
98
98
 
99
+ ### Use With Remark.js
100
+
101
+ [Remark.js](https://remark.js.org/) is a powerful markdown processor, you can use the [sugar-high remark plugin](https://remark-sugar-high.vercel.app/) with remark.js to highlight code blocks in markdown.
102
+
103
+ Check out the [documentation](https://remark-sugar-high.vercel.app/) for more details.
104
+
99
105
  ### LICENSE
100
106
 
101
107
  MIT
package/lib/index.js CHANGED
@@ -180,13 +180,18 @@ function isStringQuotation(chr) {
180
180
  return isSingleQuotes(chr) || isStrTemplateChr(chr)
181
181
  }
182
182
 
183
+ /** @returns {0|1|2} */
183
184
  function isCommentStart_Js(curr, next) {
184
185
  const str = curr + next
185
- return str === '//' || str === '/*'
186
+ if (str === '/*') return 2
187
+ return str === '//' ? 1 : 0
186
188
  }
187
189
 
190
+ /** @returns {0|1|2} */
188
191
  function isCommentEnd_Js(prev, curr) {
189
- return curr === '\n' || (prev + curr) === '*/'
192
+ return (prev + curr) === '*/'
193
+ ? 2
194
+ : curr === '\n' ? 1 : 0
190
195
  }
191
196
 
192
197
  function isRegexStart(str) {
@@ -470,6 +475,9 @@ function tokenize(code, options) {
470
475
  if (isIdentifier(prop)) {
471
476
  current = prop
472
477
  append(T_PROPERTY)
478
+ } else if (prop === ' ') {
479
+ current = prop
480
+ append(T_SPACE)
473
481
  }
474
482
  continue
475
483
  }
@@ -555,13 +563,16 @@ function tokenize(code, options) {
555
563
  } else if (onCommentStart(curr, next)) {
556
564
  append()
557
565
  const start = i
558
- const commentType = onCommentStart(curr, next)
566
+ const startCommentType = onCommentStart(curr, next)
559
567
 
560
568
  // just match the comment, commentType === true
561
569
  // inline comment, commentType === 1
562
570
  // block comment, commentType === 2
563
- if (commentType) {
564
- for (; i < code.length && !onCommentEnd(code[i - 1], code[i]); i++);
571
+ if (startCommentType) {
572
+ for (; i < code.length; i++) {
573
+ const endCommentType = onCommentEnd(code[i - 1], code[i])
574
+ if (endCommentType == startCommentType) break
575
+ }
565
576
  }
566
577
  current = code.slice(start, i + 1)
567
578
  append(T_COMMENT)
@@ -622,7 +633,7 @@ function tokenize(code, options) {
622
633
 
623
634
  /**
624
635
  * @param {Array<[number, string]>} tokens
625
- * @return {Array<any>}
636
+ * @return {Array<{type: string, tagName: string, children: any[], properties: Record<string, string>}>}
626
637
  */
627
638
  function generate(tokens) {
628
639
  const lines = []
@@ -653,12 +664,12 @@ function generate(tokens) {
653
664
  type: 'element',
654
665
  tagName: 'span',
655
666
  children: [{
656
- type: 'text',
667
+ type, // token type
657
668
  value: value, // to encode
658
669
  }],
659
670
  properties: {
660
671
  className: `sh__token--${TokenTypes[type]}`,
661
- style: `color: var(--sh-${TokenTypes[type]})`,
672
+ style: { color: `var(--sh-${TokenTypes[type]})` },
662
673
  },
663
674
  }
664
675
  ))
@@ -697,6 +708,19 @@ function generate(tokens) {
697
708
  return lines
698
709
  }
699
710
 
711
+ /** @param { className: string, style?: Record<string, string> } */
712
+ const propsToString = (props) => {
713
+ let str = `class="${props.className}"`
714
+
715
+ if (props.style) {
716
+ const style = Object.entries(props.style)
717
+ .map(([key, value]) => `${key}:${value}`)
718
+ .join(';')
719
+ str += ` style="${style}"`
720
+ }
721
+ return str
722
+ }
723
+
700
724
  function toHtml(lines) {
701
725
  return lines
702
726
  .map(line => {
@@ -704,11 +728,7 @@ function toHtml(lines) {
704
728
  const tokens = line.children
705
729
  .map(child => {
706
730
  const { tagName, children, properties } = child
707
- return `<${tagName} class="${
708
- properties.className
709
- }" style="${
710
- properties.style
711
- }">${encode(children[0].value)}</${tagName}>`
731
+ return `<${tagName} ${propsToString(properties)}>${encode(children[0].value)}</${tagName}>`
712
732
  })
713
733
  .join('')
714
734
  return `<${lineTag} class="${line.properties.className}">${tokens}</${lineTag}>`
package/package.json CHANGED
@@ -1,29 +1,29 @@
1
1
  {
2
2
  "name": "sugar-high",
3
- "version": "0.8.1",
3
+ "version": "0.8.3",
4
4
  "type": "module",
5
5
  "types": "./lib/index.d.ts",
6
- "exports": {
7
- "types": "./lib/index.d.ts",
8
- "default": "./lib/index.js"
9
- },
6
+ "main": "./lib/index.js",
10
7
  "description": "Super lightweight JSX syntax highlighter",
11
8
  "files": [
12
9
  "lib"
13
10
  ],
14
11
  "license": "MIT",
15
12
  "scripts": {
16
- "test": "vitest run",
13
+ "test": "vitest",
17
14
  "dev": "next dev docs",
18
15
  "build": "next build docs"
19
16
  },
20
17
  "devDependencies": {
21
- "codice": "^0.2.0",
22
- "next": "15.1.3",
18
+ "@types/node": "22.10.7",
19
+ "@types/react": "19.0.7",
20
+ "codice": "^0.5.0",
21
+ "next": "15.1.5",
23
22
  "react": "^19.0.0",
24
23
  "react-dom": "^19.0.0",
25
24
  "sugar-high": "link:./",
26
- "vitest": "^2.1.3"
25
+ "typescript": "5.7.3",
26
+ "vitest": "^3.0.2"
27
27
  },
28
28
  "packageManager": "pnpm@8.7.1"
29
29
  }