sugar-high 0.6.1 → 0.7.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.js +71 -12
  3. package/package.json +9 -9
package/README.md CHANGED
@@ -25,7 +25,7 @@ document.querySelector('pre > code').innerHTML = codeHTML
25
25
 
26
26
  ### Highlight with CSS
27
27
 
28
- Then make your own theme with customized colors by token type and put in global CSS. The corresponding class names star with `sh__` prefix.
28
+ Then make your own theme with customized colors by token type and put in global CSS. The corresponding class names start with `--sh-` prefix.
29
29
 
30
30
  ```css
31
31
  /**
package/lib/index.js CHANGED
@@ -435,6 +435,11 @@ function tokenize(code) {
435
435
  }
436
436
  // `=` in property=<value>
437
437
  if (next === '=' && !inStringContent()) {
438
+ // if current is not a space, ensure `prop` is a property
439
+ if (isSpaces(current)) {
440
+ append(T_SPACE, current)
441
+ current = ''
442
+ }
438
443
  const prop = current ? (current + curr) : curr
439
444
  if (isIdentifier(prop)) {
440
445
  current = prop
@@ -578,20 +583,52 @@ function tokenize(code) {
578
583
 
579
584
  /**
580
585
  * @param {Array<[number, string]>} tokens
581
- * @return {Array<string>}
586
+ * @return {Array<any>}
582
587
  */
583
588
  function generate(tokens) {
584
- const linesHtml = []
585
- const createLine = (content) => `<span class="sh__line">${content}</span>`
589
+ const lines = []
590
+ /**
591
+ * @param {any} children
592
+ *
593
+ */
594
+ const createLine = (children) =>
595
+ // generateType === 'html'
596
+ // ? `<span class="sh__line">${content}</span>`
597
+ ({
598
+ type: 'element',
599
+ tagName: 'span',
600
+ children,
601
+ properties: {
602
+ className: 'sh__line',
603
+ },
604
+ })
586
605
 
606
+ /**
607
+ * @param {Array<[number, string]>} tokens
608
+ * @returns {void}
609
+ */
587
610
  function flushLine(tokens) {
588
- linesHtml.push(createLine(
589
- tokens.map(([type, value]) => (
590
- `<span class="sh__token--${types[type]}" style="color: var(--sh-${types[type]})">${encode(value)}</span>`
591
- ))
592
- .join('')
593
- ))
611
+ /** @type {Array<any>} */
612
+ const lineTokens = (
613
+ tokens
614
+ .map(([type, value]) => (
615
+ {
616
+ type: 'element',
617
+ tagName: 'span',
618
+ children: [{
619
+ type: 'text',
620
+ value: value, // to encode
621
+ }],
622
+ properties: {
623
+ className: `sh__token--${types[type]}`,
624
+ style: `color: var(--sh-${types[type]})`,
625
+ },
626
+ }
627
+ ))
628
+ )
629
+ lines.push(createLine(lineTokens))
594
630
  }
631
+ /** @type {Array<[number, string]>} */
595
632
  const lineTokens = []
596
633
  for (let i = 0; i < tokens.length; i++) {
597
634
  const token = tokens[i]
@@ -620,7 +657,26 @@ function generate(tokens) {
620
657
  if (lineTokens.length)
621
658
  flushLine(lineTokens)
622
659
 
623
- return linesHtml
660
+ return lines
661
+ }
662
+
663
+ function toHtml(lines) {
664
+ return lines
665
+ .map(line => {
666
+ const { tagName: lineTag } = line
667
+ const tokens = line.children
668
+ .map(child => {
669
+ const { tagName, children, properties } = child
670
+ return `<${tagName} class="${
671
+ properties.className
672
+ }" style="${
673
+ properties.style
674
+ }">${encode(children[0].value)}</${tagName}>`
675
+ })
676
+ .join('')
677
+ return `<${lineTag} class="${line.properties.className}">${tokens}</${lineTag}>`
678
+ })
679
+ .join('\n')
624
680
  }
625
681
 
626
682
  /**
@@ -630,17 +686,20 @@ function generate(tokens) {
630
686
  */
631
687
  function highlight(code) {
632
688
  const tokens = tokenize(code)
633
- const output = generate(tokens).join('\n')
689
+ const lines = generate(tokens)
690
+ const output = toHtml(lines)
634
691
  return output
635
692
  }
636
693
 
637
694
  // namespace
638
695
  const SugarHigh = /** @type {const} */ {
639
696
  TokenTypes: types,
697
+ TokenMap: new Map(types.map((type, i) => [type, i])),
640
698
  }
641
699
 
642
700
  export {
643
701
  highlight,
644
702
  tokenize,
703
+ generate,
645
704
  SugarHigh,
646
- }
705
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sugar-high",
3
- "version": "0.6.1",
3
+ "version": "0.7.1",
4
4
  "type": "module",
5
5
  "types": "./lib/index.d.ts",
6
6
  "exports": {
@@ -12,18 +12,18 @@
12
12
  "lib"
13
13
  ],
14
14
  "license": "MIT",
15
+ "scripts": {
16
+ "test": "vitest run",
17
+ "dev": "next dev docs",
18
+ "build": "next build docs"
19
+ },
15
20
  "devDependencies": {
16
21
  "codice": "^0.2.0",
17
- "next": "14.2.3",
22
+ "next": "14.2.4",
18
23
  "react": "^18.3.1",
19
24
  "react-dom": "^18.3.1",
20
25
  "sugar-high": "link:./",
21
26
  "vitest": "^1.2.2"
22
27
  },
23
- "packageManager": "pnpm@8.7.1",
24
- "scripts": {
25
- "test": "vitest run",
26
- "dev": "next dev docs",
27
- "build": "next build docs"
28
- }
29
- }
28
+ "packageManager": "pnpm@8.7.1"
29
+ }