rn-shiki 0.0.37-19 → 0.0.37-20

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rn-shiki",
3
3
  "type": "module",
4
- "version": "0.0.37-19",
4
+ "version": "0.0.37-20",
5
5
  "description": "Shiki syntax highlighter for React Native.",
6
6
  "author": "Ryan Skinner <hello@ryanskinner.com>",
7
7
  "license": "MIT",
@@ -7,37 +7,72 @@ export interface HighlighterStyleSheet {
7
7
 
8
8
  export type ReactStyle = Record<string, TextStyle>
9
9
 
10
- const ALLOWED_STYLE_PROPERTIES: Record<string, boolean> = {
11
- color: true,
12
- backgroundColor: true,
13
- fontWeight: true,
14
- fontStyle: true,
10
+ // Helper function to convert hex color to RGB
11
+ function hexToRgb(hex: string): { r: number, g: number, b: number } | null {
12
+ const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
13
+ return result
14
+ ? {
15
+ r: Number.parseInt(result[1]!, 16),
16
+ g: Number.parseInt(result[2]!, 16),
17
+ b: Number.parseInt(result[3]!, 16),
18
+ }
19
+ : null
20
+ }
21
+
22
+ // Helper function to normalize color values
23
+ function normalizeColor(color: string | undefined): string {
24
+ if (!color)
25
+ return 'transparent'
26
+
27
+ // Type guard to ensure color is string before we call string methods
28
+ if (typeof color === 'string' && color.startsWith('#')) {
29
+ const rgb = hexToRgb(color)
30
+ if (rgb) {
31
+ return `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`
32
+ }
33
+ }
34
+ return color
15
35
  }
16
36
 
17
37
  export function convertTokenColorsToReactStyle(tokenColors: any[]): ReactStyle {
18
38
  const styleMap: ReactStyle = {}
19
39
 
40
+ // Add default styles
41
+ styleMap.default = {
42
+ color: '#e1e4e8',
43
+ backgroundColor: 'transparent',
44
+ }
45
+
20
46
  tokenColors.forEach((token) => {
21
- const { scope, settings } = token
47
+ if (!token.settings || !token.scope)
48
+ return
49
+
50
+ const scopes = Array.isArray(token.scope) ? token.scope : [token.scope]
22
51
  const styleEntries: Array<[string, string]> = []
23
52
 
24
- if (settings.foreground)
25
- styleEntries.push(['color', settings.foreground])
26
- if (settings.background)
27
- styleEntries.push(['backgroundColor', settings.background])
28
- if (settings.fontStyle) {
29
- if (settings.fontStyle.includes('italic'))
53
+ // Only add styles if they exist
54
+ if (token.settings.foreground) {
55
+ styleEntries.push(['color', normalizeColor(token.settings.foreground)])
56
+ }
57
+ if (token.settings.background) {
58
+ styleEntries.push(['backgroundColor', normalizeColor(token.settings.background)])
59
+ }
60
+ if (token.settings.fontStyle) {
61
+ if (token.settings.fontStyle.includes('italic')) {
30
62
  styleEntries.push(['fontStyle', 'italic'])
31
- if (settings.fontStyle.includes('bold'))
63
+ }
64
+ if (token.settings.fontStyle.includes('bold')) {
32
65
  styleEntries.push(['fontWeight', 'bold'])
66
+ }
33
67
  }
34
68
 
35
69
  if (styleEntries.length > 0) {
36
- const transformedStyle = transform(styleEntries.filter(([key]) => ALLOWED_STYLE_PROPERTIES[key]))
70
+ const transformedStyle = transform(styleEntries)
37
71
 
38
- const scopes = Array.isArray(scope) ? scope : [scope]
39
- scopes.forEach((sc: string) => {
40
- styleMap[sc] = { ...(styleMap[sc] || {}), ...transformedStyle }
72
+ scopes.forEach((scope: string) => {
73
+ if (typeof scope === 'string') {
74
+ styleMap[scope] = { ...(styleMap[scope] || {}), ...transformedStyle }
75
+ }
41
76
  })
42
77
  }
43
78
  })
@@ -46,12 +81,27 @@ export function convertTokenColorsToReactStyle(tokenColors: any[]): ReactStyle {
46
81
  }
47
82
 
48
83
  export function getRNStylesFromShikiStyle(theme: any): HighlighterStyleSheet {
49
- console.log('Converting theme to React Native styles:', theme)
50
- if (!theme.tokenColors) {
84
+ if (!theme || !theme.tokenColors) {
51
85
  console.error('Provided theme does not contain \'tokenColors\'.')
52
86
  return {}
53
87
  }
54
- const reactStyle = convertTokenColorsToReactStyle(theme.tokenColors)
55
- console.log('Converted theme to React Native styles:', reactStyle)
56
- return reactStyle
88
+
89
+ try {
90
+ // Convert theme colors to React Native styles
91
+ const styles = convertTokenColorsToReactStyle(theme.tokenColors)
92
+
93
+ // Add theme-specific base styles if they exist
94
+ if (theme.colors) {
95
+ styles.base = {
96
+ color: normalizeColor(theme.colors['editor.foreground']),
97
+ backgroundColor: normalizeColor(theme.colors['editor.background']),
98
+ }
99
+ }
100
+
101
+ return styles
102
+ }
103
+ catch {
104
+ console.error('Error converting theme styles')
105
+ return {}
106
+ }
57
107
  }