sugar-high 0.9.3 → 0.9.5

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 +2 -2
  2. package/lib/index.js +35 -9
  3. package/package.json +8 -19
package/README.md CHANGED
@@ -97,9 +97,9 @@ You can use `.sh__token--<token type>` to customize the output node of each toke
97
97
 
98
98
  ### Use With Remark.js
99
99
 
100
- [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.
100
+ [Remark.js](https://remark.js.org/) is a powerful markdown processor, you can use the [sugar-high remark plugin](https://sugar-high.vercel.app/remark) with remark.js to highlight code blocks in markdown.
101
101
 
102
- Check out the [documentation](https://remark-sugar-high.vercel.app/) for more details.
102
+ Check out the [readme](https://github.com/huozhi/sugar-high/tree/main/packages/remark-sugar-high) for more details.
103
103
 
104
104
  ### LICENSE
105
105
 
package/lib/index.js CHANGED
@@ -275,7 +275,7 @@ function tokenize(code, options) {
275
275
  if (isJsxLiterals) return T_JSX_LITERALS
276
276
 
277
277
  // Determine strings first before other types
278
- if (inStringQuotes()) {
278
+ if (inStringQuotes() || inStrTemplateLiterals()) {
279
279
  return T_STRING
280
280
  } else if (keywords.has(token)) {
281
281
  return last[1] === '.' ? T_IDENTIFIER : T_KEYWORD
@@ -326,9 +326,9 @@ function tokenize(code, options) {
326
326
  const p_c = prev + curr // previous and current
327
327
  const c_n = curr + next // current and next
328
328
 
329
- // Determine string quotation outside of jsx literals.
330
- // Inside jsx literals, string quotation is still part of it.
331
- if (isSingleQuotes(curr) && !inJsxLiterals()) {
329
+ // Determine string quotation outside of jsx literals and template literals.
330
+ // Inside jsx literals or template literals, string quotation is still part of it.
331
+ if (isSingleQuotes(curr) && !inJsxLiterals() && !inStrTemplateLiterals()) {
332
332
  append()
333
333
  if (prev !== `\\`) {
334
334
  if (__strQuote && curr === __strQuote) {
@@ -599,6 +599,8 @@ function tokenize(code, options) {
599
599
  } else if (
600
600
  // it's jsx literals and is not a jsx bracket
601
601
  (isJsxLiterals && !JSXBrackets.has(curr)) ||
602
+ // it's template literal content (including quotes)
603
+ inStrTemplateLiterals() ||
602
604
  // same type char as previous one in current token
603
605
  ((isWord(curr) === isWord(current[current.length - 1]) || __jsxChild()) && !Signs.has(curr))
604
606
  ) {
@@ -676,9 +678,13 @@ function generate(tokens) {
676
678
  }
677
679
  /** @type {Array<[number, string]>} */
678
680
  const lineTokens = []
681
+ let lastWasBreak = false
682
+
679
683
  for (let i = 0; i < tokens.length; i++) {
680
684
  const token = tokens[i]
681
685
  const [type, value] = token
686
+ const isLastToken = i === tokens.length - 1
687
+
682
688
  if (type !== T_BREAK) {
683
689
  // Divide multi-line token into multi-line code
684
690
  if (value.includes('\n')) {
@@ -693,15 +699,30 @@ function generate(tokens) {
693
699
  } else {
694
700
  lineTokens.push(token)
695
701
  }
702
+ lastWasBreak = false
696
703
  } else {
697
- lineTokens.push([type, ''])
698
- flushLine(lineTokens)
699
- lineTokens.length = 0
704
+ if (lastWasBreak) {
705
+ // Consecutive break - create empty line
706
+ flushLine([])
707
+ } else {
708
+ // First break after content - flush current line
709
+ flushLine(lineTokens)
710
+ lineTokens.length = 0
711
+ }
712
+
713
+ // If this is the last token and it's a break, create an empty line
714
+ if (isLastToken) {
715
+ flushLine([])
716
+ }
717
+
718
+ lastWasBreak = true
700
719
  }
701
720
  }
702
721
 
703
- if (lineTokens.length)
722
+ // Flush remaining tokens if any
723
+ if (lineTokens.length) {
704
724
  flushLine(lineTokens)
725
+ }
705
726
 
706
727
  return lines
707
728
  }
@@ -737,7 +758,12 @@ function toHtml(lines) {
737
758
  /**
738
759
  *
739
760
  * @param {string} code
740
- * @param {{ keywords: Set<string> } | undefined} options
761
+ * @param {
762
+ * {
763
+ * keywords: Set<string>
764
+ * onCommentStart?: (curr: string, next: string) => number | boolean
765
+ * onCommentEnd?: (curr: string, prev: string) => number | boolean
766
+ * } | undefined} options
741
767
  * @returns {string}
742
768
  */
743
769
  function highlight(code, options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sugar-high",
3
- "version": "0.9.3",
3
+ "version": "0.9.5",
4
4
  "type": "module",
5
5
  "types": "./lib/index.d.ts",
6
6
  "main": "./lib/index.js",
@@ -19,25 +19,14 @@
19
19
  "lib"
20
20
  ],
21
21
  "license": "MIT",
22
- "scripts": {
23
- "test": "vitest",
24
- "dev": "next dev docs",
25
- "build": "next build docs"
26
- },
27
22
  "devDependencies": {
28
- "@types/node": "22.10.7",
29
- "@types/react": "19.0.7",
30
- "codice": "^1.3.1",
31
- "dom-to-image": "^2.6.0",
32
- "next": "15.1.5",
33
- "react": "^19.0.0",
34
- "react-dom": "^19.0.0",
35
- "sugar-high": "link:./",
23
+ "@types/node": "22.12.0",
36
24
  "typescript": "5.7.3",
37
25
  "vitest": "^3.0.2"
38
26
  },
39
- "resolutions": {
40
- "sugar-high": "link:./"
41
- },
42
- "packageManager": "pnpm@8.7.1"
43
- }
27
+ "packageManager": "pnpm@8.7.1",
28
+ "scripts": {
29
+ "test": "vitest",
30
+ "build": "echo 'package requires no build'"
31
+ }
32
+ }